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
180
votes
4 answers

Why are global variables evil?

I'm trying to find out why the use of global is considered to be bad practice in python (and in programming in general). Can somebody explain? Links with more info would also be appreciated.
LarsVegas
  • 6,522
  • 10
  • 43
  • 67
165
votes
4 answers

Lambda expression vs method reference

IntelliJ keeps proposing me to replace my lambda expressions with method references. Is there any objective difference between both of them?
Gerard
  • 2,784
  • 3
  • 19
  • 17
92
votes
10 answers

if statement - short circuit evaluation vs readability

Sometimes, an if statement can be rather complicated or long, so for the sake of readability it is better to extract complicated calls before the if. e.g. this: if (SomeComplicatedFunctionCall() || OtherComplicatedFunctionCall()) { // do…
relaxxx
  • 7,566
  • 8
  • 37
  • 64
84
votes
2 answers

What Does Webpack 4 Expect From A Package With sideEffects: false

Webpack 4 has added a new feature: it now supports a sideEffects flag in the package.json of the modules it is bundling. From Webpack 4: released today Over the past 30 days we have worked closely with each of the frameworks to ensure that they…
Undistraction
  • 42,754
  • 56
  • 195
  • 331
76
votes
7 answers

Purity vs Referential transparency

The terms do appear to be defined differently, but I've always thought of one implying the other; I can't think of any case when an expression is referentially transparent but not pure, or vice-versa. Wikipedia maintains separate articles for…
52
votes
7 answers

Is the use of del bad?

I commonly use del in my code to delete objects: >>> array = [4, 6, 7, 'hello', 8] >>> del(array[array.index('hello')]) >>> array [4, 6, 7, 8] >>> But I have heard many people say that the use of del is unpythonic. Is using del bad practice? >>>…
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
52
votes
5 answers

Why is the raising of an exception a side effect?

According to the wikipedia entry for side effect, raising an exception constitutes a side effect. Consider this simple python function: def foo(arg): if not arg: raise ValueError('arg cannot be None') else: return…
canadadry
  • 8,115
  • 12
  • 51
  • 68
46
votes
5 answers

What exactly does "effectful" mean?

Time and again I read the term effectful, but I am still unable to give a clear definition of what it means. I assume the correct context is effectful computations, but I've also seen the term effectful values) I used to think that effectful means…
Martin Drautzburg
  • 5,143
  • 1
  • 27
  • 39
44
votes
3 answers

What is the result of i == (i = 2)?

Run the following code: // In Java, output ##### public static void main(String[] args) { int i = 1; if(i == (i = 2)) { System.out.println("@@@@@"); } else { System.out.println("#####"); } } But: // In C, output…
37
votes
7 answers

What are the alternative of monads to use IO in pure functional programming?

monads are described as the haskell solution to deal with IO. I was wondering if there were other ways to deal with IO in pure functional language.
Cheick
  • 2,154
  • 24
  • 28
32
votes
6 answers

Javascript closures and side effects in plain English? (separately)

I've been reading some JavaScript books and I always hear about closures and side effects. For some reason I can't understand what they really are. Can anyone explain to me what they are in plain English plus examples? (as you were explaining it to…
alexchenco
  • 53,565
  • 76
  • 241
  • 413
32
votes
7 answers

Why are assignments not allowed in Python's `lambda` expressions?

This is not a duplicate of Assignment inside lambda expression in Python, i.e., I'm not asking how to trick Python into assigning in a lambda expression. I have some λ-calculus background. Considering the following code, it looks like Python is…
stefan
  • 674
  • 1
  • 5
  • 13
30
votes
13 answers

Are side effects a good thing?

I feel the term rather pejorative. Hence, I am flabbergasted by the two sentences in Wikipedia: Imperative programming is known for employing side effects to make programs function. Functional programming in turn is known for its …
30
votes
3 answers

Stream.peek() can be skipped for optimization

I've come across a rule in Sonar which says: A key difference with other intermediate Stream operations is that the Stream implementation is free to skip calls to peek() for optimization purpose. This can lead to peek() being unexpectedly called…
Evgeny Mamaev
  • 1,237
  • 1
  • 14
  • 31
28
votes
3 answers

Which algorithms are hard to implement in functional languages?

I'm dabbling in functional languages and I have found some algorithms (especially those that use dynamic programming) harder to write and sometimes less efficient in worst case runtime. Is there a class of algorithms which are less efficient in…
1
2 3
30 31