Your classes are abstract, final, or undefined
TL;DR: If your language has the right tool, your classes should be either abstract or final.
-
Subclassification for Code Reuse
-
Classes with just one concrete subclass
-
Liskov Substitution Violation
-
Yo-Yo Problem
- Declare all your leaf classes as final and the rest of them abstract.
Managing hierarchies and composition is the main task of a good software designer.
Keeping hierarchies healthy is crucial to favor cohesion and avoid coupling.
public class Vehicle
{
// the class is not a leaf. Therefore it should be abstract
// an abstract method that only declares, but does not define the start
// functionality because each vehicle uses a different starting mechanism
abstract void start();
}
public class Car extends Vehicle
{
// the class is a leaf. Therefore it should be final
}
public class Motorcycle extends Vehicle
{
// the class is a leaf. Therefore it should be final
}
abstract public class Vehicle
{
// the class is not a leaf. Therefore it must be abstract
//an abstract method that only declares, but does not define the start
//functionality because each vehicle uses a different starting mechanism
abstract void start();
}
final public class Car extends Vehicle
{
// the class is a leaf. Therefore it is final
}
final public class Motorcycle extends Vehicle
{
// the class is a leaf. Therefore it is final
}
[X] Automatic
Since this is enforced by static analysis, we can't do it with most available tools.
- Subclassification
We should look back at our classes and start qualifying them either as abstract or final.
There are no valid cases for two concrete classes, one subclassifying the other.
Code Smell 11 - Subclassification for Code Reuse
Code Smell 136 - Classes With just One Subclass
Code Smell 37 - Protected Attributes
Coupling - The one and only software design problem
Code Smells are just my opinion.
Photo by William Bossen on Unsplash
When the final design seems too simple for the amount of work you've put in, then you know you're done.
Brady Clark
Software Engineering Great Quotes
This article is part of the CodeSmell Series.