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

Is there a way to avoid using side effects to process this data

I have an application I'm writing that runs script plugins to automate what a user used to have to do manually through a serial terminal. So, I am basically implementing the serial terminal's functionality in code. One of the functions of the…
gfree
  • 479
  • 7
  • 16
0
votes
0 answers

Is (++x > ++x) well-defined in c++?

I read a bit about undefined behavior regarding side-effects and sequence points and their replacement in C++11. (Undefined behavior and sequence points) It raised the following question. Does this code have defined behavior? int x = 0; bool result…
Gonen I
  • 5,576
  • 1
  • 29
  • 60
0
votes
0 answers

SML/NJ : Calling function directly, versus using "val _ = fn...."

A content question (not a homework question) from a Coursera course on programming languages (taught at U of Washington) - I'm getting no response on the Coursera forum.... Abridged code (variable 'cbs' is a list reference that is already defined…
JayW
  • 23
  • 5
0
votes
1 answer

Add a side-effect to a function in a generic way

How can I write a Kotlin generic function that takes a function as an argument and adds a side-effect to it? For instance, fun something(one: Int, two: String): String { return "${one}, ${two}" } fun somethingElse(arg: Array): String {…
digory doo
  • 1,978
  • 2
  • 23
  • 37
0
votes
0 answers

What difference does it make if we don't perform the same side-effects after every re-render in React class component?

I came across this class component with 2 lifecycle methods handling the component's side-effects after each re-render: componentDidMount and componentDidUpdate I copied the example (at the bottom) and tried to play with it by deleting…
Eddie Lam
  • 579
  • 1
  • 5
  • 22
0
votes
1 answer

Constexpr functions not called at compile-time if result is ignored

I was investigating some rather weird code-coverage results of constexpr functions (compile-time calls can't get instrumented by the code-coverage tool I use), and noticed that some constexpr functions were getting called as runtime functions, if…
Sean
  • 393
  • 2
  • 11
0
votes
1 answer

When do Applicatives run their effects?

Under "Why do Static Arrows generalise Arrows?" post, we often spoke of Applicative running side-effects before the application of the function-in-the-functor to the value-in-the-functor. Also, danidiaz used this fact to show that Arrow is not…
Zhiltsoff Igor
  • 1,812
  • 8
  • 24
0
votes
2 answers

Are there any languages that handle functional impurity (side effects) without modeling them as RealWorld or IO?

One thing that always bothered me in Haskell (and other functional languages, for that matter) is that the entire language is pure, but side-effects are indirectly allowed by using an object that represents the entire "real world" (the IO monad, for…
user3F31A28
  • 104
  • 2
  • 13
0
votes
3 answers

Why this python code occurs the side-effect?

def to_un(g): un = [] for v in g: un.append(v) print(g) for v in range(len(un)): for u in un[v]: if v not in un[u]: un[u].append(v) print(g) print(g == un) print(g is un) def…
0
votes
1 answer

Can not implement callback inside of useFocusEffect from React-navigation

I have React-native app with topTabNavigator with three tabs. And usually componentDidMount and componentWillUnmount lifecycle methods don't work when the user changes the tab. Therefore instead of them I decided to use for the side effects…
0
votes
2 answers

Why is this java code not producing the result I am expecting?

The program needs to issue the correct ticket price based on the age and gender of the customer. import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Person…
Rajsh
  • 21
  • 1
0
votes
1 answer

Are side effects always visible in task continuations?

Are side effects guaranteed to be visible in task continuations? object o = null; Task.Run (() => o = new object ()) .ContinueWith (t => o.ToString ()); The continuation may run on a different thread than the first task. If there is no kind of…
mafu
  • 31,798
  • 42
  • 154
  • 247
0
votes
1 answer

How to check if a function has no side-effects (is pure) at runtime?

So say we have loaded a function F that take in/out a set of args and returns a result. How to check at runtime if this F does not act on anything other than args members and functions? Meaning no Console.Writeline, Singletons (or other stuff not…
DuckQueen
  • 772
  • 10
  • 62
  • 134
0
votes
1 answer

Unittest mocked class's method not being called

I'm new to Mock testing. For learning purposes, I'm trying to simulate the status check whether the database connection established or not and no of times the Handler is trying to perform database connect operation. class Handler: def…
Surya Bhusal
  • 520
  • 2
  • 8
  • 28
0
votes
1 answer

View result of Scala Future for side effects

I have a method that returns a Future. I want to log success or failure based on it, and then process the result. In Java with streams, I would do something like getCollection().stream(). peek(e -> System.out.println("element: " + e). …
Troy Daniels
  • 3,270
  • 2
  • 25
  • 57