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
27
votes
1 answer

PSR-1 2.3 Side Effects Rule

I have a question Regarding PHP Basic Coding Standards PSR1. PSR 1 Rule 2.3 states: Rule 2.3 Side Effects A file SHOULD declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it SHOULD execute logic with…
Brian C.
  • 273
  • 3
  • 5
25
votes
5 answers

What is the origin of "launch the missiles"?

In the context of functional programming, a typical example of a side effect is "launch the missiles". Where does that expression come from historically?
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
24
votes
2 answers

Mock side effect only X number of times

I have a celery retry task that I would like to test that it retries until successful. Using mock's side_effect, I can fail it for a set number of executions and then passing None, clear the side effect. However, the method the task is calling…
voidnologo
  • 1,005
  • 2
  • 12
  • 14
23
votes
1 answer

Pure functional programming in D

To my mind the power of functional purity is when deep code paths can be verified as side-effect free. What are people's experiences in the scale of the code tree that can be inside a pure specifier, and what of the level of code reuse? A few…
John
  • 405
  • 3
  • 6
21
votes
1 answer

VLAs and side-effect in sizeof's operand

I know that sizeof never evaluates its operand, except in the specific case where said operand is a VLA. Or, I thought I knew. void g(int n) { printf("g(%d)\n", n); } int main(void) { int i = 12; char arr[i]; // VLA (void)sizeof…
Quentin
  • 62,093
  • 7
  • 131
  • 191
21
votes
10 answers

Are side-effects possible in pure functional programming

I have been trying to wrap my head around functional programming for a while now. I have looked up lambda calculus, LISP, OCaml, F# and even combinatorial logic but the main problem I have is this - how do you do things that require side effects…
Jeremy E
  • 3,704
  • 2
  • 28
  • 46
20
votes
2 answers

Side Effects in Functional Programming

In a Functional Programming book the author mentions the following are the side effects. Modifying a variable Modifying a data structure in place Setting a field on an object Throwing an exception or halting with an error Printing to the console…
mohamed
  • 311
  • 2
  • 6
20
votes
2 answers

Bug or Feature: Kotlin allows to change 'val' to 'var' in inheritance

I just started to explore the language Kotlin. I'm struggling with inheritance, var&val and side-effects. If I declare a trait A with a val x and override x in AImpl it is possible to override it as var (see code below). Surprisingly the print()…
miho
  • 833
  • 7
  • 11
19
votes
2 answers

Unsequenced value computations (a.k.a sequence points)

Sorry for opening this topic again, but thinking about this topic itself has started giving me an Undefined Behavior. Want to move into the zone of well-defined behavior. Given int i = 0; int v[10]; i = ++i; //Expr1 i = i++; //Expr2 ++ ++i; …
Chubsdad
  • 24,777
  • 4
  • 73
  • 129
18
votes
2 answers

Function argument evaluation and side effects

The C++20 standard says in Function Call, 7.6.1.3/8: The initialization of a parameter, including every associated value computation and side effect, is indeterminately sequenced with respect to that of any other parameter. Indeterminately…
18
votes
3 answers

Scala: "map" vs "foreach" - is there any reason to use "foreach" in practice?

In Scala collections, if one wants to iterate over a collection (without returning results, i.e. doing a side effect on every element of collection), it can be done either with final def foreach(f: (A) ⇒ Unit): Unit or final def map[B](f: (A) ⇒ B):…
GreyCat
  • 16,622
  • 18
  • 74
  • 112
18
votes
1 answer

Why can I call a non-constexpr function inside a constexpr function?

Consider the following code: #include constexpr int f() { return printf("a side effect!\n"); } int main() { char a[f()]; printf("%zd\n", sizeof a); } I would have expected the compiler to complain about the call to printf…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
18
votes
4 answers

Why can applicative functors have side effects, but functors can't?

I'm feeling rather silly asking this question, but it's been on my mind for a while and I can't find any answers. So the question is: why can applicative functors have side effects, but functors can't? Maybe they can and I've just never noticed...?
Brian
  • 692
  • 3
  • 14
16
votes
3 answers

Getter with side effect

I create a class whose objects are initialized with a bunch of XML code. The class has the ability to extract various parameters out of that XML and to cache them inside the object state variables. The potential amount of these parameters is large…
Boris Gorelik
  • 29,945
  • 39
  • 128
  • 170
16
votes
2 answers

Side effects in Scala

I am learning Scala right in these days. I have a slight familiarity with Haskell, although I cannot claim to know it well. Parenthetical remark for those who are not familiar with Haskell One trait that I like in Haskell is that not only functions…
Andrea
  • 20,253
  • 23
  • 114
  • 183
1
2
3
30 31