Don’t Fear the Monad

Functional Programming is receiving a huge boost in popularity with the booming popularity of React and Redux in JavaScript land. I see one very common sticking point amongst all would-be functional programmers, and that is of course the terminology.

If you are a JavaScript Developer and are interested in Functional Programming, but you are off put by the confusing terminology, I am here to tell you that you don’t need to be. Most of the terminology is in fact scary and implies that you need to be a mathematician in order to understand it. Just like you don’t need to be a computer scientist in order to code, you do not need to be a mathematician to reap the benefits of FP.

By adopting two simple principles in your code today, you can reap 90% of the benefits of FP without ever having to learn what a Monad or a Functor is.

Purity

A pure function by virtue always returns the same output given the same input. The big takeaway here is not to use global state! Your functions need not rely on their outside environment.

What this means is that if you are using global variables, you are doing it wrong. The litmus test is whether you can remove the file from your code and run it in another application with minimal setup. In my React projects, all of my UI components are organized in their own modules. Each can be published to NPM on its own and often after I finish a project, I will go back and remove the best components. I put them aside for a later use.

The reason I am able to acheive such a high level of modularity is because I impose a few simple rules, which keep my modules decoupled and infinitely composable.

Immutable Data Structures

I suppose by virtue, I am breaking the rule here by giving you complex terminology. What I will tell you is that without fully understanding the implementation details of immutable data structures, you can implement them into your workflow. There are libraries, such as ImmutableJS that bridge the gap.

I will tell you the best way to start using Immutable data structures today is just to get in the habit of not mutating your data! With ES6, using const is often the go to approach. In many ways, by using the const keyword, you will get into the habit of not reassigning your variables. Although a const is not immutable, being that you can mutate the properties of an object defined with const, using it will help you to get used to the idea of how to work with immutability.

Summing it up

Everyone can benefit from the lessons of Functional Programming. We live in a very exciting time, being able to see cutting edge techniques make their way from academia to the mainstream. By making two small changes to your programming style, you can reap many of the benefits of Functional Programming, without having to learn all of the terminology. Keep your functions pure and your data immutable.

Beyond the benefits enumerated here, I do also suggest that you take a deeper dive into the world of Functional Programming and FRP. Elm, for example, is a magical little purely functional language that compiles into JavaScript.

There is no shortage of purely functional languages and resources to learn them. You can pick up Haskell, Elixir, Scala or Clojure through a multitude of resources available for free online.

Show your support

Clapping shows how much you appreciated Ryan C. Collins’s story.