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

Are composed functions contained in an IO monad guaranteed to have no not-desired effects?

I understand that IO is used to separate pure code from impure one. Also, I am aware that IO allows for referential transparency. One thing about IO is still a bit obscure to me though. Namely, a guarantee that between IO contained actions nothing…
Mateusz Wit
  • 425
  • 5
  • 10
0
votes
1 answer

Is it possible to garantee the consistency of memoization?

I want to build a module I with memoization. The type I.t contains a real complex content c, and some mutable properties (eg, mutable is_cool). The module provides outside with functions to calculate and get the properties (eg, is_cool), which can…
SoftTimur
  • 5,630
  • 38
  • 140
  • 292
0
votes
1 answer

What does 'pure' in functional programming mean if an application mutates the stack?

We know that pure functions: Always return the same result for a given input Produce no side-effects This leads us to referential transparency - where an expression can be replaced with a value without changing the behaviour of the program. This…
0
votes
1 answer

Rename() intrinsic function side effects?

GNU fortran manual section "10.5.2.126 Rename Intrinsic (function)" says "Due to the side effects performed by this intrinsic, the function form is not recommended." On other hand section "8.11.9.213 Rename Intrinsic (subroutine)" says "Some…
Roux
  • 435
  • 3
  • 12
0
votes
1 answer

How do I prevent a const& argument from being changed indirectly?

Is it possible to make a const& truely immutable? int* side_effect; void function(int const& i){ *side_effect = 123; } int main(){ int i = 0; side_effect = &i; //based on the function signature, i is suspected not to change. …
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
0
votes
3 answers

How to unit test side effecting logic

I have a following code public class Component extend Framework { private Integer someInt; private String someString; public Integer getSomeInt() { return someInt; } public String getSomeString() { return someString; } …
user_1357
  • 7,766
  • 13
  • 63
  • 106
0
votes
1 answer

How to revert side-effects when disabling an action in Redux logger?

Assume that an action creator (or a middleware) has a side-effect of starting a service: service.start(). The disable-action feature of redux-devtools reverts the state changes due to that specific action. How do we make sure that the side-effects…
Panagiotis Panagi
  • 9,927
  • 7
  • 55
  • 103
0
votes
1 answer

Working with references in Java to create side effects

I'm used to working with languages that explicitly allow you to work with references/pointers, what I'm attempting to do relies upon that present. If I have an object of class Foo and I have three objects of class Bar. How do I have all three…
Mattisdada
  • 979
  • 1
  • 15
  • 24
0
votes
0 answers

IO monad and ordering

I am playing with IO monad and learning to harness side effects in Haskell and it seems like I got it wrong. Consider the following code: main = do putStr "test" getLine return () My understanding here is that do "glues" three IO monads…
Eugene Loy
  • 12,224
  • 8
  • 53
  • 79
0
votes
1 answer

Recursion with the least sideeffect

So I have a tree of people with children and I only want to get the people with cars. if a child has a car but the parent does not, I want to keep the parent in the tree. I thought the best way would be to use a recursive function, that looked like…
Edwinhai
  • 67
  • 1
  • 1
  • 7
0
votes
1 answer

Bash arithmetic expression's side effects not executed

$ declare -i i=0 $ for j in {0..2}; do echo "${j} $((i++))"; done 0 0 1 1 2 2 $ for j in {0..2}; do echo "$(echo "${j} $((i++))")"; done 0 3 1 3 2 3 $ Why i doesn't get incremented in the 2nd for loop? (Yes, I know that there's a workaround.)
0
votes
1 answer

How does the elimination of side effect facilitate parallel and concurrent programming?

Are there any successful and practical functional languages(such as Erlang) or libraries that are base on the assumption that the program written in such language has no side effect or only has controlled side effect(for example, represented by…
Alaya
  • 3,287
  • 4
  • 27
  • 39
0
votes
2 answers

Are there any types with side-effecting methods that return the original type?

Often I find myself wanting to chain a side-effecting function to the end of another method call in a more functional-looking way, but I don't want to transform the original type to Unit. Suppose I have a read method that searches a database for a…
Michael Zajac
  • 55,144
  • 7
  • 113
  • 138
0
votes
2 answers

Why short circuiting gains priority over increment in C?

void main() { int i=-3, j=2, k=0,m; m = ++i && ++j || ++k; printf("\n%d %d %d %d",i,j,k,m); } This snippet prints -2 3 0 1 but why not -2 3- 1 1 . Eventhough the ++ operator of k has precedence over logical operator, why k is not incremented…
muru
  • 111
  • 1
  • 1
  • 10
0
votes
0 answers

Tree structure in DBs

I am challenged with persisting a structure that is inherently tree-like: a drawing can have multiple layers a layer can have multiple lines a line can have multiple points My current implementation is with MongoDB, points as an Array attribute of…
aledalgrande
  • 5,167
  • 3
  • 37
  • 65