OdeToCode IC Logo

Learning From the Ghosts of Software Past

Thursday, February 14, 2013

It was about 12 years ago when I helped to write an application that featured a class hierarchy like the following:

Inheritance Is A Tool of the Devil

I remember the code because I was the one to refactor the application to build the class hierarchy in an attempt to remove duplicated code.

I believed what I was creating was a better application.

What I actually created was a model where other developers struggled to understand when a virtual method override would kick in. They had to work hard to figure out when to properly call a base class method. All changes inside the hierarchy were like moving balanced blocks of wood in a game of Jenga. Someone eventually has to lose a game of Jenga. You just hope the structure stays stable enough to make it through through your turn.

Learning a Lesson

As a kid my parents told me not to play with matches, not to go near the dog chained to the oak tree down the street, and to always hold on to the handrail when the back porch steps were wet.

As a kid, I didn't understand why my parents told me these things until I was burned, or chewed, or contemplating life during a cartwheeling descent of rain slickened steps.

Similarly, I believe there are some aspects of software development that you have to experience to truly appreciate. I've been looking for a good example where composition clearly trumps inheritance, and might have something that is contrived, of course, but reasonable enough to demonstrate the principle and demonstrate some of the pain.

Coming up next – the Composition Kata.

Gravatar AbelebA Thursday, February 14, 2013
12 years ago? I created something similar last year and felt quite proud about it. Fortunately for me, and others, I haven't had a need to make changes. Just lucky I guess. Looking forward to reading the follow-up...
Gravatar LosManos Thursday, February 14, 2013
The allegory(?) "Jenga" and "make it through through your turn" were the best I've heard on the subject. I still write this type of code because architecturally it feels correct. But when the real world hits...
Gravatar Steve Gentile Friday, February 15, 2013
looking forward to the composition kata! good stuff, thanks Scott - its rather interesting how our styles and views of code mature and change over time
Gravatar Remco Friday, February 15, 2013
Yup, indeed it's a lesson most developers only learn after a few years. Always try to use composition over inheritance, unless it causes greater complexity. Working like this forces a developer to build smaller objects with single responsibilities. The challenge we are facing now is how to communicate all those small reusable components between developers, meaning how to make them aware and let them reuse those, instead of rewriting them.
Comments are closed.