How do I know if I really need object-oriented programming?

Published on 2021-03-30.

It's easy to determine if you really need the object-oriented paradigm, you just need to look for the things that you can only do with object-oriented programming and then ask yourself whether you really need that. It's called "The Three Pillars of Object-Oriented Programming". If you don't use all three at the same time, then you're not doing object-oriented programming and you don't need it.

The "three pillars" of object-oriented programming are:

The usage of encapsulation alone, i.e., defining and using classes, but making no use of either inheritance or polymorphism, is referred to as "object based programming", it is not truly "object-oriented programming", you need to be using all three of the pillars.

Encapsulation

Object-oriented programming started out as a new technique which allowed data to be divided into separated scopes called "objects". Only specific functions belonging to the same scope could access the same data. This is called encapsulation.

In the beginning objects where not called objects, they where just viewed upon as separate scopes. Later when dependencies were reduced and connections between functions and variables inside these scopes where viewed upon as isolated segments, the result gave birth to the concepts of "objects" and "object-oriented programming".

Inheritance

Inheritance is the mechanism by which one class "inherits" the properties and methods of another class.

This is in contrast to copying and pasting code from one function into another because you need the same basic functionality, yet at the same time you need to expand the second function with further capabilities.

In programming you should avoid repeating yourself. The way it is handled correctly in procedural programming is to split up the functions into smaller pieces and then pass the result of the first function into a variable or an array and then pass that into the new function. You generally want to avoid calling one function from within another.

In object-oriented programming you can do inheritance.

Polymorphism

Polymorphism (Greek for "many forms") refers to the ability in programming to present the same interface for different functionality. Poly means "many" and "morph" means change or form. It's the ability to have multiple functions with the same name, yet with different functionality or implementation. With polymorphism we can have 3 different functions all called printOut, depending on the context one function could output HTML, another JSON, and the last XML.

We use an "interface" when we want to implement polymorphism. An interface is like a blueprint. We define method names and arguments in the interface, but not the contents of the methods. The objects that then executes the interface MUST execute all methods characterized by the interface.

Do you need any of that?

If you don't need the three pillars of object-oriented programming, then there is no reason to roll out an object-oriented setup. On the contrary, you will benefit from not doing so as your code will less complex.

If you are just using e.g. classes without implementing these three features, then you are actually just doing procedural programming, only with classes to encapsulate your functions. That's fine, nothing wrong with that, you just need to know that you're not doing any object-oriented programming and you can stop worrying about it.