Functional Programming Vs Object Oriented Programming

So you have been trying to pick up coding and now you are already bombarded with hundreds of options: from languages to code editors to the type of programming you wish to study. Or you just finished a bootcamp like myself and want to understand something more fundamental about the programming paradigms out there. One of the topics you will see flung around often is this comparison between Functional Programming and OOP (Object Oriented Programming). If you have been coding in Javascript you are already thinking, like I did, “wait, I thought functions were a type of object and by that definition, isn’t FP (functional programming) basically an OOP? Worry not, because the choice between FP and OOP is something you will make as you become a seasoned developer and you might as well end up including both in your applications depending on your needs. But for now, let’s dive into what they are exactly.

FUNCTIONAL PROGRAMMING (FP)

Functional Programming is basically organizing your logics into functions, whereas OOP organizes its logic within classes and methods. Each function performs a clearly defined task and these functions are the primary building blocks of the programs. That’s why FP promotes and emphasizes MODULARITY (the practice of breaking down big logics into smaller ones so that it can be reused elsewhere, rather than rewriting the same lines of code elsewhere). That is also why we are able to implement declarative style programming in FP (we are deciding to reuse code already written or invoke built-in methods, such as map or reduce in Javascript).

Functional Programming is based on the idea of writing “pure functions”. You will see later that this is a qualified definition. But first, what exactly are pure functions, you ask? In a pure function:

  1. The data and the behavior(logic) of the program are kept separate.
  2. All objects created are immutable, meaning once an object is created/introduced in the program, it cannot be changed or manipulated.
  3. Objects/functions do not share scope with other objects/functions and therefore, there is no shared states. The function does not depend on variables outside of it scope, but depends on inputs passed in as arguments. This simplifies state management because invoking one function will not affect the state of other components of a program or mutate values in the parents scope.
  4. There is no side effect (this is a fundamental concept in FP), meaning for any given input, the function should always return the same output regardless of where or how often it is invoked in your application/program. We want this feature because we want the functions to be predictable in their role, so that we can rely on these functions when we reuse them.

The goal of Functional Programming is to limit the number of the side effects (described above) we introduce to our program, and to make sure that you only have side effects when they are meaningful and/or necessary. There is no escape from these side effects, and in some cases we need these side effects for our program to run efficiently and as desired.

In Functional Programming, since it is declarative in nature, instead of writing down every single step of your logic needed to complete a task, you can tell your function what to do with a high level code (for e.g built-in methods). This way FP codes are more direct and easier to read and write. Some will even make the case that it reduces the chances of making silly errors.

OBJECT ORIENTED PROGRAMMING (OOP)

Object Oriented Programming (OOP) is another paradigm of programming or a way of organizing your program. It organizes data and logics in objects (data as properties and logic as methods). These data and logics represent the things you are dealing with and the functions you want to carry out on them. In that sense, it is a real-life simulation sort of programming and this is what, according to OOP proponents, makes OOPS intuitive.

Features of Objet Oriented Programming:

  1. Objects are instances of a class, and are seen as individual entities which interact with each other. These objects mimic the real world to a certain degree.
  2. OOP encapsulates data from outsiders: it hides the variables within the class (object constructor) from outside access, making a program secure.
  3. OOPs are not as reusable as Functional Programming. Because some of your functions depend on the class that is using them, it is hard to use some functions with another class.
  4. Imperative programming (where you tell your program to perform a certain function with meticulous step by step instructions) is used in OOP. It is just like following a recipe. Imperative languages have the benefit of having implicit state, again mirroring the real-life examples of state among humans and machines dependent on time.

The decision over which paradigm to pursue is a very personal one and it will evolve over time. For some people, OOP’s ability to model after real world scenario is very attractive, but for some, PF’s ability to run functions irrespective of its order of appearance in a program is the deciding factor. For a lot of the beginners, it is very difficult to even understand the difference because you have been exposed to only one kind. However, in the immediate future, when you become more conscious about the efficiency and styling of your coding and approach to problem solving, consider the following pointers to pick one over the other:

  1. OOP languages are good when you have a fixed number of operations to run on your things. Whereas, FP languages are good when you have a fixed number of things to perform your operations on.
  2. OOP proponents argue that the concept of inheritance (new objects taking on the attributes/methods of existing objects letting us to reuse code more) and encapsulation make it easier to to manage and manipulate data. Whereas, FP bandwagon will have you believe that the separation of data and methods, and abstraction of operations leave less room for errors.
  3. Other differences such as the ability to use global variables and access to recursion method for iterating over data in FP can also be considered.

At the risk of sounding like a broken record, the choice between these two paradigms is something you would worry about as you start caring about the efficiency and organization of your code. For a lot of us beginners, we are happy just by the fact that it is working. Majority of the developers incorporate both paradigms in their program, so rarely will you find someone who exclusively implements one and completely neglects the other. As you become more adept and evolve into a developer who has to worry about more things than just making a code work, you will definitely make your choices and I hope you will remember this poor man’s attempt at explaining the difference.