OOP vs Functional

February 18, 2024

Overview

Both Object-Oriented Programming and Functional Programming have been discussed in other blog posts:

OOP and FP are very different paradigms. Their primary use cases have little overlap:

  • Object-Oriented Programming:
    • Systems with complex relationships and hierarchies.
    • Systems that require modeling real-world entities, given that the real world naturally organizes itself into categories and hierarchies.
  • Functional Programming:
    • Data processing systems.
    • Complex systems where reliability and predictability are critical.

For a deeper comparison, continue reading.

Table of Contents

Comparison
Object-Oriented Programming
Functional Programming
Conclusion

Comparison
^

Object-Oriented ProgrammingFunctional Programming
Core ConceptObjects represent things with attributes and behaviors.Data is run through computations and can be parallelized.
Data HandlingData and behavior are encapsulated together within objects.Data is immutable. Functions work on this data and produce new data.
Functionality SharingInheritance, composition, and polymorphism.Higher-order functions and function composition.
ConcurrencyRequires extra care due to mutable state.Easy to manage due to immutability.
DisadvantagesCan result in overly complex hierarchies that are tightly coupled.Immutability can result in lower performance in inadequately sized systems due to the creation of more objects.

Table template created by ChatGPT of OpenAI.

Object-Oriented Programming
^

The potential pitfalls of Object-Oriented Programming are explained in What is OOP?

Most use cases do suit OOP. Most scenarios that require an application can be modeled in objects that take some hierarchy. A common example is the animal kingdom. Some animals fly, while others swim, or run. The Strategy pattern is one Object-Oriented design pattern often used to abstract these differences.

The other important characteristic of OOP is the encapsulation of data and behavior. This also models the real world very well. If you imagine yourself modeled in a simulation, you would be an object. All of your thoughts, ideas, beliefs, and aspirations would be data. And any action that you're able to perform, like speaking or running would be behaviors.

Note: When examining the Strategy pattern example in the Wikipedia link within this heading, focus on the setBrakeBehavior functionality, rather than the identical Car type being used for sedanCar and suvCar.

Functional Programming
^

Functional Programming can technically be used in any use case where Object-Oriented Programming is used, however, its tenet of maintaining immutability is most useful in complex data processing. In such cases, updating complex code is easier when you know that all the state changes of a function are contained within it. It also makes code easier to test, because each function is capable of operating in complete isolation. Lastly, this isolation makes code easier to parallelize because there are fewer risks of race conditions and deadlocks.

More nuances can be found in Functional vs Procedural.

Conclusion
^

Object-Oriented Programming and Functional Programming have very different use cases for the most part. One can be used in place of the other in most situations, but in the vast majority of situations, one is far better suited than the other. In some data processing cases where the data and its permutations take a very hierarchical structure, both Object-Oriented and Functional can be used together. In such a case, the data and how it should be processed is modeled in an Object-Oriented hierarchy, but the functions that handle its processing are entirely Functional. Furthermore, given the rise of Functional features, even in Object-Oriented languages like Java, elements of the paradigm can be seen casually in Object-Oriented projects. However, those projects are still considered to exist under the Object-Oriented paradigm, and the data-processing projects are still considered to exist under the Functional paradigm.

Updated: 2024-02-23