A Practical Road Map to Functional Programming: Part 1 Immutability

In the world of tech and development there’s been a ton of talk about functional programming. It’s certainly the buzzword of the year and rightfully so.

For those interested in building massive systems it has tremendous power. But between the concepts of immutability, composition, monads, type systems and streams it can be a daunting undertaking.

This will be the first in a series of posts on functional programming.Getting you from a total novice, foreign to the world of functional programming towards comfort in implementing it day to day.

Why Functional Matters

But before we dive in I’d like to touch on why functional programming is so important. If you’ve ever done much programming you’ve probably focused on imperative programming giving the computer explicit statements for particular behavior. Basically how something is done, its you telling a list of commands to the computer.

This is probably something similar to what you've done give it an input, tell the system what to do and conditions.

 var l = [1,2,3,4]; 

 for (var i =0;i < l.length;i++){  
   if (list[i] == 1) { 
     console.log(‘start list!’); 
   }
   else { 
     console.log(list[i]);
   }
 }  

This is fine for small applications but imagine trying to create software, for a space station or sequencing DNA you have vasts amount of data with tons of variations. Now using imperative programming has been done before for large systems but it certainly isn’t ideal.

alt (Punch card programming used for initial NASA launch)

Perhaps the biggest problem I’ve seen in large scale systems with imperative programming is side effects or unintended. Instead we should be creating a series of reusable small functions that can be easily interchanged thats the basis of functional programming. In another post I'll focus on the high level of functional programming but this particular post will be about immutability.

Immutability

Immutability is the idea that once a object is set we cannot change that object. Any attempt to do so will either be ignored or create a second object.

This has an immense impact on our application because we instantly have more certainty about state. And if you ever dealt with large scale applications state is critical towards building something meaningful. By knowing whether an object is a certain state or doesn’t exist we worry far less about mutation tracking. Not entirely but the burden is greatly reduced.

The best way to appreciate immutability is to actually understand what it is not, what is known as via negativa.

When you’re dealing with mutable objects, it can be almost impossible to determine what other functions or objects are changing it. This is commonly known as “spooky actions at a distance”. Multiple event listeners can be attached and methods outside your domain could be changing the value. It greatly reduces the code readability as you cannot be certain that the variable name describes the behavior.

The other aspect we see is with concurrency. If multiple threads are trying to access a mutable object we often experience what is known as locking. This makes it extremely hard again to know which thread has access and the changes to the object.

Immutable objects dont have the same issues and change how you interact with objects. As always the best way to get started is to dive right in.

Getting Started

For javascript developers its as simple as adding the immutable.js library to any codebase you’re working with. It’s something that quite quickly you’ll see we’ll clean up your code.

https://facebook.github.io/immutable-js/

Next week we’ll cover composition as we go further into functional programming and touch on more powerful concepts.



comments powered by Disqus