Object Oriented Programming (OOP) & Functional Programming —What are they & the Pros and Cons

Objected-Oriented Programming (OOP) and Functional Programming (FP) are the two predominant paradigms in JavaScript, often considered the two pillars of JavaScript. Both have their place in the JavaScript programming world, but it’s no stretch of the imagination to say that sometimes the fanbases of these two paradigms collide.

Object-Oriented programming is achieved in JavaScript through the use of prototypal inheritance. OOP utilizes objects without classes, prototype delegation — aka OLOO (objects linking to other objects).

Here’s a simple of using the OOP approach to create a person object with a name property and a greeting property:

Object-Oriented programming works upon the idea of inheritance — assigning your types based on what they are. Here we have a ‘Person’ object which has a name and a greeting. The common problem with the OOP approach is the idea of shared state between different methods which can lead to the issue of race conditions.

A “race condition” exists when parallel code that would access a shared resource could do so in such a way as to cause unexpected results. Race conditions are not encountered very often, but it opens up the possibility of retrieving unexpected results as side effects of this “racing”.

Cases like this open up the need for a paradigm like Functional Programming.

Functional Programming is a paradigm that avoids shared state and mutation as much as possible. The two greatest points of functional programming is about building pure functions with no side effects. The expectation being that you should always expect the same output from the same function, provided the same input.

Functional Programming works upon the concept of higher-order and first-class functions. JavaScript is well-suited for functional programming because of higher-order functions, which is a function that can another function as an argument and even return out a function as result.

In JavaScript, functions are first-class because they can have properties and methods just like any other objects. This is extremely important in making this a comparable opponent to the OOP approach.

Here’s the same person function utilizing the Functional Programming approach:

Functional programming, especially in an example like this, can be viewed as much more verbose than the OOP example. However, generally the functional programming approach stresses simple functions that provide one piece of functionality. Additional and bigger tasks are modularized into multiple simple functions.

Object-Oriented Programming and Functional Programming can be used intertwined throughout your coding and don’t need to be solely heralded as enemies. However, they each have their pros and cons and it’s important as a JavaScript developer to understand those. Let’s dive in!

OOP Pros:

— Objects and methods are very readable and understandable.

— OOP utilizes an imperative style, in which code reads like a straight-forward set of instructions as a computer would read it.

OOP Cons:

— OOP commonly depends upon shareable state. The unfortunate result of so many objects and methods existing within the same state and being accessed in an entirely undetermined order can lead the pre-discussed concept of “race conditions”.

FP Pros:

— Utilizing pure functions, leads to reliable functions with no side effects that accomplish and return exactly what you expect them to.

— FP utilizes a more declarative style, which focuses more on what to do and less about how it’s being done. This places the emphasis on performance and optimization, leaving the door to refactor without completely reworking your code.

FP Cons:

— Functional programming is a newer paradigm. It’s much easier to find documentation and information on the OOP approach.

— Similar to one of OOP’s strengths, functional programming can lack readability at times. Sometimes functions can become very verbose and become difficult to follow comparatively to the object-oriented style.

You saw a simple example of this in the Person object and createPerson function we discussed earlier.

The Conclusion

Too often do we pit paradigms, languages, and other competitive programming concepts against one another. Often times, the best approach is that of the hybrid approach. Utilize Object-Oriented Programming in moments where its strengths can best shines. Utilize Functional Programming in moments where its strengths can best shine as well.

The goal for all developers is to become as well-rounded as possible and be able to not only understand a concept at a high-level, but to also be able to understand the advantages and drawbacks of different approaches and to be able to speak to them.

Happy Coding!


Eric Elliot — https://medium.com/javascript-scene/10-interview-questions-every-javascript-developer-should-know-6fa6bdf5ad95

Eric Elliot — The Two Pillars of JavaScript: https://medium.com/javascript-scene/the-two-pillars-of-javascript-ee6f3281e7f3

Stack Overflow — What is a Race Condition http://stackoverflow.com/questions/34510/what-is-a-race-condition

SitePoint — Higher Order Functions https://www.sitepoint.com/higher-order-functions-javascript/