Functional programming is something that existed for much longer than object oriented, dating back to the ancient days of Turing machines. It has seen ebbs and flows in popularity over time, based on whatever programming language and its foundational paradigms have taken over as part of the developer’s toolkit. For a long time, object oriented thinking has dominated the community. It’s one of the first things taught in Computer Science degrees and most talked about when it comes to learning programming.

Everywhere you go, everything seems to be an object — except that’s not always the case.

With JavaScript rising as the dominant language of the web, functional programming is making a comeback. To an extent, JavaScript is a paradigm agnostic language, which means that it’s not super glued to a specific or certain way of coding or thinking. It is also one of the reasons why it is considered the easiest programming languages to learn. You can ascribe any structure or methods of thinking to it and it will still work.

So this brings me back, what’s the big deal with functional programming?

They’re all just patterns

At its core, programming is the process of clarifying business requirements into clear and concise patterns for machines to understand and perform. It is also a communication tool between developers in a common language that transcends local dialects. Someone who speaks German should, in theory, be able to read code written by someone living in Russia.

Patterns facilitate this communication process and are a way to think, classify and categorize everything. Object oriented is often the one that is taught and spoken about the most. Functional programming often takes a sideline and doesn’t really come into most people’s attention until much later on.

Object oriented, functional and procedural programming are all just different patterns of writing code. Some programming languages are friendlier towards a particular paradigm, but it doesn’t make the others less valuable to our programming toolkits.

Patterns solve a specific set of problems and over time, we’ve come to realize and accept that not everything is an object and there may be better ways of writing code.

Understanding declarative patterns

There are two ways to program — imperatively and declaratively. Imperative programming focuses on describing how the program works. It is essentially a list of commands for computers to perform. It is a series of statements that is executed in turn. You can’t skip a step or else things go a bit haywire.

Check initial state of door.
If door is closed, reach out to door handle and turn. 
Remember new state, otherwise continue. 
Walk through doorway. 
Close door. 

In the example above, the process must be completed in the exact order or else disaster may ensue.

Declarative is a little flexible in that it works by telling the program what it should accomplish without specifying how the program is to achieve it. Functional programming is a subcategory of the declarative style with instructions that can run in any order without breaking the program.

Algebra rules are a good example of declarative programming provided that the base rules of calculating are preserved.

1 + 2 + 3 + 4 + 5 = 15
2 + 4 + 5 + 3 + 1 = 15
(3 x 5 x 8) + 12 - 5 + (2 x 5) = 137
(5 x 2) - 5 + 12 + (5 x 3 x 8) = 137

Another way to understand declarative programming is that it exists to solve a particular problem by transform the input into an expected output, regardless of what that input may be and how that input is processed.

Problem: I want to change this creature (input) chicken into a (output) cow.

Apply 4 legs
Lengthen legs
Remove outer coat and replace with white cow hide
Give creature black spots
Increase creature size
Change noise to 'moo'
Modify creature face and replace with cow snout

Goodbye state!

In JavaScript functional programming, state is not part of the equation and therefore reduces a layer of complexity by focusing on only what’s its given and how to process it.

When testing, the results only depends on the values given rather than a process flow or state of the application. There is a distinct disconnect from states — a sort of isolated island factory that produces items based on input.

Lets take a quick look at the code below.

let ticketSales = [
   {name:'Twenty One Pilots', isActive: true, tickets:430}, 
   {name:'The Wiggles Reunion', isActive: true, tickets:257},
   {name:'Elton John', isActive: false, tickets:670}
/*using imperative*/
let activeConcerts = [];
for (let i = 0; i < ticketSales.length; i++){
    let t = ticketSales[i]; 

When using an imperative pattern, there is an exact procedure that the program needs to take in order to produce the correct output. The language of the code focuses on describing how everything works rather than what it’s supposed to accomplish. A state is required to work and if i is somehow modified and falls out of range, then there’s a high chance of an incorrect output.

/*using declarative style of coding using the same data*/
let activeConcerts = [];
activeConcerts = (ticketSales.filter((t)=>{
   return t.isActive;

Both examples uses the same data set but processes it very differently from each other.There are multiple Array methods in JavaScript that enables functional programming (find, map, reduce, every, some) possible and therefore reduce the overall complexity of the code.

Final words

While this only covers the basics and basis of functional programming, it is not the final and definitive guide.

When learning FP, a lot of people get hit with the technicalities straight away rather than the ideology behind it. Functional programming is a pattern and way to write code that is not tied to a set procedure that can cause errors if something blips out. Once understood properly, functional patterns can be applied inside object oriented patterns.

Topics in functional programming that needs further discussion includes and is not limited to are immutability, observation, referential transparency, first-class entities, higher order functions, filters, map and reduce are a few of the many things.

At the end of the day, everything in programming is a pattern. Extending your knowledge beyond a single paradigm gives you more tools to work with when it comes to solving problems in an efficient and effective manner.

Share this post