This is a one part series of more to come on the subject functional programming. JavaScript is a multi-paradigm language that allows you to freely mix and match object-oriented, procedural, and functional paradigms. Recently there has been a growing trend toward functional programming. Now, what exactly is this functional programming that I keep mentioning?
Functional programming is a programming paradigm that involves thinking of software development and construction based on pure function as its fundamentals and defining principles.
Functional programming is more declarative than imperative. Let me dumb this down for you before you lose interest in this article. Declarative programming involves writing of code that describes what you need to be executed but not necessarily how you want it to be executed. The difference with imperative programming is that with imperative programming, you will highlight the steps that the compiler will follow in achieving what it is that you want to be done.
Want to see a code example? Terrific. Here you go.
Let's start with this collection.
collection = new List <int> { 1, 2, 3, 4, 5 };
Now study this code.
var results = collection.Where( num => num % 2 != 0);
Here, we are trying to find the odd numbers in the already listed collection above. We check for the remainder (using modulus) of the integer in the collection and if it is not 0, the integer is an odd number.
What do you think the code above is. Imperative or declarative?
We did not write a step by step instruction on how to achieve the even/ odd number but we briefly described what we wanted our code to do thus this is a declarative type of programming.
On the other hand, take a look at an imperative type of code solving the same issue.
List<int> results = new List<int>();
foreach(var num in collection)
{
if (num % 2 != 0)
results.Add(num);
}
The difference is now clear isn't it? No? okay.
In the second code snippet, we are explicitly saying that the code should:
Create a result collection.
Iterate through each number in the collection.
Check the number, if it's odd, add it to the results.
This is a great example of imperative method of programming.
Functional code tends to be more concise, predictable, and easier to test than imperative code — but if you’re unfamiliar with it and the common patterns associated with it, functional code can also seem a lot more dense, and the related literature can be impenetrable to newcomers - like me.
What functional code isn't.
Now that you have a vague idea of what functional programming is, let see what it is not. As a matter of fact, these are some of the JavaScript language constructs and ideas you should throw out of the window if you want to learn about functional programming.
Loops. (while, do...while, for, for...of, for...in)
Variable declarations with var or let
Array mutator methods. (reverse, shift, sort, splice)
Void functions.
and others that we will get to learn as we move along and also through your own research dear reader. (Dang!! Do i have t do everything for you?)
Functional programming building blocks.
Here is a list of vocabularies that you will need to learn in order to start grasping and understanding functional programming.
Pure functions.
Function composition.
Avoid side effects.
Avoid mutating state.
Avoid shared state.
We will cover these in the next article.