Code Smell Parallel Inheritance Hierarchies
Signs and Symptoms
Whenever you create a subclass for a class, you find yourself needing to create a subclass for another class.
Reasons for the Problem
All was well as long as the hierarchy stayed small. But with new classes being added, making changes has become harder and harder.
- You may de-duplicate parallel class hierarchies in two steps. First, make instances of one hierarchy refer to instances of another hierarchy. Then, remove the hierarchy in the referred class, by using Move Method and Move Field.
- Reduces code duplication.
- Can improve organization of code.
When to Ignore
- Sometimes having parallel class hierarchies is just a way to avoid even bigger mess with program architecture. If you find that your attempts to de-duplicate hierarchies produce even uglier code, just step out, revert all of your changes and get used to that code.
Tired of reading?
No wonder, there are 7 hours worth of the text on this website.
Try our interactive course on refactoring. It offers less boring approach to learning new stuff.Let's see...