paint-brush
OOP Design: Composible objects support changeby@clm160
1,451 reads
1,451 reads

OOP Design: Composible objects support change

by Liviu CosteaDecember 13th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

I believe ‘composition over inheritance’ is the most important OOP principle understood by very few people writing code in OOP languages and applied by even fewer developers. And I am basing my affirmation on my whole experience as a developer and interviewer at 4 companies since I started writing the first lines of code and getting paid for it back in 2003. To be honest it is also based on the reading that I have done in the past years, and I can also tell you there are a lot of developers better than me that say this.

Company Mentioned

Mention Thumbnail
featured image - OOP Design: Composible objects support change
Liviu Costea HackerNoon profile picture

Best tools for the job

I believe ‘composition over inheritance’ is the most important OOP principle understood by very few people writing code in OOP languages and applied by even fewer developers. And I am basing my affirmation on my whole experience as a developer and interviewer at 4 companies since I started writing the first lines of code and getting paid for it back in 2003. To be honest it is also based on the reading that I have done in the past years, and I can also tell you there are a lot of developers better than me that say this.

At all the interviews that I have participated whenever I asked the question nobody knew how to explain ‘composition over inheritance’. On the other side I don’t ask this composition question very frequently, I only put it in front of developers that I consider to be above average. I know that we are all biased so maybe I missed a good explanation because of my take on ‘above average’ developer.

Good developers are 10 times more productive than the average developer, but it is not about typing several times faster, it is mostly taking better decisions. And those decisions are the ones that give you the flexibility you need when your client comes up with his new great feature to add.

Base everything

A lot of us developers in Java/C# have been involved in at least a project where we could find a class like BaseService or BaseManager or BaseClient or BaseHandler or even all of them. And such a class is a clear sign that inheritance was used for the wrong reason: a place to put the duplicate code. I believe that inheritance can still help us if we use it in the right way and that would mean to reach polymorphic design. I also see that inheritance is probably the most abused OOP concept in our code, so maybe removing it and replacing it with interfaces only, might do our code a greater good. Until such thing happens let me try to explain why it is better to go with composite objects and not with derived ones.

When we learn in school (computer science faculty) about inheritance usually we have an example like Dog is a Mammal, so we can have class Mammal with behaviors like gives birth to children, feds children with milk and then we have the Dog class that inherits from Mammal and we can add specific dog behavior like barking. And this a good example because whenever we use just the Mammal behavior of the Dog then we can work with the base class. So we are reaching the polymorphic state, because instead of a Dog we can have other objects that inherit from Mammals.

Inheritance

Design a School

Now think of designing a School system. When we say ‘school’ most of us think of the building so a first step in designing it would be to create a Building class which becomes the base class of a School class. And we think it is reasonable because whatever attributes and behavior a building has they will be inherited by the school. Then we see that a school is a place where we study in classes, learn new things, take exams, have Christmas celebration and many more. Still there is no contradiction if the School inherits a Building. You can still design a working system with this inheritance.

School inherits from building

Lets put the question in a more OOP way: does the School have a building or is a building? Like we said before there is no right answer, you could inherit from the building class or have a building part of the School and in both cases you can come up with an working solution for your client’s problem. It is more about the future changes, the ones that even your client has no clues about right now.

We could get feature requests to create online classes and exams, make the study material be in other languages and so on. Some requests might get you in trouble when you inherit the School from Building, like supporting new wings in other towns. In such a case the School needs to support more than one building, so a composition would be better. So maybe for simple systems inheritance could work, but for systems that require change (probably all of them today) composition will be a better choice.

School & building composition

The School and Building is a simple example, we usually don’t have the luxury to work with such simple concepts in our code. The same with dogs and mammals. But these concepts are the best starting point to explain more complex ones. And I am thinking of doing an workshop on OOP Design, where one of the main discussion points would be composition over inheritance. You can find out more about it here.