Object Oriented Programming (OOP) & Functional Programming —What are they & the Pros and Cons
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.
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.
— 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 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”.
— 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.
— 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.
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.
Stack Overflow — What is a Race Condition http://stackoverflow.com/questions/34510/what-is-a-race-condition