Functional Programming Vs Object Oriented Programming

If you search in google, for difference between Functional (FP ) and object oriented programming (OOP), you will get tons and tons of information. I did the same to understand the difference as an Novice functional programmer. As you all do I too did a blunder, I went searching for materials from my favorite authors on Software development and one such person was Michael Feathers. I loved his books, and I use them as my bible to refer to for some of my situations, Unfortunately I landed with the complicated statement of Michael Feathers, on Functional Programming.

Object-oriented programming makes code understandable by encapsulating moving parts. Functional programming makes code understandable by minimizing moving parts.

Trust me, this statement looks so simple but if you deeply try to understand what does Mr Feathers mean, it is somewhat quite complicated. After spending hours of reading and experimenting I figured out what exactly Mr Michael Feathers mean by this statement.

Let me decipher this statement,

It mentions MOVING PARTS, for the god sake I did not get what exactly it means. In my opinion all of us who are object oriented paradigm practitioners, might have applied functional paradigm concepts one way or the other unknowingly.

For eg … all of us might have done this during our university days.

class Factorial {

    static int factorial( int n ) {
        if (n != 0)  // termination condition
            return n * factorial(n-1); // recursive call
        else
            return 1;
    }

    public static void main(String[] args) {
        int number = 6, result;
        result = factorial(number);
        System.out.println(number + " factorial = " + result);
    }
}

In the above code function factorial is a recursive function, and it is somewhat special by nature, Not because it is just recursive, it is super special because it is Pure by nature.

A function can be considered pure when it returns a value that is computed using only the arguments passed to it. A pure function avoids mutating its arguments or any other external variables. As a result, a pure function always returns the same value given the same arguments, independently of when it is invoked.

Now, it make sense to call Factorial method as a Pure function, because it returns a value that is computed using only the arguments passed to it. it does not mutate its arguments the one returned is different. At any given point of time for a particular value this method always gives me the same result.

You know the moving part Mr Michael Feathers mean in his quote is nothing but the value ‘n’ in the factorial function.

That is the key to the black box :) .

In OOP, we use encapsulation to prevent objects from being aware of the state mutations of other objects. In functional programming, we try to avoid dealing with state mutations instead of encapsulating them. Classes encapsulate state properties that are then modified by methods, which encourages methods to be stateful and not pure.

Both functional and object oriented paradigms has its own pillars,

PILLARS OF OOP PILLARS OF FP

So we now know what exactly functional programming is, and we now know we have done some in the past unknowingly but what is the value add ?

As programmers what we regularly do is figure out how things can be combined to build bigger things. That’s what we programmers understand really well.

Every day in our life at work what we recursively do is. We decompose a problem into smaller problems than we create teams of programmers, and then we give these chunks to programmers to work on so that we could decompose into smaller pieces so that one person can take a smaller piece and work on it.

Things cannot run, by making all developers to gather somewhere and shout about solutions to the problem, we have to isolate ourselves and then focus on our little piece of work and program it.

While programming, we divide stuff into files, then subdivided in to classes and then into functions then we create data structures and then in the end we compose the little solutions like them into bigger solutions.

So all we do is decompose the problem and re-compose the solution…..

In Functional programming paradigm functions are first class citizens. We say that a function is a first-class citizen when it can do everything that a variable can do, which means that functions can be passed to other functions as an argument.

The moment I create a function, one of the most beautiful feature of the programming construct called function irrespective of the paradigm is it lets you forget its implementation and makes you think about where and all you can apply that function. It helps you relate and makes you refactor. If a paradigm gives you concepts and methods to achieve this easily why don’t we use it. By using FP, it makes your life easy by thinking only about functions because functions are the first class citizens that in-turn helps you de-compose the problem and re-compose the solutions better.