Functional Programming
Functional Programming (FP) is a programming paradigm with some particular techniques.
In programming languages, you’ll find purely functional programming languages as well as programming languages that support functional programming techniques.
Haskell, Clojure and Scala are some of the most popular purely functional programming languages.
Popular programming languages that support functional programming techniques are JavaScript, Python, Ruby and many others.
Functional Programming is not a new concept, actually its roots go back o the 1930’s when lamda calculus was born, and has influenced many programming languages.
FP has been gaining a lot of momentum lately, so it’s the perfect time to learn about it.
Why Functional Programming
- Its pure function, provides confidence of not changing things outside of its scope.
- Its reduces the complexity, need not to worry about how it is doing it, focus will be only on what it is doing.
- Ease of testing, because it does not depend on state of the application and result verification also will be easy.
- It makes the code more readable.
- Functional programming makes code easier to understand.
Practices In Javascript Using FP
Array Functions
In the above example, we are trying to filter only active meet-ups and we can see it can be achieved with two different approach. Here, second approach is Functional Programming where filter() method takes care of “how program operates” and program focuses only on input i.e. meetups array and care about output activeMeetupsFP but in first approach program cares about how to operate as well with for loop.
Similarly, below are other Array methods which help to achieve functional programming and reduce the complexity of the code.
- find
- map
- reduce
- every
- some
Function Chaining
It’s a mechanism for invoking multiple method calls, each method returns an object, allowing the calls to be chained together in a single statement without requiring variables to store the intermediate results.
In the above code snippet, we want to print total members of all active meetups, considering 10% members might be duplicate.
No more ‘var’
All my var’s were replaced with const. When my code became immutable and my functions became pure, var disappeared completely.
I remember looking at my code and marveling at not seeing a single var or even let, everything became const. My interest levels started rising.
The ‘for’ Loop
The for loop was one of the first things to go. I started replacing the majority of my for loops with filter, map and reduce. The loops that required something extra were replaced with recursion. Before you start to mutter the word break, read this.
The for loop is now completely extinct in my codebase. If you do happen to stumble across one, point it out so I can kill it.
https://hackernoon.com/rethinking-javascript-death-of-the-for-loop-c431564c84a8
The ‘if’ Statement
The if statement was soon the next thing to go. I stopped writing large blocks of code nested inside an if-else. (this is also good practice to do with OOP). The logic was extracted into functions. After this, it became obvious to convert the if to a simple ternary operator.
https://hackernoon.com/rethinking-javascript-the-if-statement-b158a61cd6cb
RIP ‘switch’
With the if and for gone, the next feature I eliminated was the switch. These weren’t used often, but I wanted a functional alternative.
Summary
Code with FP now looks completely different. It consists of a collection of many pure functions, organized into ES6 modules. I use these functions to compose into more complex functions. A large majority of functions are one-line lambda expressions that immediately return a value.
I now see software inputs as data streams and program reactively on those streams.
My understanding of functional programming has given me more options at my disposal to me to solve common problems.
I also learned that functional programming is inclusive and can be used as much or as little as you need in existing projects today. C#’s LINQ is a great example of functional design in an OO language.
Functional Programming can be beautiful.
References
https://codeburst.io/functional-programming-in-javascript-e57e7e28c0e5
https://flaviocopes.com/javascript-functional-programming
https://hackernoon.com/rethinking-javascript-break-is-the-goto-of-loops-51b27b1c85f8
https://hackernoon.com/rethinking-javascript-death-of-the-for-loop-c431564c84a8