Functional Programming and Javascript


Though there are many languages for many task there are only three possible paradigms of programming

Structured, Object-Oriented and Functional

Each of the paradigms poses some sort of restriction on how to code. For example, while structured programming restricts the use of ‘goto’ statements, object-oriented programming restricts the way we call the functions. This article is not particularly about the restrictions posed by structured programming or object-oriented programming. Rather, this article aims to discuss what restrictions are posed by functional programming paradigm and how these restrictions are maintained in a Javascript code.

What is functional programming?

Functional programming (often abbreviated FP) is the process of building software by composing pure functions, avoiding shared state, mutable data, and side-effects. Functional programming is declarative rather than imperative, and application state flows through pure functions. Contrast with object-oriented programming, where application state is usually shared and colocated with methods in objects.

In order to understand these programming paradigm, let's take a look at some Javascript code that tries to solve the problem of calculating squares from an array of number.

We can solve this problem very easily using a for loop using structural programming

let arr = [1, 2, 3, 4, 5];

for(let i = 0 ; i < arr.length; i++)
    arr[i] = arr[i]*arr[i];

console.log(arr);
Or, we can create an object and use that that object for code reusability.
class squaringClass{
    constructor(arr)
    {
        this.arr = arr;
    }

    square()
    {
        for(var i = 0 ; i < arr.length; i++)
            this.arr[i] = this.arr[i]*this.arr[i];
        return this.arr;
    }
}

let arr = [1, 2, 3, 4, 5];
let squaringObject = new squaringClass(arr);
let squaredArr = squaringObject.square();

console.log(squaredArr);

The first idea of functional programming is to divide our program into small and testable functions similar to how we construct classes in object-oriented programming. One quick glance and we can refactor our previous codeblocks to include a function that takes a number as input and returns the square for us as the output.

let arr = [1, 2, 3, 4, 5];

function square(x)
{
    return x * x;
}

for(let i = 0 ; i < arr.length; i++) 
    arr[i]=square(arr[i]);

It looks as if we have decomposed the program to include as much function as possible. So, can we say that this program is following the functional programming paradigm fully ?
The answer is 'No'.

The restriction posed by functional programming is in using the assignment operator. In another term, we don't change the assigned values of variables directly in a pure functional program.

So, how can we achieve our goal of squaring all the number of arrays without actually modifying the values? Turns out javascript has lots of function that can help with that. Here, we will use map function and apply the previously defined square function to each element of array individually. The built-in map function does not modify the values inside the array directly but rather creates a new array altogether. This all relates to the concept of immutability in javascript and we can discuss it in depth in a future article.

function square(x)
{
    return x * x;
}

let arr = [1, 2, 3, 4, 5];

console.log(arr.map(item => square(item)));

console.log(arr);

In summary, we took a look at functional programming paradigm and how it differs from more common paradigms like structural programming and object-oriented programming. Functional programming paradigm is becoming very popular because of it's use in libraries like React, where mutating states are discouraged. Moreover, the properties of functional programming help us to avoid deadlocks. This helps the programs to be more thread safe. So, we can write more reliable programs by using this paradigm properly and we will discuss this in depth in future articles.