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

getStrLn in ZIO working with flatMap but not inside for comprehension

val askNameFlatMap: ZIO[Console, IOException, Unit] = putStrLn("What is your Name? ") *> getStrLn.flatMap(name => putStrLn(s"Hello $name")) val askNameFor: ZIO[Console, IOException, Unit] = { val getName: ZIO[Console, IOException, String] =…
0
votes
1 answer

Executing an effect inside an effect in ZIO

So I was writing a simple program in ZIO which would ask user its name and then simply greet him/her. object PrintName extends App { val askName: ZIO[Any, Throwable, ZIO[Console, IOException, Unit]] = for { name: String <-…
0
votes
0 answers

Flutter: How to change values of switches created in loop?

In essence, I have the switches created this way within an overridden build's State method: List daySwitches = []; for (int i = 0; i <= 6; i++) { daySwitches.add(Switch( (...) onChanged: (bool value) { days[i].active =…
Jacques
  • 301
  • 2
  • 13
0
votes
1 answer

How would a functional language actually define/translate primitives to hardware?

Let's say I have a few primitives defined, here using javascript: const TRUE = x => y => x; const FALSE = x => y => y; const ZERO = f => a => a; const ONE = f => a => f(a); const TWO = f => a => f(f(a)); If a language is purely function, how would…
David542
  • 104,438
  • 178
  • 489
  • 842
0
votes
1 answer

How to mimic multiple returns for reused api call using pytest

I'm trying to test a function that looks like this that calls an SDK. FUNCTION TO BE TESTED def create_folder_if_not_exists( sdk: looker_sdk, folder_name: str, parent_id: str) -> dict: folder =…
hselbie
  • 1,749
  • 9
  • 24
  • 40
0
votes
1 answer

Blockchain backtracking logic inside RxJs Observable pipe

Background I am using NestJS and the Observable pattern with the HttpModule to "observe" and eventually forward values returned by a JSON-RPC server, in this case a Blockchain node. In case Blockchains are unfamiliar, they can be…
0
votes
1 answer

how to run side effect on value change in Vue.js?

my goal is to run a sideeffect when the "monthIndex" changes. In react I would use a useEffect hook with the dependency, but I am new to vue. I am basically incrementing the "monthIndex" via buttons, which changes the index in the array…
lache
  • 608
  • 2
  • 12
  • 29
0
votes
1 answer

Is it okey to call collect within a Composable

I took a look at this tutorial and within it the author explains that making network calls in a composable is a bad habit. But what about getting the results in a StateFlow in a composable is that to a bad habit? Or is that okey? So the question is…
jens
  • 207
  • 2
  • 9
0
votes
0 answers

Pass a str variable for flag in Python function?

In python want to do something like this: def func(*args, flag): do something if cond A: flag="some A" else: flag="some B" Then I found the flag passed is always unchanged. If I change the flag to be a list, then yes…
JP Zhang
  • 767
  • 1
  • 7
  • 27
0
votes
1 answer

custom function changes Vue.js data value as side effect

I have a function inside the methods property which takes passedData value from the data() method, does some changes to the object and stores the new value in a constant this somehow causes a side effect which changes the passedData value also. What…
Sator
  • 636
  • 4
  • 13
  • 34
0
votes
1 answer

Compute the max of 2 operands whose evaluation may causes side effects, in preprocessing

I found myself in need of computing the maximal value of 2 operands. The 2 operands may have side effects and must be evaluated only once, and The evaluation take place in preprocessing macro, and allocation of additional variable is not preferred…
DannyNiu
  • 1,313
  • 8
  • 27
0
votes
2 answers

How to avoid side-effects (Javascript)

I'm currently building a blackjack game, and one of my classes is called "Deck". This deck needs to do two things: Return a set of cards that other objects can use (this is equivalent to dealing those cards out) Remove those cards from the deck…
bugsyb
  • 5,662
  • 7
  • 31
  • 47
0
votes
0 answers

Randomly choosing from list without putting back with unequal probability distribution in functional/side effect free way

I have a study related "problem". I'm trying to write as functional/side-effect free code as possible in Python. The problem ist as follows: I need to choose x items from a list based on different probabilities for each item. The newly chosen items…
0
votes
0 answers

Using React.useMemo to pass arrays to react-table, one of the useMemo's returns an empty array due to renders

I am hitting two different api's for my data and am using React memo to pass them as variables into a react-table useTable function. When I run all my code without the useTable, the reactMemo seems to work, I can console the two variables that I am…
Anders Kitson
  • 1,413
  • 6
  • 38
  • 98
0
votes
2 answers

Sum values of nested objects without side-effects (reduce)

I'm trying to sum nutritional values nested inside food objects: [ { id: 'Potato', nutrition: [ { id: 'PROT', val: 4 }, { id: 'SUGAR', val: 1 } ] }, { id: 'Banana', nutrition: [ { id: 'FIB', val: 10 }, …