JavaScript: Functional Programming vs OOP

JavaScript: functional programming vs OOP

Functional programming is a trending approach when it comes to user interface development. Why? Is it because object-oriented programming (OOP) is too complicated, overpriced, and old-school? Let's try to sort out why there's a 'functional programming vs OOP' narrative in the programming community.

In fact, developers often prefer to use a function-oriented programming style rather than OOP. Everyone knows that OOP features encapsulation, inheritance, polymorphism, and SOLID. Of course, with JavaScript, things work a little bit differently. OOP has a prototype inheritance which, for some reason, is not widely discussed or used, and Object has null in its prototype.

Function-oriented programming is an effective technique based on the functional factors required for developing various programs. Unfortunately, developers often master just a couple of functional programming techniques. This is understandable since it is easier to learn three or four function-oriented programming techniques than to deep dive into the depths of OOP.

What's the primary difference between object-oriented programming and functional programming?

The key difference between object-oriented programming and functional programming

To identify what distinguishes functional programming vs OOP, you need to understand the fundamental basis, the smallest unit that each uses. Different disciplines have different fundamental bases. In math, it is numbers; in literature, words; in physics, quarks; in chemistry, elements. The rules, structures, and other complex notions relevant to each are created based on this smallest unit.

In OOP, the fundamental basis is classes, instances of which can be stored in variables. In functional programming, however, there are no variables; there are functions and functions only.

Principles

During candidate interviews, I’ve observed that most candidates from Junior to Lead roles can enlist closures, pure functions, and immutability. I rarely hear about composition, idempotency, finite state machines, and category theory. This is disturbing. OOP is a familiar topic: everyone knows about abstraction, encapsulation, inheritance, polymorphism, and the concepts behind DRY and KISS. Most of the candidates, however, prefer to bypass design patterns and SOLID.

Functional programming becomes salvation from an OOP interview because not everyone has the knowledge or background to discuss it in depth.

To see the difference between functional programming vs OOP, let's examine a common composition from the position of JavaScript, using the example of several calculator operations based on user input. The main task is to eliminate the need to store anything. We have an entry point and an exit point and we don't need anything more:

Below is the same code without unnecessary returns and comments; a similar idea is implemented in Redux, with state machines and that's all:

Other differences between object-oriented programming vs functional programming

In addition to the fundamental distinction between the two, other differences between object-oriented programming and functional programming include:

  • Data usage: Functional programming uses immutable data while OOP uses mutable data.
  • Programming model: Functional programming follows a declarative model while OOP follows an imperative model.
  • Support: Advocates for functional programming adhere to the ideology of pipelines, which conveys the ideas of state machines accurately enough. OOP (true OOP) has tight relations between class elements, which doesn't allow the code to be fully modular.
  • Execution: In functional programming vs. OOP, functions are atomic, and are isolated from the context as much as possible. As a result, you can reuse functions anywhere since they are not linked to modules.
  • Element: The basic elements of functional programming are functions while the basic elements of OOP are objects and methods.
  • Use: Due to its lack of ambiguity, the level of fault tolerance in functional programming is extremely high, so you can achieve reliability approaching 99.9999%.

Final thoughts

Functional programming can be effectively applied in JavaScript, but it should be applied properly, with an understanding of the concept of state machines, for example. Employed this way, programming using Redux, GraphQL, and solutions like Redis / MongoDB might be quite interesting. You can try to completely eliminate the imperative style, leave data transfer and processing to the user, and focus on the business logic instead.

The key difference between object-oriented programming vs functional programming is that object-oriented programming attempts to operate using abstractions, rather than code, so that a developer can use known patterns and master the highest level. Functional programming, on the other hand, pushes us towards what Robert Martin said when refactoring the Date class, that the minimum arity of functions is required (basically, that zero is the optimal number of arguments for a function).

Of course, you can chase math and try to write good REST in Haskell, but it's important to understand that functional programming in the right hands provides predictable fault tolerance and testability under high loads. On the other hand, there are those who advocate for OOP in the OOP vs functional programming debate. Ideally, professionals in both camps should try their hand at JavaScript to see how you can make the paradigms work together in a twisted way.

we'll be happy to share our latest stories with you

subscribe to get the latest tech insights, career growth, and lifestyle tips right in your inbox

By subscribing, you agree to receive marketing emails from EPAM Anywhere and other partners offers and accept our Privacy Policy. You can opt-out at any time.