Immutable Data

Get familiar with the benefits of immutability in functional programming languages.

We'll cover the following

If we listen to functional programming experts, we’ll hear them make a big deal about immutability, which is the fact that in a functional program, data cannot be altered once created. And, indeed, Elixir enforces immutable data. But why?

Let’s forget about Elixir for a moment and think about our current programming language of choice. Let’s imagine we wrote this:

count = 99
do_something_with(count)
print(count)

We’d expect it to output 99. In fact, we’d be very surprised if it didn’t. We believe that count will always have the value 99. Now, we could of course bind a new value to our variable, but that doesn’t change the fact that the value 99 is still 99.

Imagine programming in a world where we couldn’t rely on that, where some other code, possibly running in parallel with our own, could change the value of 99. In that world, the call to do_something_with might kick off code that runs in the background, passing it the value 99 as an argument. And that could change the contents of the parameter it receives. Suddenly, 99 could be 100.

Get hands-on with 1200+ tech skills courses.