80

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.

2
  • 6
    A program without side effects is useless, so side effects are neither evil nor taboo. But FP encurages delimiting the code with side effects, so as large a part of the code as possible are side-effect free functions. This is encouraged because side-effect free functions and subsystems are easier to understand, easier to analyze, easier to test and easier to optimize.
    – JacquesB
    Jan 28, 2016 at 19:34
  • @JacquesB It would make a good answer to explain why they are easier to understand, easier to analyse, easier to test and easier to optimize.
    – ceving
    Dec 13, 2016 at 16:58

14 Answers 14

82

Writing your functions/methods without side effects - so they're pure functions - makes it easier to reason about the correctness of your program.

It also makes it easy to compose those functions to create new behaviour.

It also makes certain optimisations possible, where the compiler can for instance memoise the results of functions, or use Common Subexpression Elimination.

Edit: at Benjol's request: Because a lot of your state's stored in the stack (data flow, not control flow, as Jonas has called it here), you can parallelise or otherwise reorder the execution of those parts of your computation that are independent of each other. You can easily find those independent parts because one part doesn't provide inputs to the other.

In environments with debuggers that let you roll back the stack and resume computing (like Smalltalk), having pure functions means that you can very easily see how a value changes, because the previous states are available for inspection. In a mutation-heavy calculation, unless you explicitly add do/undo actions to your structure or algorithm, you cannot see the history of the computation. (This ties back to the first paragraph: writing pure functions makes it easier to inspect the correctness of your program.)

7
  • 4
    Maybe consider adding something about concurrency in your answer?
    – Benjol
    Oct 28, 2010 at 8:48
  • 7
    Side-effect free functions are easier to test and to reuse. Oct 28, 2010 at 10:56
  • @Lenny222: reuse was what I was hinting at by talking about function composition. Oct 28, 2010 at 11:22
  • @Frank: Ah, ok, too shallow browsing. :) Oct 28, 2010 at 11:38
  • @Lenny222: It's ok; it's probably a good thing to spell it out. Oct 28, 2010 at 11:56
30

From an article about Functional programming:

