Code Smell 11 - Subclassification for Code Reuse

Subscribe to my newsletter and never miss my upcoming articles

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

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.

Relations

More info

Credits

Photo by Brandon Green on Unsplash


This article is part of the CodeSmell Series.

No Comments Yet