Skip to main content

Code Smell 43 - Concrete Classes Subclassified

Published 2 min readMarkdown
Part of seriesCode Smells
Code Smell 43 - Concrete Classes Subclassified

Inheritance. Concrete classes. Reuse. A fantastic mix up.

Problems

Solutions

  1. Subclasses should be specializations.

  2. Refactor Hierarchies.

  3. Favor Composition.

  4. Leaf classes should be concrete.

  5. Not leaf classes should be abstract.

Sample Code

Wrong

Detection

Overriding a concrete method is a clear smell. We can enforce these policies on most linters.

Abstract classes should have just a few concrete methods. We can check against a predefined threshold for offenders.

Tags

  • Composition

Conclusion

Accidental sub-classification is the first obvious advantage for junior developers.

More mature ones find composition opportunities instead.

Composition is dynamic, multiple, pluggable, more testable, more maintainable and less coupled than inheritance.

Only sub-classify an entity if it follows the relationships behaves like.

After sub-classing the parent class should be abstract.

Relations

More info


Software is a gas; it expands to fill its container.

Nathan Myhrvold


This article is part of the CodeSmell Series.

About the author

  • I’m a senior software engineer loving clean code, and declarative designs. S.O.L.I.D. and agile methodologies fan.

No comments yet. Be the first to comment.

Code Smells

Part 1 of 50

In this series, we will see several symptoms and situations that make us doubt the quality of our developments. We will present possible solutions. Most are just clues. They are no hard rules.