With the C++17 Concepts we can say goodbye to the class inheritance

Here’s the common definition of the inheritance from wikipedia:

In object-oriented programming (OOP), inheritance is when an object or class is based on another object (prototypal inheritance) or class (class-based inheritance), using the same implementation (inheriting from an object or class) specifying implementation to maintain the same behavior (realizing an interface; inheriting behavior). It is a mechanism for code reuse and to allow independent extensions of the original software via public classes and interfaces. 

The inheritance is overused in many C++ libraries and every C++ developer must master the inheritance and also the polymorphism mechanism if he chose to adopt the OOP approach. But what’s wrong with the inheritance?

Short answer: Very high coupling.

Explanation:

Let’s take as example the implementation of a class calculating a tax.

generics1

 

The CTaxCalculator collaborates only with classes inheriting from  ICalculator  and can’t use any other not ICalculator class even if it can help to calculate the Tax. The calculator implementation class will be highly coupled with ICalculator and no possibility to use another class kind unless we introduce many other classes and interfaces to bypass this limitation. For example the adapter pattern is a solution to bypass the inheritance high coupling issue. Think about it some GOF design patterns are there to also resolve some issues generated by the inheritance high coupling issue.

Generic programming to the rescue

Using the generic programming, the same Tax calculator could be implemented like this:

generics2

 

The CGenericTaxCalculator class in the other side calculate the tax  by using  any type capable to calculate the tax and it’s  not aware about its kind. What’s important is the methods implemented by the type and not the class kind. What makes the generic programming more natural and flexible. Indeed in the first implementation it’s like in the real world, a company searching for a developer accept only ones graduated from a specific school and reject all the others even if they have the skills needed.

But the flexibility comes with a price, the code became hard to understand, Indeed in OOP programming I can just go to the definition of ICalculator to know what we wait for this type. It’s not the case in generic programming, it’s difficult to know what we expect exactly from the template parameter? Which members must contains? Which constraints must be satisfied?

C++17 concepts to the rescue.

From this interesting article about C++17 concepts:

Concept is a term that describes a named set of requirements for a type.

Formal specification of concepts (ISO/IEC TS 19217) is an experimental technical specification, finalized in 2015, which makes it possible to verify that template arguments satisfy the expectations of a template or function during overload resolution and template specialization.

The concepts was planned in the C++11 standards, but it was postponed twice. The good news is that the C++17 will includes this interesting feature.

With concepts we can resolve the constraints specifications issue to make the code more readable and maintainable. It’s true that Boost provides for many years a concept implementation but adding them to the language will make C++ more powerful and unique.

Summary:

If the inheritance is overused when choosing the OOP approach, what introduce a high coupling between classes and force you to add more classes just to resolve this issue, it’s not the case when choosing to adopt the generic progamming approach. And fortunately the missing piece to implement a readable, maintainable, low coupled code will be part of the language soon.

 

Leave a Reply

Your email address will not be published. Required fields are marked *