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
15
votes
3 answers

Most common pattern for using a database in a functional language, given desire for no side-effects?

I'm trying to get my head around a core concept of functional langauges: "A central concept in functional languages is that the result of a function is determined by its input, and only by its input. There are no…
Allyl Isocyanate
  • 13,306
  • 17
  • 79
  • 130
15
votes
4 answers

Infinite loop with ngrx/effects

I'm trying to understand ngrx/effects. I have built a simple function that increments number by 1 with each click. But it's going in an infinite loop when clicked, not sure whats going on. I'm sure im making some stupid…
Sudhakar
  • 2,904
  • 8
  • 33
  • 47
15
votes
7 answers

is there any way to prevent side effects in python?

Is there any way to prevent side effects in python? For example, the following function has a side effect, is there any keyword or any other way to have the python complain about it? def func_with_side_affect(a): a.append('foo')
yigal
  • 3,923
  • 8
  • 37
  • 59
14
votes
2 answers

How to deal with side effects in tree shaking code?

I've been trying to learn how to write code that is tree shaking friendly, but have run into a problem with unavoidable side effects that I'm not sure how to deal with. In one of my modules, I access the global Audio constructor and use it to…
Steven Lambert
  • 5,571
  • 2
  • 29
  • 46
14
votes
10 answers

Is there a way to unit test against side effects?

Any code can provide side effects. Most of the time, side effects can be a sign of bad design and/or need of refactorisation, but when unit testing I find it hard to test against. Consider the following example: [Test] public void…
Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
14
votes
1 answer

How are side effects and observable behavior related in C++?

C++03 Standard 1.9/6 defines observable behavior: The observable behavior of the abstract machine is its sequence of reads and writes to volatile data and calls to library I/O functions. and then and then 1.9/7 defines side effects: Accessing an…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
13
votes
4 answers

What are the best resources for learning how to avoid side effects and state in OOP?

I've been playing with functional programming lately and there are pretty good treatments on the topic of side effects, why they should be contained, etc. In projects where OOP is used, I'm looking for some resources which lay out some strategies…
Robert Campbell
  • 6,848
  • 12
  • 63
  • 93
12
votes
5 answers

Does placement-new introduce a sequence point?

Consider the following line of code: new (p++) T(); If the constructor T() throws an exception, is p guaranteed to have already been incremented?
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
12
votes
6 answers

C# Paradigms: Side effects on Lists

I am trying to evolve my understanding of side effects and how they should be controlled and applied. In the following List of flights, I want to set a property of each flight satisfying a conditions: IEnumerable fResults =…
Pierre
  • 1,607
  • 3
  • 21
  • 33
12
votes
2 answers

Should macros have side effects?

Can (or should) a macro expansion have side effects? For example, here is a macro which actually goes and grabs the contents of a webpage at compile time: #lang racket (require (for-syntax net/url)) (require (for-syntax…
Ord
  • 5,693
  • 5
  • 28
  • 42
11
votes
4 answers

Generate unique numbers at compile time

I want to generate unique numbers for each class in my header, primes in my case primes but let's say this should only be consecutive numbers i.e. 1,2,3,4,etc. Of course I can hardcode these: struct A { enum { ID = 1; }; }; struct B { enum { ID = 2;…
helami
  • 2,099
  • 2
  • 14
  • 17
11
votes
2 answers

Saving to database in stream pipeline

As per the documentation on Oracle's website: Side-effects in behavioral parameters to stream operations are, in general, discouraged, as they can often lead to unwitting violations of the statelessness requirement, as well as other thread-safety…
Titulum
  • 9,928
  • 11
  • 41
  • 79
11
votes
1 answer

python side_effect - mocking behavior of a method

In the mock, I want a certain function to return a new value in the test. This is how i did it. Class MyClass: my_var = None def foo(self, var1): return somevalue def bar(self): my_var = foo(1) Class…
Jieke Wei
  • 303
  • 1
  • 4
  • 12
11
votes
1 answer

Why isn't mySet.erase(it++) undefined behavior, or is it?

Accordint to this quite highly upvoted answer, the canonical way to iterate through a set erasing some elements is the following: for (it = mySet.begin(); it != mySet.end(); ) { if (conditionToDelete(*it)) { mySet.erase(it++); } …
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
11
votes
1 answer

Why is "volatileQualifiedExpr + volatileQualifiedExpr" not necessarily UB in C but in C++?

When I today read the C Standard, it says about side effects Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations are all side effects and the C++ Standard says Accessing an…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
1 2
3
30 31