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
5 answers

Boolean AND does not evaluate both sides of the expression

class Program { static void Main(string[] args) { bool success = true; int[] array = { 10, 15, 20 }; foreach (var i in array) success = success && SynchronizeAccount(i); } static bool SynchronizeAccount(int i) { …
Denis
  • 3,653
  • 4
  • 30
  • 43
0
votes
1 answer

How to call functions inside ternary operators in UnityScript?

I'm trying to decide which function to call, based on a boolean value. myBooleanVariable ? function1() : function2(); Unity gives the error : Expressions in statements must only be executed for their side-effects. So why does this not work,…
jeff
  • 13,055
  • 29
  • 78
  • 136
0
votes
0 answers

How do I get animated swipe background effect in Listview, similar to swipe to call feature in android enabled phone

I have an app developed using jquery mobile and build using phonegap. I wanted to achieve the similar effect as that of "swipe to call" feature in android enabled phones in my listview. Not sure how to go forward. checked some other posts but those…
Prals
  • 606
  • 3
  • 7
  • 22
0
votes
1 answer

Reactive Depth-First Search with cut

I am trying to develop a reactive DFS implementation that produces distinct nodes, without traversing unnecessary paths. The tree may contain duplicate/equal nodes. If a node has been traversed, then all of it's children have been traversed as well,…
derabbink
  • 2,419
  • 1
  • 22
  • 47
0
votes
2 answers

using rspec to test side-effects

In my model Passages I have a method receives_damage: def receives_damage self.damage += 1 self.phrases.each do |phrase| if !phrase.blank && phrase.hit_points <= self.damage phrase.blank = true phrase.content =…
Ziggy
  • 21,845
  • 28
  • 75
  • 104
0
votes
1 answer

Using NSArray's makeObjectsPerformSelector to have side effects

I have an NSArray of Foos in an Objective-C program. I would like to call the doIt function of each Foo, however, the makeObjectsPerformSelector function of NSArray does not allow the original Foos to be modified, per the docs. The doIt selector…
Mat Kelly
  • 2,327
  • 7
  • 29
  • 51
0
votes
1 answer

how to add a clip toggle effect to a static bit of jquery

I'm pretty new to jquery and I've been trying to add a 'clip' slide-open effect to a static bit of jquery I have. this is the effect that I'm after... http://www.tutorialspoint.com/cgi-bin/practice.cgi?file=jquery_142 and this is my code:
0
votes
2 answers

How do I create a stack in a REST API?

I am working on a distributed execution server. I have decided to use a REST API based on HTTP on the server. The clients will connect to the server and GET the next task to be accomplished. Obviously I need to "update" the task that is retrieved…
0
votes
5 answers

How to use properly recursion and side effects in python

In a tree structure, I'm trying to find all leafs of a branch. Here is what I wrote: def leafs_of_branch(node,heads=[]): if len(node.children()) == 0: heads.append(str(node)) else: for des in node.children(): …
Lpmat
  • 29
  • 9
0
votes
1 answer

Side effects of a iphone app

is there a way to measure side effects of a iphone app on other apps performance? at least using a black-box focus. Any suggestion about a monitorization app with this orientation? I have found that my app can know nothing about other apps, but I…
emecas
  • 1,586
  • 3
  • 27
  • 43
0
votes
1 answer

Side Effect Tracking in SSA

I am working on an optimizer for Java bytecode and decided to use SSA. However, most optimizations require all operations to be purely functional, so in order to handle side effects, I decided to add an extra opaque state parameter and return value…
Antimony
  • 37,781
  • 10
  • 100
  • 107
0
votes
1 answer

unexpected side effect of map and for loop in scala

Perhaps a simple to be answered question, but I did not find a satisfying answer from the API. I am not trying to write nice code, but I am trying to learn more on how certain things work: I have created an initial HashMap. From an arbitrary list I…
Wayne Jhukie
  • 147
  • 2
  • 12
-1
votes
1 answer

Need clarification of the meaning of "side effect" in C

I have read many discussions of what constitutes a "side effect" in C, and many seem to indicate that it must involve changing something that is not local to the function causing the change. Changing an external variable or a file are the typical…
BenevolentDeity
  • 693
  • 1
  • 4
  • 18
-1
votes
1 answer

Which design pattern violation does this bug do and what to call it?

There is a singleton service class ItemsDataSource: IItemsDataSource injected into many other classes (business domain classes) These business domain classes are many and run asynchronously calling methods on that ItemsDataSource service. public…
EEAH
  • 715
  • 4
  • 17
-1
votes
1 answer

Side-Effects Occurring Even When Declaring New Variables in Void Functions

I was doing a question where I had to return all the permutations of an input array, and I noticed something really peculiar. For the purpose of this question, I removed any distracting, actual permutation parts of the code to demonstrate what I'm…
Stacking_Flow
  • 115
  • 1
  • 1
  • 11
1 2 3
30
31