Functional purity v/s Side effects
You must have created so many functions already in your suited programming languages. But never must have noticed, whether my function has a side effect or my function is pure.
In this article, I am going to give you a quick understanding of what Side effects and Pure functions mean.
To understand the above terms, we need to know what deterministic and non-deterministic functions mean in the programming world.
Deterministic :
Deterministic functions always return the same result any time they are called with a specific set of input values (arguments).
const add = (x, y) => x + yconst multiply = (x, y) => x * yconst surfaceAreaCalculator = (radius) => {
return 4 * 3.14 * square(radius);
}
In the above sample code the add, multiply, and surfaceAreaCalculator are examples of deterministic functions.
While calculating the return value, these functions don’t take into account anything else, only the argument. It doesn’t care what time it is, what weather it is, what other functions are working in the program, what values are there in some variables outside. Functions like these are called deterministic.
Non-Deterministic :
A non-deterministic function may return different results every time it is called, even when the same input values are provided.
Math.random(); // => 0.4011148700956255
Math.random(); // => 0.8533405303023756
Math.random(); // => 0.3550692005082965
Consider the above random function, even calling the same function with no argument again and again does not guarantee that it may produce the same output. Let’s look at another example.
let outVar = 0;const f = () => {
outVar = outVar + 1;
return true;
}f();
The above function ‘f’ has a side effect on the outside variable called “outVar”. But still, the function is a deterministic function. You may ask Why?. If you notice the function ‘f’ returns the same value i.e true every time.
console.log(“Hello Sumeet”)
The console.log is also a deterministic function since it always returns undefined as its output. But has side effects on the outside world to print the text on the screen.
But what if we modify the function and ‘f’ and change its return type to something else. Let’s see.
let outVar = 0;const f = () => {
outVar = outVar + 1;
return outVar;
}f();
If you closely look at the function ‘f’, the function has no guarantee that it would return the same value(output) again and again when called an arbitrary number of times. It depends on some outside factor, namely, the current value of outVar
.
Again, this is not bad, after all, a program without side effects will not be useful. We want our programs to do something, to somehow change the world — show something on the screen, make a noise, send an email, etc. But it’s possible to minimize side effects in your functions and programs, and it’s a good idea, really.
This is called programming side effects and has little disadvantages.
Fewer side effects a function has — the better.
When a function has no side effects and is deterministic — we call it a “Pure function”. It’s so predictable, clean, and transparent.
Pure functions are:
- easier to read
- easier to debug
- easier to test
- the order they are called in doesn’t matter
- easy to make them work in parallel (simultaneously)
Please if you really like the article, give it 50 claps and also write your feedback in the comments section.