Things to keep in mind when doing Functional Programming

·

3 min read

So you have started with web development and were working your way through Javascript when you came across Functional Programming. You might be saying, "But, we were only taught Imperative and Object-Oriented programming in college. What is this new thing?". Let us understand that and walk through some of the pointers to keep in mind while doing Functional Programming.

What is Functional Programming?

In simple terms, Functional Programming is a programming paradigm where functions are the most important unit. More formally,

Functional Programming is the use of Pure Functions to create extremely explicit and easily testable code. By avoiding sharing/mutating state and reducing side-effects FP lowers the cognitive burden of adding functionality to a growing codebase. blog.imnick.dev/what-you-need-to-know-about..

The following blog will be an elaboration of the definition above. Let's now look at the things we need to keep in mind while doing Functional Programming.

Use pure functions

Pure functions always return the same result for the same argument and do not cause any side effects like changing the arguments or changing any global variable.

//Impure function
var name = "Mr. Stark"
function greet(){
console.log("Hello" + name)
}
//Pure function
function greet(name){
console.log("Hello" + name)
}

Use Higher-Order Functions

Higher Order functions are functions that take functions as input or return a function as output.

function addAdjectives(adjectives){
return function(name){
return "Presenting" + adjectives + " " + name;
}
var introducer = addAdjective("Billionaire, Playboy, Philanthropist");
introducer("Tony Stark")
// result : Presenting Billionaire, Playboy, Philanthropist Tony Stark

Using Map, Filter, Reduce for Iteration

In Functional Programming, it is important to use Higher Order functions like Map, Filter, Reduce

//Example of Map
const array= [1, 4, 9, 16];
// pass a function to map
const newArray= array.map(x => x * 5);
console.log(newArray);
// expected output: Array [5, 20, 45, 80]


//Example of Filter
const infinityStones = ['space', 'soul', 'reality', 'time', 'power', 'mind'];
const result = infinityStones.filter(infinityStone => infinityStone.length > 4);
console.log(result);
// expected output:  Array ["space", "reality", "power"]


//Example of Reduce
const array = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
// 1 + 2 + 3 + 4
console.log(array.reduce(reducer));
// expected output: 10

// 5 + 1 + 2 + 3 + 4
console.log(array.reduce(reducer, 5));
// expected output: 15

The reason we use Higher Order functions like Map, Filter, Reduce for iteration is to avoid mutation of data, which brings me to my last point

Use Immutable Data

In Functional Programming, always keep your data immutable. This avoids a lot of unwanted bugs in your code.

//Mutating the original array : Avoid This
var guardians = ["Star-Lord", "Groot","Rocket","Drax","Nebula","Gamora"];
guardians[4] = "Bad Nebula";
console.log(guardians);
// expected output:  Array ["Star-Lord", "Groot","Rocket","Drax", "Bad Nebula","Gamora"];

In the above example, the original array is getting mutated. This might introduce unwanted bugs in your code.

A better way to do this is by using a map function

var guardians = ["Star-Lord", "Groot","Rocket","Drax","Bad Gamora","Nebula"]
var betterGuardians = guardians.map((item) => (item === "Bad Gamora")?"Gamora":item)

console.log(guardians)
// ["Star-Lord", "Groot", "Rocket", "Drax", "Bad Gamora", "Nebula"]
console.log(betterGuardians)
//["Star-Lord", "Groot", "Rocket", "Drax", "Gamora", "Nebula"]

Conclusion

As you get more experience in web development you would realize how much functional programming is used all the time. And keeping these things in mind, you can start getting deeper into the world of functional programming. Text Thank you for reading the blog. Do drop your feedback in the comments. Share it with your dev friend if you liked reading it. I write blogs documenting the things I learn and the insights I get from them.

You can follow me on Twitter and LinkedIn