OOP vs Generics : “Is” vs “Has” approach.

As Bjarne Stroustrup points out, “C++ is a multi-paradigmed language.” It supports many different styles of programs, or paradigms, and object-oriented programming is only one of these. Some of the others are structured programming and generic programming.

And as Thomas Becker explains in this interesting article, there’s a tension between the generic programming and OOP. And here’s from the article the opinion of Alexander Stepanov and elder statesman about OOP: 

Let us start with a little trivia quiz. Who said the following things about object-oriented programming?

"I find OOP technically unsound."

"I find OOP philosophically unsound."

"I find OOP methodologically wrong."

"I have yet to see an interesting piece of code that comes from these OO people."

"I think that object orientedness is almost as much of a hoax as artificial intelligence."

All the quotes above are from an interview with Alexander Stepanov, the inventor of the STL and elder statesman of generic programming.

To have a concrete idea about the generics flexibility, let’s compare the implementation of a class calculating a tax in OOP and generic programming.

Let’s discover a major difference between the two methodologies which could explain the opinion of Alexander Stepanov about the OOP approach.  For that let’s take as example this basic tax calculator

generics1

The CTaxCalculator collaborate only with classes of ICalculator kind. Indeed, we have to inherit from ICalculator and override some virtual methods to implement our algorithm.

On the other side here’s an example of the  generic TaxCalculator

generics2

The CGenericTaxCalculator class does not collaborate only with a specific class kind but with any type capable to calculate the tax,  it ’s not aware of its kind.

The OOP approach is more an “Is oriented approach“, the inheritance is overused and in almost all the OOP code each class A collaborate with another class B if B is a kind of another class, abstract or not.

On the other side, for the generic approach a class A collaborate with B if B has some specific methods and fields, no need to inherit from a specific class.

This makes the generic programming more natural and flexible, it gives to the developer the possibility to adopt a “has oriented approach“, the OOP is more rigid due to the high coupling generated by the inheritance which forces the developer to follow an “Is oriented approach“.

Think about it the “has oriented approach ” is the more natural one. Indeed in the real world, I collaborate with a person who has specific skills, no matter if it is from a specific family.

The fact that the generic programming is more flexible than OOP makes it the preferred choice for the modern C++ design as pointed by  Andrei Alexandrescu:

Modern C++ Design defines and systematically uses generic components - highly flexible design artifacts that are mixable and matchable to obtain rich behaviors with a small, orthogonal body of code.

Three assertions are interesting in his point of view:

  • Modern C++ Design defines and systematically uses generic components.
  • highly flexible design.
  • obtain rich behaviors with a small, orthogonal body of code.

To resume the generic programming is more naturable and flexible than the OOP approach and it gives more possibilities to write efficient code.