What is purity?
Purity for a function is defined as one that will always return the same output for the same input and perform no side effects.
So what do we mean by same output for the same input?
well... lets see some examples...
lets say we have this code:
let y = 2
const addYtoX = (x) => x + y
This function is not considered pure as the output could be modified by the value of y
witch mean we cannot guaranty it will return the same output for the same input.
let y = 2;
const addYtoX = (x) => x + y
console.log(addYtoX(3)) // 5
y = 1
console.log(addYtoX(3)) // 4
As you can see the value of y
will affect the output of the function even if we call it using the same input... this is definitely impure.
Lets look at the pure version of the same function:
const addYtoX = (x, y) => x + y
There you go!!!
I can call this guy as many times as I want and I can be sure to get the same output for the same input.
That's cool man, but what about that side effect thing?
well... lets see the same function but with added side effects!
const addYtoX = (x, y) => {
makeSomeNoodles()
return x + y
};
I hope you can guess the side effect is caused by the makeSomeNoodles
function call right?
Why is that bad? Well... the function is suppose to add x to y and now it is making noodles before computing my values? And I have no idea what is happening during that function.... maybe it is actually crashing my simple function before it can execute or worst!!!
Anyway, I hope you get the basic idea. :)
Until next time... stay pure!
Top comments (20)
I don't think that the
void
return type has anything to to with function purity. A function can be pure even if it returns some value. It just shouldn't modify anything outside of it and does not use any variables that are defined outside of its scope. Its return value should be determined solely by its input parameters.@tuxOr
There is nothing to do with the signature of a function.
A function is pure if and only if
So in your example, assuming
process(var1, var2)
will make some side effect somewhere else in the program, it is not a pure function.However, if
process
do not make any changes, then it will still be a pure function.No.
For example:
This function is not pure as it modifies
a
.Yes I would say that Luka is right.
And would add that for me, making a function that returns void is a way of declaring that that particular function is impure and will most likely be made to do side effects stuff.... cause we need to do them sometimes:)
I think something that returns VOID is not even a function at all, but a procedure....don't functions need input/ouput by mathematical definitions?
This is the working definition that I use.
that is a great way to describe it I think!
Currently this is not valid Javascript:
const addYtoX(x, y) => x + y
But this is:
const addYtoX = (x, y) => x + y
oups! I just corrected that, thanks a lot!
Any useful function returning
void
is definitely impure.Even if it doesn't reference anything besides its parameters, it's still bound to mutate or otherwise bother the parameters.
A pure function:
One of the consequences of 2. is that when given the same parameters, it should always produce the same return value.
This is a useful dipping stick that demonstrates that functions that depend on eg time or randomness aren't pure.
Unfortunately the signature wont make this function pure.
What you would need in this case is
composition
which would allow you to compose 2 functions. I will at one point cover composition, in the mean time, you can read that great article by Eric Elliott --> medium.com/javascript-scene/master...Luka is right. For function to be pure it shouldn't change things out of it's scope too which
void
function can do. It's because whole point of pure functions is to write more predictable code.Funny that you choose JavaScript for the examples, while it doesn't support pure functions.
There is a good article on the topic that seems to spark some interesting debates--> hackernoon.com/do-pure-functions-e...
I feel we can still benefit from the concept and that indeed JavaScript will remain a language full of surprises ;) Maybe that is why we like it so much in the end?