In this tutorial, you will gain an understanding of what functional programming is and how you can use some functional techniques to help you write more concise and easier to understand JavaScript code. Functional programming is not a new concept and it has been around a long time. Functional programming has been around since the 1950s, when the first functional programming language, LISP was released. Functional programming can not be used with every programming language. The goods news is that some of it can be used with JavaScript! The emphasis of that statement is on 'the some of it'. Pure functional programming can cause sweats of fear down some developers spines. The idea behind using functional techniques with JavaScript is not to be completely pure, instead, the aim is to write easier more understandable code. If you apply a functional technique and it does not improve understanding, there is little point in religiously following it!
Is Javascript A Functional Programming Language
Javascript is not a fully functional programming language, like Haskell. The reason why functional programming can be used with Javascript is that Javascript treats functions as first-class citizens. To be clear, when I say 'functions are first-class citizens' this means that functions work the same way as variables. This means functions can be assigned to variables, passed into functions as an argument and can also be returned as the result of a function.
As Javascript treats functions this way, there is a whole new world of design paradigms and tools you can add to your coding toolbox. Examples of some of these new patterns include higher-order functions, currying, pipelining and composing. A lot of popular frameworks like Redux, JQuery and Lodash all use functional programming techniques, so a lot of it might not be that new to you.
What Is Functional Programming?
In simple terms, functional programming is all about writing very small and concise functions that do one thing well, and then chaining these functions together to do more powerful things. This is a completely different way of coding to non-functional languages. In a non-functional approach, you may use tools like conditional logic, inheritance, objects, and classes to build up the structure of your app. For a simple example, if you had a function that contained a massive if/else statement, in a functional approach, you would refactor it, remove the conditional statements and then build one or more functions, that can be chained together to achieve the same logic.
When it comes to functional programming in Javascript, it is definitely not advised to use it as an all or nothing technique. Using pure functional techniques in some instances can result in more complicated and harder to understand code. In other situations, it can make your code more declarative (easier to understand).
When it comes to Javascript, to gain a more in-depth understanding about how to architecture your code, you need to know about the different functional programming and you also need to learn in which situations you should apply them. Functional programming is useful to make things easier to understand. Just like how the name of a unit test describes what some code does, in a functional world, the name of the function gives added context as to its intention. An example of a functional style is shown below:
Another way of writing this code is shown below:
This second code snippet is not as good as the first example. It is not reusable for starters. It also reads as code rather than a sentence. Readability is one of the main benefits of a functional style. Write code that reads like a sentence. If you look at this and think Lambda, you have used a functional technique!
This in essence is what a functional style is. Writing code to use functions rather than conditional statements. How you apply functional styles will take a lot of practice, however, this is the main concept to understand. The name functional style gives its purpose away, it's a focus on functions. Next time you are in a meeting and someone mentions this technique I now hope you feel less stupid as you now know what the conversation is about. Happy Coding 🤘