Code reuse is good. But subclassing generates a static coupling.
Problems
Coupling
Maintainability
Solutions
- Favor composition.
Exceptions
- If hierarchy follows the principle behaves like then it is safe.
Sample Code
Wrong
Right
Detection
- Overriding can issue warnings when subclassing concrete methods.
- Deep Hierarchies (more than 3 levels) are also a clue of bad subclassing.
Tags
- Composition
Conclusion
In legacy systems is very common to have Deep Hierarchies and method overriding, we need to refactor them and subclass by essential reasons and not implementative ones.
More info
Credits
Photo by Brandon Green on Unsplash
Discussion (0)