Questions tagged [side-effects]

A side-effect is a programming term that refers to intended changes in the program behavior, such as a variable changing value. It is in important term when discussing compiler optimization and expression evaluation.

A side-effect is a programming term that refers to intended changes in the program behavior, such as a variable changing value. It is in important term when discussing compiler optimization and expression evaluation.

Most notably, this term is often used in the C and C++ languages. One formal definition of the term can be found in ISO 9899:2011 (C11) 5.1.2.3 §2:

Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations are all side effects, which are changes in the state of the execution environment. Evaluation of an expression in general includes both value computations and initiation of side effects.

C++ contains an identical definition, see for example C++11 1.9/12.

When a compiler optimizes code, it has to ensure that it removes no side effects (C11 5.1.2.3 §4):

In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object).

Therefore the volatile qualifier is often used to prevent the compiler from optimizing cerain parts of the code, since any access (read/write) to a volatile object is considered a side effect.

Side effects is also an important term when discussing expression evaluation. For example, expressions that contain multiple side effects on the same variable, with no so-called "sequence point" in between, invokes undefined behavior, see Undefined behavior and sequence points.

461 questions
0
votes
7 answers

Assigning to an object

Is there any reasonable explanation of following techinque: //value HAS TO BE CHANGED those are requirements int f(int value) { int tmp = value;//In order to not change "value" inside fnc - again requirements //do something with tmp return…
There is nothing we can do
  • 23,727
  • 30
  • 106
  • 194
0
votes
1 answer

Sequence points in a language with left to right evaluation order?

When evaluation order is specified as "left to right" and language is a (pseudo) C-like, which are the sequence points in the following examples? int x = 1; int z = x-- + x; // z = 1 + 0 or z = 1 + 1? my_func(x++); // x incremented before or after…
gremo
  • 47,186
  • 75
  • 257
  • 421
0
votes
3 answers

Dealing with side effects: helper function to chain constructor and setup method

When working with existing libraries (can't edit constructors or companion objects) I often find that I need to call an init/setup procedure on an object and then return the object itself. I usually end up doing something like the following. def…
Felix Glas
  • 15,065
  • 7
  • 53
  • 82
0
votes
1 answer

How to solve redux side-effects overlapping? (redux-observable)

I have an Angular 4 application, which uses angular-router to navigate between different pages (lets call them pageA and pageB). pageA and pageB load same list of items - sharedState.items (which is reloaded from time to time). Here is the redux…
Ostap
  • 353
  • 1
  • 4
  • 8
0
votes
0 answers

Python: default parameter replaced by wrong side effect value

I am pretty new to python so it might simply be that I did not understand some parts of the language. Here is some code that I managed to make minimal for the sake of the example: class A(object): def __init__(self): self.m = 5 class…
Kiplaki
  • 171
  • 6
0
votes
1 answer

side effects on constant variable of nested array type

I encountered strange side effects that I cannot explain to myself in the slightest. Probably I am missing something very obvious, but I have looked for the bug for several hours now and the code is quite simple, so I concluded that I must have some…
lo tolmencre
  • 3,804
  • 3
  • 30
  • 60
0
votes
0 answers

How does a list comprehension in python avoid side effects?

from python documentation : We can calculate the list of squares without any side effects using: squares = [x**2 for x in range(10)] My question is - How is this without side effects ? the above code also creates (or overwrites) a variable named…
Thalish Sajeed
  • 1,351
  • 11
  • 25
0
votes
1 answer

"Stacking" effectful functions in functional language - DrRacket

DrRacket is a functional programming language built in lisp. I created an effectful function called run-ins which take in an instruction and make some changes to specified variables (I'm trying to re-create a working computer) Now, I want to create…
user289661
  • 181
  • 7
0
votes
3 answers

is there any example of function that has no side effect but non-referential transparency?

I know side effect and non-referential transparency mean different thing. "side effect, which implies some violation of referential transparency." "Referential transparency means that an expression (such as a function call) can be replaced with…
wew tie
  • 23
  • 5
0
votes
1 answer

RxJava subscription to side effect

I have a question, it is simple business logic flow: check whether an employee in multiple departments, department and employee relationship is in cache, first check whether the relationships exists in cache, if exist, check whether employee belong…
junfei wang
  • 111
  • 1
  • 5
0
votes
1 answer

What is the point of no side effects if I need 3 loops instead of one to do it?

Imagine I have a list of objects that are Questions. I have to find if they are visible and if they are visible I have to change their answer to visible as well. (The example may not be practical it is just an example) If this is no side effect way…
h3dkandi
  • 1,106
  • 1
  • 12
  • 27
0
votes
1 answer

XCode 8: Puzzling side effect of setting label.text

It appears that setting the text of a label can have the side effect of increasing the opacity of a MKCircle that has been added to an MKMapView. Here is what's going on: First I have this delegate function to handle rendering my overylay func…
user3915477
  • 275
  • 4
  • 11
0
votes
1 answer

Clojure Quil: nested loops while drawing lines

I'm having a serious problem with figuring out an algorithm in the Clojure Quil library. I've been trying to make some diagrams of networks/flocking patterns like this with the functional middleware mode. My question is how do you create a nested…
0
votes
1 answer

Side effects in Fluxible?

Here's what I'm trying to do---add a listener to a fluxible event to do an ajax call. In Redux, this is called a side effect and there are several ways of doing this (ex: create "side effects" using redux-saga, which are just listeners that do…
U Avalos
  • 6,538
  • 7
  • 48
  • 81
0
votes
1 answer

How to implement function with side effects?

I am beginner in programming via Haskell language. I am interested at writing function in Haskell, which has side effects like this (for educational purposes, it can have another side effect): -- function foo behaves like random numbers…
Alex Aparin
  • 4,393
  • 5
  • 25
  • 51