In practice, applications need to have some side effects. Simon Peyton-Jones, a major contributor to the functional programming language Haskell, said the following: "In the end, any program must manipulate state. A program that has no side effects whatsoever is a kind of black box. All you can tell is that the box gets hotter." (http://oscon.blip.tv/file/324976) The key is to limit side effects, clearly identify them, and avoid scattering them throughout the code.

1
28

You've got it wrong, functional programming promotes limiting side effects to make programs easy to understand and optimize. Even Haskell allows you to write to files.

Essentially what I am saying is that functional programmers don't think side effects are evil, they simply think limiting the use of side effects is good. I know it may seem like such a simple distinction but it makes all the difference.

5
  • Which is why they're "something like taboo" - FPLs encourage you to limit side effects. Oct 28, 2010 at 6:30
  • +1 for the approach. side effects still exist. indeed, they are limited
    – Belun
    Oct 28, 2010 at 9:22
  • For clarification, I haven't said 'why side effect is not allowed in functional programming' or 'why side effect is not needed'. I know It IS allowed in functional languages and sometimes is a must. But it is very much discouraged in functional programming. Why? That was my question.
    – Gulshan
    Oct 28, 2010 at 9:25
  • @Gulshan - Because side effects make programs harder to understand and optimize. Oct 28, 2010 at 14:00
  • in the case of haskell, the main point isn't to "limit side effects". side effects are impossible to express in the LANGUAGE. what functions like readFile are doing is defining a sequence of actions. this sequence is functionally pure and is kind of like an abstract tree describing WHAT to do. the actual dirty side effects are then carried out by the runtime.
    – sara
    Jun 30, 2016 at 13:45
15

A few notes:

  • Functions without side effects can trivialy be executed in parallel, while functions with side effects typically require some sort of synchronisation.

  • Functions without side effects allow for a more aggressive optimization (e.g. by transparentely using an result cache), because as long as we get the right result, it doesn't even matter whether or not the function was really executed

4
  • Very interesting point: it doesn't even matter whether or not the function was really executed. It would be interesting to end up with a compiler that can get rid of subsequent calls to side effect free functions given equivalent parameters. Oct 25, 2017 at 12:29
  • 1
    @NoelWidmer Something like that already exists. Oracle's PL/SQL offers a deterministic clause for functions without side effects, so they do not get executed more often than necessary.
    – user281377
    Oct 25, 2017 at 13:54
  • Wow! However, I think languages should be semantically expressive so that the compiler can figure it out by itself without having to specify an explicit flag (I am unsure what a clause is). A solution could be to specify parameters to be mutable/immutable f.e. Generally speaking, this would require a strong type system in which assumptions about side effects can be made by the compiler. And the feature would need to be able to be turned off if desired. Opt-out instead of opt-in. That's just my opinion, based on the limited knowledge I have since I read your answer :) Oct 25, 2017 at 13:59
  • The deterministic clause is just a keyword that tells the compiler that this is a deterministic function, comparable to how the final keyword in Java tells the compiler that the variable cannot change.
    – user281377
    Oct 26, 2017 at 15:59
14

I primarily work in functional code now, and from that perspective it seems blindingly obvious. Side effects create a huge mental burden on programmers trying to read and understand code. You don't notice that burden until you are free from it for a while, then suddenly have to read code with side effects again.

Consider this simple example:

val foo = 42
// Several lines of code you don't really care about, but that contain a
// lot of function calls that use foo and may or may not change its value
// by side effect.

// Code you are troubleshooting
// What's the expected value of foo here?

In a functional language, I know that foo is still 42. I don't even have to look at the code in between, much less understand it, or look at the implementations of the functions it calls.

All that stuff about concurrency and parallelization and optimization is nice, but that's what computer scientists put on the brochure. Not having to wonder who's mutating your variable and when is what I really enjoy in day to day practice.

1
  • Some languages have const specifiers for function parameters, so that you know if a function can modify values or not.
    – Nuclear
    Feb 19, 2021 at 8:47
6

Few to no languages make it impossible to cause side-effects. Languages that were completely side-effect free would be prohibitively difficult (near to impossible) to use, except in a very limited capacity.

Why side-effects are considered evil?

Because they make it much more difficult to reason about exactly what a program does, and to prove that it does what you expect it to do.

At a very high level, imagine testing an entire 3-tier web site with only black-box testing. Sure, it's doable, depending on the scale. But there is certainly a lot of duplication going on. And if there is a bug (that is related to a side-effect), then you could potentially break the entire system for further testing, until the bug is diagnosed and fixed, and the fix is deployed to the test environment.

Benefits

Now, scale that down. If you were fairly good at writing side-effect free code, how much faster would you be at reasoning at what some existing code did? How much faster could you write unit tests? How confident would you feel that the code with no side-effects was guaranteed bug-free, and that users could limit their exposure to any bugs it did have?

If code has no side-effects, the compiler also may have additional optimizations that it could perform. It may be much easier to implement those optimizations. It may be much easier to even conceptualize an optimization for side-effect free code, which means that your compiler vendor might implement optimizations that are difficult-to-impossible in code with side effects.

Concurrency is also drastically simpler to implement, to automatically generate, and to optimize when code has no side-effects. This is because all the pieces can be safely evaluated in any order. Allowing programmers to write highly concurrent code is widely considered the next big challenge that Computer Science needs to tackle, and one of the few remaining hedges against Moore's Law.

2
  • 1
    Ada makes it very difficult to cause side-effects. It is not impossible though, but you clearly know what you do then.
    – mouviciel
    Oct 28, 2010 at 9:22
  • @mouviciel: I think there are at least a few useful languages out there that make side-effects very difficult, and try to relegate them to Monads. Oct 28, 2010 at 9:27
5

Well, IMHO, this is quite hypocritical. Nobody likes side effects, but everybody needs them.

What is so dangerous about side effects is that if you call a function, then this possibly has an effect not only on the way the function behaves when it is called next time, but possibly it has this effect on other functions. Thus side effects introduce unpredictable behavior and nontrivial dependencies.

Programming paradigms such as OO and functional both address this problem. OO reduces the problem by imposing a separation of concerns. This means the application state, which consists of a lot of mutable data, is encapsulated into objects, each of which is responsible for maintaining its own state only. This way the risk of dependencies is reduced and problems are far more isolated and easier to track.

Functional programming takes a far more radical approach, where the application state is simply immutable from the perspective of the programmer. This is a nice idea, but renders the language useless on its own. Why? Because ANY I/O-operation has side effects. As soon as you read from any input stream, you application state is likely to change, because the next time you invoke the same function, the result is likely to be different. You may be reading different data, or - also a possibility - the operation might fail. The same is true for output. Even output is an operation with side effects. This is nothing you realize often nowadays, but imagine you have only 20K for your output and if you output any more, your app crashes because you're out of disk space or whatever.

So yes, side effects are nasty and dangerous from the perspective of a programmer. Most bugs come from the way certain parts of the application state are interlocked in a nearly obscure way, through unconsidered and oftentimes unnecessary side effects. From the perspective of a user, side effects are the point of using a computer. They don't care for what happens inside or how it is organized. They do something and expect the computer to CHANGE accordingly.

4
  • interestingly logic programming not only does not have functional side effects; but you can't even change the value of a variable once assigned.
    – Ilan
    Oct 28, 2010 at 10:31
  • @Ilan: This is also true for some functional languages and it is a style easy to adopt.
    – back2dos
    Oct 28, 2010 at 14:17
  • "Functional programming takes a far more radical approach, where the application state is simply immutable from the perspective of the programmer. This is a nice idea, but renders the language useless on its own. Why? Because ANY I/O-operation has side effects": FP does not forbid side-effects, it rather restricts them when not necessary. E.g. (1) I/O -> side effects are necessary; (2) computing an aggregate function from a sequence of values -> side effect (e.g. for loop with accumulator variable) not necessary.
    – Giorgio
    Nov 18, 2012 at 19:31
  • "read from any input stream" is strictly speaking a side-cause, not a side-effect. Writing to an output stream would be a side-effect. See: blog.jenkster.com/2015/12/what-is-functional-programming.html
    – Magne
    Feb 10, 2022 at 13:26
4

Side effects are like "leaks" in your code that will need to be handled later, either by you or some unsuspecting coworker.

Functional languages avoid state variables and mutable data as a way of making code less context dependent and more modular. Modularity insures that the work of one developer won't affect/undermine the work of another.

Scaling rate-of-development with team size, is a "holy grail" of software development today. When working with other programmers, few things are as important as modularity. Even the most simple of logical side effects make collaboration extremely difficult.

3
  • +1 - "or some unsuspecting coworker" Oct 28, 2010 at 9:35
  • 1
    -1 for side effects being "leaks that need to be handled." Creating "side effects" (non-pure-functional code) is the entire purpose of writing any non-trivial computer program. Jan 19, 2011 at 18:33
  • This comment comes six years later, but there are side effects and then there are side effects. The desirable kind of side effects, doing I/O and so on, are indeed necessary to any program, because you need to give your results to the user somehow — but the other kind of side effects, where your code changes state without a good reason like doing I/O, are indeed a "leak" that will need to be handled later. The basic idea is command-query separation: a function that returns a value should not have a side effect.
    – rmunn
    Nov 18, 2017 at 5:24
2

Any side-effect introduces extra input/output parameters which must be taken in account when testing.

This make code validation much more complex as the environment cannot be limited to just the code being validated, but must bring in some or all of the surrounding environment (the global that is updated lives in that code over there, which in turn depends on that code, which in turn depends on living inside a full Java EE server....)

By trying to avoid side-effects you limit the amount of externalism needed to run the code.

1

In my experience good design in Object Orientated programing mandates the use of functions that have side effects.

For example, take a basic UI desktop application. I may have a running program that has on its heap an object graph representing the current state of domain model of my program. Messages arrive to the objects in that graph (for instance, via methods calls invoked from the UI layer controller). The object graph (domain model) on the heap is modified in response to the messages. Observers of the model are informed of any changes, the UI and maybe other resources are modified.

Far from being evil the correct arrangement of these heap-modifying and screen-modifying side effects are at the core of OO design (in this case the MVC pattern).

Of course, that does not mean that your methods should have arbitary side-effects. And side effect free functions do have a place in improving the readbility and sometimes performance, of your code.

2
  • 1
    Observers (including the UI) should find out about modifications via subscribing to the messages/events that your object send out. This is not a side effect unless the object directly modifies the observer - which would be bad design.
    – ChrisF
    Oct 28, 2010 at 10:42
  • 1
    @ChrisF Most defintely it is a side effect. The message passed to the observer (in an OO langauge most likely a method call on an interface) will lead to the state of the UI component on the heap changing (and these heap objects are visible to other parts of the program). The UI component is neither a parameter of the method or the return value. In a formal sense, for a function to be side effect free it must be idempotent. Notification in the MVC pattern is not, for example the UI may display a list of messages it has received - console - calling it twice results in a different program state. Oct 28, 2010 at 15:17
1

As questions above have pointed out, functional languages don't so-much prevent code from having side effects as provide us with tools for managing what side effects can happen in a given piece of code and when.

This turns out to have very interesting consequences. First, and most obviously, there are numerous things that you can do with side-effect free code, which have already been described. But there are other things we can do, too, even when working with code that does have side effects:

  • In code with mutable state, we can manage the scope of the state in such a way as to statically ensure that it cannot leak outside of a given function, this enabling us to collect garbage without either reference counting or mark-and-sweep style schemes, yet still be sure that no references survive. The same guarantees are also useful for maintaining privacy-sensitive information, etc. ( This can be achieved using the ST monad in haskell)
  • When modifying shared state in multiple threads, we can avoid the need for locks by tracking changes and performing an atomic update at the end of a transaction, or rolling the transaction back and repeating it if another thread made a conflicting modification. This is only achievable because we can ensure that the code has no effects other than the state modifications (which we can happily abandon). This is performed by the STM (Software Transactional Memory) monad in Haskell.
  • we can track the effects of code and trivially sand box it, filtering any effects it may need to perform in order to be sure it's safe, thus allowing (for example) user inputted code to be executed securely on a web site
0

Evil is a bit over the top.. it all depends on the context of the usage of the language.

Another consideration to those already mentioned is that it makes proofs of correctness of a program much simpler if there are no functional side effects.

0

In complex code bases, complex interactions of side effects are the most difficult thing I find to reason about. I can only speak personally given the way my brain works. Side effects and persistent states and mutating inputs and so on make me have to think about "when" and "where" things happen to reason about correctness, not just "what" is happening in each individual function.

I can't just focus on "what". I can't conclude after thoroughly testing a function which causes side effects that it will spread an air of reliability throughout the code using it, since callers might still misuse it by calling it at the wrong time, from the wrong thread, in the wrong order. Meanwhile a function that causes no side effects and just returns a new output given an input (without touching the input) is pretty much impossible to misuse in this way.

But I'm a pragmatic type, I think, or at least try to be, and I don't think we necessarily have to stamp out all side effects to the barest minimum to reason about the correctness of our code (at the very least I would find this very difficult to do in languages like C). Where I find it very difficult to reason about correctness is when we have the combination of complex control flows and side effects.

Complex control flows to me are the ones that are graph-like in nature, often recursive or recursive-like (event queues, e.g., which aren't directly calling events recursively but are "recursive-like" in nature), maybe doing things in the process of traversing an actual linked graph structure, or processing a non-homogeneous event queue that contains an eclectic mixture of events to process leading us to all kinds of different parts of the codebase and all triggering different side effects. If you tried to draw out all the places you'll ultimately end up in the code, it would resemble a complex graph and potentially with nodes in the graph you never expected would have been there in that given moment, and given that they are all causing side effects, that means you might not just be surprised about what functions are called but also what side effects are occurring during that time and the order in which they are occurring.

Functional languages can have extremely complex and recursive control flows, but the result is so easy to comprehend in terms of correctness because there aren't all sorts of eclectic side effects going on in the process. It's only when complex control flows meet eclectic side effects that I find it headache-inducing to try to comprehend the entirety of what's going on and whether it'll always do the right thing.

So when I have those cases, I often find it very difficult, if not impossible, to feel very confident about the correctness of such code, let alone very confident that I can make changes to such code without tripping on something unexpected. So the solution to me there is either simplify the control flow or minimize/unify the side effects (by unifying, I mean like only causing one type of side effect to many things during a particular phase in the system, not two or three or a dozen). I need one of those two things to happen to allow my simpleton brain to feel confident about the correctness of the code that exists and the correctness of the changes I introduce. It is pretty easy to be confident about the correctness of code introducing side effects if the side effects are uniform and simple along with the control flow, like so:

for each pixel in an image:
    make it red

It's pretty easy to reason about the correctness of such code, but mainly because the side effects are so uniform and the control flow is so dead simple. But let's say we had code like this:

for each vertex to remove in a mesh:
     start removing vertex from connected edges():
         start removing connected edges from connected faces():
             rebuild connected faces excluding edges to remove():
                  if face has less than 3 edges:
                       remove face
             remove edge
         remove vertex

Then this is ridiculously oversimplified pseudocode which would typically involve far more functions and nested loops and much more things that would have to go on (updating multiple texture maps, bone weights, selection states, etc), but even the pseudocode makes it so difficult to reason about correctness because of the interaction of the complex graph-like control flow and side effects going on. So one strategy to simplify that is to defer the processing and just focus on one type of side effect at a time:

for each vertex to remove:
     mark connected edges
for each marked edge:
     mark connected faces
for each marked face:
     remove marked edges from face
     if num_edges < 3:
          remove face

for each marked edge:
     remove edge
for each vertex to remove:
     remove vertex

... something to this effect as one iteration of simplification. That means we're passing through the data multiple times which is definitely incurring a computational cost, but we often find we can multithread such resulting code more easily, now that the side effects and control flows have taken on this uniform and simpler nature. Furthermore each loop can be made more cache-friendly than traversing the connected graph and causing side effects as we go (ex: use a parallel bit set to mark what needs to be traversed so that we can then do the deferred passes in sorted sequential order using bitmasks and FFS). But most importantly, I find the second version so much easier to reason about in terms of correctness as well as change without causing bugs. So that's how I approach it anyway and I apply the same kind of mindset to simplify mesh processing there above as simplifying event handling and so forth -- more homogeneous loops with dead simple control flows causing uniform side effects.

And after all, we need side effects to occur at some point, or else we'd just have functions that output data with nowhere to go. Often we need to record something to a file, display something to a screen, send the data over through a socket, something of this sort, and all of these things are side effects. But we can definitely reduce the number of superfluous side effects that go on, and also reduce the number of side effects going on when the control flows are very complicated, and I think it'd be a lot easier to avoid bugs if we did.

-1

It is not evil. My opinion, it is necessary to distinguish the two function types - with side effects and without. The function without side effects: - returns always the same with same arguments, so for example such function without any arguments makes no sense. - That also means, that the order in what some such functions are called plays no role - must be able to run and may be debugged just alone (!), without any another code. And now, lol, look what JUnit makes. A function with side effects: - has sort of "leaks", what can be highlighted automatically - it is very important by debugging and searching of mistakes, what generally are caused by side effects. - Any function with side effects has also a "part" of itself without side effects, what can also be separated automatically. So evil are those side effects, what produce difficult mistakes to track.

1
  • this doesn't seem to offer anything substantial over points made and explained in prior 12 answers
    – gnat
    Apr 22, 2016 at 13:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.