Questions tagged [side-effect]
46 questions
80
votes
14 answers
Why are side-effects considered evil in functional programming?
I feel that side effects are a natural phenomenon. But it is something like taboo in functional languages. What are the reasons?
My question is specific to functional programming style. Not all programming languages/paradigms.

Gulshan
- 9,402
- 10
- 58
- 89
49
votes
9 answers
Return considered harmful? Can code be functional without it?
OK, so the title is a little clickbaity but seriously I've been on a tell, don't ask (TDA) kick for a while. I like how it encourages methods to be used as messages in true object-oriented fashion. But this has a nagging problem that has been…

candied_orange
- 102,279
- 24
- 197
- 315
46
votes
7 answers
What do you call a function where the same input will always return the same output, but also has side effects?
Say we have a normal pure function such as
function add(a, b) {
return a + b
}
And then we alter it such that it has a side effect
function add(a, b) {
writeToDatabase(Math.random())
return a + b;
}
It's not considered a pure function as far…

m0meni
- 773
- 1
- 6
- 12
33
votes
6 answers
Asynchronous Programming in Functional Languages
I'm mostly a C/C++ programmer, which means that the majority of my experience is with procedural and object-oriented paradigms. However, as many C++ programmers are aware, C++ has shifted in emphasis over the years to a functional-esque style,…

Charles Salvia
- 7,342
- 1
- 35
- 33
30
votes
3 answers
Different ways to see a monad
While learning Haskell I have faced a lot of tutorials trying to explain what are monads and why monads are important in Haskell. Each of them used analogies so it would be easier to catch the meaning.
At the end of the day, I have end up with 3…

Oni
- 927
- 1
- 9
- 13
20
votes
5 answers
Is the benefit of the IO monad pattern for handling side effects purely academic?
Sorry for yet another FP + side effects question, but I couldn't find an existing one which quite answered this for me.
My (limited) understanding of functional programming is that state/side effects should be minimised and kept separate from…

Stu Cox
- 311
- 2
- 6
19
votes
5 answers
Why is reading from memory not a side-effect but reading from a file is?
What does exactly make reading from the process memory a pure operation? Suppose I created an array of 100 integers in the global memory and then took the 42th element of this array. It is not a side effect, right? So why is reading the same array…

ZhekaKozlov
- 311
- 2
- 7
17
votes
2 answers
Side effect-free interface on top of a stateful library
In an interview with John Hughes where he talks about Erlang and Haskell, he has the following to say about using stateful libraries in Erlang:
If I want to use a stateful library, I usually build a side
effect-free interface on top of it so that…

beta
- 992
- 2
- 8
- 16
16
votes
5 answers
Is there a non-deterministic function without side effects?
By definition, a pure function is deterministic + no side effect.
Is there any example for a function which has no side effects, but is non-deterministic? I.e., a function without side effects, but not pure.
To me non-deterministic function comes…

Helin Wang
- 271
- 1
- 5
15
votes
3 answers
Origin of "a method should return a value or have side-effects, but not both"
I read once that a method should either have a return value (and be referentially transparent), or have side-effect(s), but not both. I cannot find any references to this rule, but want to learn more about it.
What is the origin of this advice? …

Wayne Conrad
- 1,118
- 10
- 20
14
votes
5 answers
How to create scalable & side-effect free integration tests?
In my current project, I am having a hard time coming up with a good solution to create scalable integration tests that have no side effects. A little clarification on the side effect free property: it is mostly about the database; there shouldn't…

Guven
- 914
- 5
- 18
12
votes
3 answers
Side Effects Breaking Referential Transparency
Functional Programming in Scala explains a side effect’s impact on breaking referential transparency:
side effect, which implies some violation of referential transparency.
I’ve read part of SICP, which discusses using the “substitution model” to…

Kevin Meredith
- 541
- 1
- 4
- 14
11
votes
3 answers
Do functional programming languages disallow side effects?
According to Wikipedia, Functional programming languages, that are Declarative, they disallow side effects. Declarative programming in general, attempts to minimize or eliminate side effects.
Also, according to Wikipedia, a side effect is related…

codebot
- 221
- 2
- 4
11
votes
2 answers
Where do we put "asking the world" code when we separate computation from side effects?
According to Command-Query Separation principle, as well as Thinking in Data and DDD with Clojure presentations one should separate side effects (modifying the world) from computations and decisions, so that it would be easier to understand and test…

Alexey
- 1,199
- 1
- 9
- 18
10
votes
4 answers
Is it ever OK for a conditional to have side effects?
I'm taking intermediate data structures course as a prereq for entry into the CS MS program at a University everyone in America has heard of. One line of code that was written in class caught my eye:
if (a > 33 | b++ < 54) {...}
This would not pass…

rianjs
- 210
- 1
- 8