Functional Programming Programming

The path to less headache – Functional Programming | What it is?

September 17, 2018
Functional Programming Featured photo

Introduction

In this series, I’ll be discussing functional programming, which has become a buzzword for a couple of years. My source of learning was different blogs, book and also sometimes video. Despite there is a lot of content out there, I find that not all of them are friendly for people who are at the beginner or intermediate stages. I might also add that a lot of people suggested that functional programming in JS is for people who are at least in the intermediate stage of their JS learning. Some might say that it’s an overstatement, I found that you should be at least comfortable with JS, especially with ES6 features. And when I say JS (JavaScript) I meant only JavaScript, not JQuery or Typescript or (…insert your imagination…)

At the end of this series, I hope that we’ll be able to see our programming problems from a different perspective. By the way, I should add that, functional programming helps you to see problems in a different way, it’s not a new paradigm nor it will grant you access to become a superman. Perhaps a good well-balanced coder who writes code like a poet that makes people feel awesome.

Function:

So in functional programming, we first need to know what is a function? Some might think, it is a very basic question. A function is a piece of code that can be run multiple times. Despite the definition is true, in terms of FP (Functional Programming), a function takes one or more inputs and always has a return value.

As you can see I wrote the “always” word in bold because that part is the most crucial knowledge in FP.  If the function is not returning anything then it’s called procedure, not function. In functional programming, we should always write functions and avoid procedures as much as possible.

First Class Function:

Many of you might have heard, functions in JS are called first-class functions. Until very recently I didn’t understand what it means actually. I first understood it when I was reading a book called “Mostly adequate guide to functional programming” by Dr. Frisby. “First class” means it’s like a normal class we use every day in OOP language. As we can create many objects of a class and store them in arrays or list, likewise in JS functions are like other data types which can be assigned to variables or store it in arrays or can be passed around function parameters. In FP we need to see them as normal data type instead of ‘functions’. If we make this thinking as our habit then it would be a lot easier to understand FP.

Now we can write this same code like this:

As greet is actually calling foo() then we can write:

So we can see that the function foo can be passed as a variable. But curious mind wants to know why do I need to pass functions as variable as it is just 1 or 2 lines of code inside a function. Maybe, in this case, it’s just a small function that contains only 2 lines of code but what if you have a function that has 327 lines of code!!? We should always avoid that type functions that do a lot of things! Each function should have only one responsibility. By writing small and single responsibility based functions we will be able to reuse functions as much as possible. If you see that your function contains 327 lines of code then 90% time you will find that your function is taking a lot of responsibilities. Take each segment out of the function and create a small function. why? Let me show you a dummy code segment.

Now take a look at this code

The first code segment is written in an imperative way. Like first do this, then do that and after that if possible do that otherwise do something else etc. Now whenever a new coder or even you look at this code after a few days later you’ll most likely forget what it is actually doing! so you will need to read the whole function to understand what it is doing. But if you write code in the second way which contains way less code than the previous one then a new coder will be able to understand it easily. Because in the 2nd code segment all the complexity all wrapped up into multiple functions and all the function’s name are self-explanatory about what it is doing.

This way we will be able to understand code much faster and implement a new feature or update a feature much easier. Even if you are not convinced then think about this. Let’s say you have written code in an imperative way(1st way) then suddenly you Project Manager says that the DB connection needs to be changed, then it would take a lot of time and energy to refactor the DB connection from all the parts of the code. But in a functional way, you just need to change the only one function that provides the DB connections to other functions.

 


This is it for today. In the next post, I’m going to talk about Pure Functions. What it is and why it is important! If you liked this post, share it with your friends, it’s free!! If you’ve any questions feel free to comment. I’ll try my best to answer all your concerns.

You Might Also Like

No Comments

Leave a Reply