Yet another introduction to functional programming
By: Clayton Marshall
Tags: JavaScript, Higher Order Function, Pure Function, Immutability, Functional Programming

If you read the Line by Line introduction posts, you’ll know I’m very interested in a thing called functional programming. Since it will be a recurring topic in my posts, I think it’s only fair that I give my version of an introduction to what functional programming is. So without further ado…

What is Functional Programming?

Wikipedia:

In computer science, functional programming is a programming paradigm- a style of building the structure and elements of computer programs-that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data.

I love Wikipedia as much as the next person, but that definition doesn’t give a great first impression. Let’s break each piece of this definition down into small chunks, and by the end of this post we’ll be able to compose them back together to reach an understanding.

Functional programming or FP for short, is a programming paradigm — meaning it is a style of programming. Consider some other programming paradigms you’re likely familiar with: (procedural, object oriented, etc.). These are just rule sets we use to structure the way we as developers write code and think about the problems we’re solving.

Many languages have been built to enforce some of the important “laws” of functional programming and are designed entirely around the paradigm. These languages are very fun to use and I’d highly encourage you to try a few of them out. That being said, the purpose of this post is not to learn a functional language. We’re going to learn what functional programming is, and how you can use its concepts inside the code you write everyday.

What is our goal with functional programming?

We’ve established that functional programming is a style of writing programs and representing computations. Before going any further though, I think it’s important to outline an overall goal that this style of programming is trying to achieve. It’s hard to get excited about a new tool if you don’t know what its used for. That being said, we have one and only one goal with FP: make programs simpler.

Now, although that idea took three words to explain and sounds pretty trivial, allow me to assure you that making programs simpler is NOT an easy task. In fact, you could argue that people have been working on this problem since computers have existed. Every programming language that has been written likely began with the goal of making our lives as programmers easier. The reason you and I don’t have to specify register addresses when we write a bubble sort is a clear benchmark that shows us programming has gotten simpler as time has progressed. Our brains can now think less about the computer’s inner-workings and spend more time thinking about our program’s process. Much progress has been made in the years before us, but we can still do better. Functional programming provides us with some rules for our code that can provide more simplicity in our programs. These rules can be broken up into three focus points:

Abstracting out common processes

Avoiding mutating state / Isolating state mutation

Writing declarative and composable code

If some of these words or phrases don’t make sense right now that is totally fine. I want to cover each of these in good detail and provide some code examples along the way. Before doing a detailed deep-dive let’s start with a question: how do we “do” functional programming?

One of the more obvious ways you might think to “do” functional programming is to express our program in terms of functions. And that’s actually a great place to start! Writing our programs as a composition of smaller bite-sized functions is a key part of FP. To get a better idea of what that means let’s transform some non-functional code into functional code.

Non-Functional:

Functional:

Nothing too ground-breaking here. We’ve abstracted out a common process in our program into a function which we can reuse. In short, our program has has been expressed in terms of functions instead of statements. Let’s look at a more complex snippet of code and try to apply this same style of thinking.

Non-Functional:

Functional:

One of our focus areas of FP is avoiding mutation. We’ll go into more detail on the benefits of immutability later, but a good rule of thumb for making our programs more functional is to avoid reassignment. In the functional version of the code, we have the exact same logic as before, but we’ve extracted it out to a separate function making it much easier to reason about.

Now that we’ve seen an overview of what functional code looks like, we can get more into the nitty-gritty details. We’ll dive into other techniques for writing functional programs, and more importantly I’ll walk through the benefits of writing our code this way.