You probably already use pure functions all the time and are unaware of what they are or what advantages they bring to your code base. Watch the video below to find out why pure functions are better and much more.
In this episode, we’re gonna be talking about pure functions what they are and how to use them. So what is a pure function? For a function to be considered pure it has to follow two simple rules.
The first one is that it has to be deterministic. That means that given the same inputs it will always return the same output. Let’s take a look at an example of a few deterministic and a few non-deterministic functions.
Okay, so in this example, we have a deterministic function. No matter how many times we call the square function if we pass in the same value we’ll get the same result. So if we pass in 3 we will get 9 every single time we call that function. Now let’s take a look at a non-deterministic function. This is one I wrote earlier I’m gonna paste it in. This function, similar the last one takes a number is an argument and multiplies it by a random number between 1 and 10.
Now, every time we call this function we have no way to guarantee what the value is going to be. This is a non-deterministic function. This is obviously a very simple example of what one is and there are more complicated more real-life scenarios, for example, a function which connects to an API or speaks to a database is a non-deterministic function because we can’t guarantee the results of what they’re going to return. The database might fail or the server that you’re connecting to might not be online.
The second rule that pure functions must follow is that they can’t have side-effects. Now, what this means is that a pure function isn’t allowed to alter variables outside of its own scope. If the variable isn’t passed in, the function shouldn’t know about it and shouldn’t operate on it in any way. Let’s have a look at an example of what that means.
So here we have an example of an impure function. We have an array of animals here cat, dog and fish. We have a function which pushes a new animal into an array and then on line 8 we’re calling that function and we’re going to print out the results. We print out the new animals first and then the original animals.
Alright, as I said, this is an impure function. It has a side effect and it’s probably not immediately obvious. So let’s run the code and see what we get. They’re both the same. What!? Essentially what’s happening here is that this push function has side effects. It affects the original array. It doesn’t create a new one and return a new value.
Let’s have a look instead of how we could do this in a way that is pure and doesn’t have any side effects. In this example, I’ve altered the code very slightly on line four. Instead of using
array.push to add the horse into the new array, we’re using the spread syntax. We create a new array, we add the animal, in this case, a horse to the start of that and then, we use spread to pluck each item out of this animals array and place it into this new array that we’re returning.
So if we run this code we should find that the original animals array is no longer changed and the new animals array has horse pushed in. So, let’s run that and see what we get. Awesome! So, as we’ve seen there, original animals remains unchanged.
Now that we know what pure functions are let’s discuss a little bit about why we would use them and what kind of benefits they bring to your code base.
The first benefit is that pure functions are incredibly easy to reason about. It’s easy to understand what the function does and given a certain set of inputs you’ll always get the same result. This means that those functions can be passed around your application, used in different parts for that every whirring that it doesn’t have a particular bit of state in your app somewhere that it needs to work properly.
Another advantage of pure functions is that they’re really easy to test because pure functions don’t rely on external state variables it’s easy to mock up every part of what that function does.