Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.4 Immutability

We typically work with immutable data in functional programming. It may seem like more work to program immutably, and inefficient to boot, but there are benefits to using immutable data. You'll learn why in this lesson.

2.4 Immutability

Before we start our project, I want to discuss a concept called immutability, because it's very important in functional programming. And it ties in very well with the pure functions that we talked about in the previous lesson. So let's create a new file called Immutability. And if you're not familiar with that term, it's very simple. It basically means that it cannot be changed. If something is immutable, it means it cannot be mutated, thus it is consistent, it's safe. And that sounds a whole lot like the pure functions that we talked about in the previous lesson. If something is consistent, it is predictable, and therefore, it is also testable. So let's start with just a normal variable, we'll have salutation. And we will assign that to hello and everything's great. Our application is saying hi to everyone, hi Jim, Hi Bob, hi you, and you, and you. And then later on, it might be months down the road, there's an update to the application. And for some reason, somebody mistakenly sets a salutation to bye. And so suddenly our application is now saying bye to Jim, and Bob, and everybody else. And that's kind of ominous coming from a computer. And so because salutation was immutable, since it could be mutated, it was mutated. It was an accident, but the fact remains that it was mutated. And therefore, we ended up with an inconsistent or an unpredictable result. So to fix that particular problem, we will just create the salutation variable as a constant using the const keyword. And now salutation cannot be changed, it is immutable. Now that's great for simple values like strings, or numbers, or booleans, but let's talk about an object. Let's create a person object that has a firstName called Jim. So the const keyword is great in that it protects the variable itself, but only the variable. So we can't come back to person and assign it a new value. We can't set it to an empty object, we can't set it to null. We can't do anything with it because it is constant. However, it does not make the object itself constant. So we can come in and still mutate the person object, we can give it a lastName of doe. So to fix this particular problem, there is a method called Object.freeze. This creates an object that is frozen, it cannot be mutated. So now we have a constant variable called person. It contains a frozen object that has a firstName property, and so we cannot add a lastName property later on. We can use object freeze with just about everything, but it only makes sense for objects and arrays. So let's look at an array, I'm going to create one that starts with 0, 1, 2, 3, 4, 5. Let's change this to indexes, the idea here is that the values for these elements also match their indexes. So let's say that we want to write a function that is going to manipulate or change the value of this array. So let's just call it to addElements and it's going to accept an array. Now since this is a frozen object, we cannot use push and add in the next item in the array. In fact, we wouldn't want to do that because we don't want to mutate data. Mutating data is unpredictable, it is inconsistent, and we want to avoid that as much as possible. For one, it would not only manipulate the array that was passed to it. But that manipulation would also affect anything else that that array is being used for. So it could be catastrophic to our entire application. So we want to work with immutable objects, so we don't want to use push. And hopefully the array would be passed as a frozen array. But if not, we want to still stay away from any manipulative methods. So then the question becomes, how do we manipulate this array? How do we add the next item to it? And that's very simple actually, we just create a new array. Now I know that sounds a little over the top because it's a whole lot easier. And it's much more efficient to just manipulate the things inside of the array that we need to. I mean, it's a whole lot simpler to just push in that new item, it's much more efficient. And I feel like I'm a broken record here, but manipulating it is inconsistency, it's introducing unpredictability. So instead we just create a new array and return that. So we would do that like this, we would still use Object.freeze. And we would spread out the array that was given to us, and then we would just add in the next item. So we could call addElement, we could pass in indexes. That would return a new array that has seven items in it. And then we could pass the result of that to another call of addElement. And that would produce another array that had eight elements in it. Now, I fully understand that this looks impractical, and yeah, this particular example is. However, in our project, you will see immutability used in a meaningful way.

Back to the top