Questions tagged [side-effect]
50 questions
81
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,532
50
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
- 119,268
48
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
- 803
37
votes
7 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,352
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
- 957
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
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
18
votes
4 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,148
17
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
- 281
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
- 1,012
15
votes
4 answers
What side-effects, if any, are okay when importing a python module?
Generally, modules should not have side effects. However, in a lot of cases, the side-effects are hard to avoid or may be desirable. There are also popular packages with on-import side-effects.
Which side effects, and when, are allowable when…
Kaia
- 422
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
- 924
- 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
- 557
11
votes
4 answers
Is it OK for a function to both do-something and return-something?
Is it OK for a function to both do-something and return-something? I try to avoid it for the most part, but there are situations where trying to avoid it would lead to worse/less clean code.
Ex:
if ( false == read_from_file( file, dest_buffer,…
Newline
- 221
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