Clean Architecture – Chapter 5 | Object Oriented Progrmming

What is Object Oriented?

I have always come across that object oriented (OO) is a way to model real world. It was the same definition, while I was in bachelors, same while I was doing masters. The author labels this definition as evasive and doesn’t tell us what OO is. He refers perhaps we accepted this definition because it makes software closer to us as it emulates real world

The other popular definition is “OO is combination of data and functions”. It implies o.f() is different than f(o), which is absurd as programmers have been passing data structures in functions long before OO was discovered. 

Yet another definition is OO is a proper mixture of encapsulation, inheritance and polymorphism. And then author goes on to refute this definition as well, ultimately defining OO by the end of the chapter. Let’s see what argument he makes against all these three.

Encapsulation

OO languages provided easy way to encapsulate both data and functions into one object. But this is not unique to OO languages, C even before OO had perfect encapsulation.  

In C, we used to declare the data structures and functions in header files and then implement them in implementation files. The users of the data structures never has access to the implementations. (we always import header files in other C files, and use data structures and functions from there)

Modern programming languages, removed this distinction between header and implementation and combined them into one, thus diluting encapsulation. So in a way modern OO languages don’t provide any improvement on encapsulation, rather degrade it. So we cannot say OO depends on strong encapsulation.

Inheritance

Inheritance is redeclaration of group of variables and functions within an enclosed scope. In C, it was possible to manually do this. Safe to say, inheritance was there in pre OO languages in some form. Modern OO languages build upon that concept, making it more easy to do.  

Polymorphism

Polymorphism is an application of pointers to functions. In C, C++, there were pointers (referring to location of a resource) that were passed between functions. This kind of polymorphism was manual, and the programmers need to be careful to follow the pointers safely to avoid errors. Although modern programming languages didn’t start polymorphism, they have made its development simple and intuitive.

Dependency Inversion – the real power of OO

Traditionally, in structured programming languages, the flow of dependencies followed the flow of control of program. It has been done using include in C, import in Java, etc.

Polymorphism enables us to control the flow of source code dependencies and keep it separate from the flow of control using dependency inversion.

 

OO is the ability through the use of polymorphism, to gain absolute control over every source code dependency of the system. It allows the architect to create a plugin architecture, in which modules that contain high level policies are independent of modules that contain low level details. These modules can be developed independently of each other.

Robert Martin.

Thanks for stopping by! See you in next post! Hope it was good byte on object oriebted methodoloty. Don’t hesitate to start conversation using comments below.