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
11
votes
2 answers

Side effects of changing filter and requirements of an existing app in Android Play/Market

No previous questions about it, so here I ask. Background: I have an old app, in free and paid versions, in the Play Market. I created a new version, radically changed and with a different payment system (free app + in app purchases only, no more a…
davidcesarino
  • 16,160
  • 16
  • 68
  • 109
10
votes
2 answers

Python closure with side-effects

I'm wondering if it's possible for a closure in Python to manipulate variables in its namespace. You might call this side-effects because the state is being changed outside the closure itself. I'd like to do something like this def closureMaker(): …
Mike
  • 58,961
  • 76
  • 175
  • 221
9
votes
0 answers

How to keep `[<-` side-effects-free?

I just discovered that `[<-` may have destructive side-effects or not depending on what arguments one gives it. Example x <- 1:5 x # [1] 1 2 3 4 5 `[<-`(x, 1:5, 1) # [1] 1 1 1 1 1 x # [1] 1 2 3 4 5 # ∴ last application of `[<-` was…
kjo
  • 33,683
  • 52
  • 148
  • 265
9
votes
2 answers

What counts as a side-effect? Why isn't memory allocation a side-effect?

I understand the appeal of pure functional languages like Haskell where you can keep track of side effects like disk I/O using monads. Why aren't all system calls considered side effects? For example, heap memory allocation (which is automatic) in…
9
votes
3 answers

What are expressions with side effects and why should they be not passed to a macro?

I came across a statement in the text C How to Program: "Expressions with side effects (i.e., variable values are modified) should not be passed to a macro because macro arguments may be evaluated more than once.". My question is what are…
user4642421
9
votes
5 answers

Non destructive way of deleting a key from a hash

Is there a non-destructive way of deleting a key value pair from a hash? For example, if you did original_hash = {:foo => :bar} new_hash = original_hash new_hash = new_hash.reject{|key, _| key == :foo} or original_hash = {:foo => :bar} new_hash =…
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
8
votes
2 answers

OCaml: Does storing some values to be used later introduce "side effects"?

For a homework assignment, we've been instructed to complete a task without introducing any "side-effects". I've looked up "side-effects" on Wikipedia, and though I get that in theory it means "modifies a state or has an observable interaction with…
Casey Patton
  • 4,021
  • 9
  • 41
  • 54
8
votes
2 answers

Pytest: Mock multiple calls of same method with different side_effect

I have a unit test like so below: # utilities.py def get_side_effects(): def side_effect_func3(self): # Need the "self" to do some stuff at run time. return {"final":"some3"} def side_effect_func2(self): # Need…
user1452759
  • 8,810
  • 15
  • 42
  • 58
8
votes
1 answer

Handling API calls using useEffect vs using useCallback

This is very likely a dumb question -- I have the understanding that anything that triggers a side-effect should be handled with useEffect. I was wondering if that understanding is correct. Particularly in the context of making an API call -- is it…
Eddie Lam
  • 579
  • 1
  • 5
  • 22
8
votes
2 answers

*p++->str : Understanding evaluation of ->

My question is about the following line of code, taken from "The C Programming Language" 2nd Edition: *p++->str; The book says that this line of code increments p after accessing whatever str points to. My understanding is as follows: Precedence…
thatmarkdude
  • 233
  • 1
  • 6
8
votes
1 answer

Surgical XML editing with Powershell

I'm working with csproj files using Powershell to perform large-scale editing of project references. So far I've managed to edit the Include attributes on ProjectReferences using the following lines: $projectXml = [xml](Get-Content…
bwerks
  • 8,651
  • 14
  • 68
  • 100
8
votes
2 answers

Assignment in method call bad practice?

This is my first question to Stackoverflow, although I have been a consumer for many years. Please forgive me if I break rules. That is certainly not my intent. I have strenuously reviewed the rules and believe I am within the bounds of…
Java Jive
  • 181
  • 3
8
votes
4 answers

Side effects in Redux reducer

Redux reducers should be without side-effects. But what if an action should trigger the download of a file in the browser where the content is based on the state of the store? Surely this should count as a side effect? Would something like the…
damd
  • 6,116
  • 7
  • 48
  • 77
8
votes
7 answers

Functions that look pure to callers but internally use mutation

I just got my copy of Expert F# 2.0 and came across this statement, which somewhat surprised me: For example, when necessary, you can use side effects on private data structures allocated at the start of an algorithm and then discard these …
J Cooper
  • 16,891
  • 12
  • 65
  • 110
8
votes
3 answers

Formatting multiple json files recursively

This is a theoretical question about minimizing side effects in bash scripting. I recently used a simple mechanism for formatting a bunch of json files, in a nested directory structure... for f in `find ./ -name *json`; do echo $f ; python…
jayunit100
  • 17,388
  • 22
  • 92
  • 167