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.
In general the term "encapsulation" refers to the placing data and functions within a class. You "encapsulate", i.e. wrap, the data and the functions.
The result is that you have implemented the conceptual notion of "an abstract data type" and "data hiding".
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 (Greek for "many forms") refers to the ability where methods from various classes do similar things and therefore should have similar names. Polymorphism is essentially a solution that enables numerous classes with different functionalities to execute or share a common interface.
We use an "interface" when we want to implement polymorphism. An interface is similar to a class except that it cannot contain code. An interface is like a blueprint for other classes. We define method names and arguments in the interface class, but not the contents of the methods. The classes 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.
If you are just using 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 object oriented programming and you can stop worrying about it.