Code Smell 161 - Abstract/Final/Undefined Classes

Code Smell 161 - Abstract/Final/Undefined Classes

Code Smell 161 - Abstract/Final/Undefined Classes

Your classes are abstract, final, or undefined

TL;DR: If your language has the right tool, your classes should be either abstract or final.

Problems

  • Subclassification for?Code Reuse
  • Classes with?just one?concrete subclass
  • Liskov Substitution Violation
  • Yo-Yo?Problem

Solutions

  1. Declare all your leaf classes as?final?and the rest of them?abstract.

Context

Managing hierarchies and composition is the main task of a good software designer.

Keeping hierarchies healthy is crucial to favor cohesion and avoid?coupling.

Sample Code

Wrong

public class Vehicle
{
  // 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
{
  // class is leaf. Therefore it should be final
}

public class Motorcycle extends Vehicle
{
  // class is leaf. Therefore it should be final
}        

Right

abstract public class Vehicle
{
  // class is not a leaf. Therefore it is 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
{
  // class is leaf. Therefore it is final
}

final public class Motorcycle extends Vehicle
{
  // class is leaf. Therefore it is final
}        

Detection

[X] Automatic

Since this is enforced by static analysis, we can't do it with most available tools.

Tags

  • Subclassification

Conclusion

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.

Relations

Code Smell 11 - Subclassification for Code Reuse

Code Smell 136 - Classes With just One Subclass

Code Smell 37 - Protected Attributes

Code Smell 58 - Yo-yo Problem

More Info

Coupling - The one and only software design problem

Deep Subclasses

Disclaimer

Code Smells are just my?opinion.

Credits

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.

How to Find the Stinky parts of your Code

要查看或添加评论,请登录

Maximiliano Contieri的更多文章

  • Code Smell 295 - String Concatenation

    Code Smell 295 - String Concatenation

    Untangling the string mess in your code TL;DR: Avoid string concatenation for complex strings, use templates. Problems…

  • Code Smell 294 - Implicit Return

    Code Smell 294 - Implicit Return

    Your language adds clever features. Making YOU more obsolete TL;DR: Overusing implicit returns makes your code harder…

  • The Great Programmer Purge: How AI Is Taking Over the Tech Workforce

    The Great Programmer Purge: How AI Is Taking Over the Tech Workforce

    How AI is Redefining the Role of Programmers in the Tech Industry TL;DR: AI-generated code outperforms lazy…

    1 条评论
  • Refactoring 024 - Replace Global Variables with Dependency Injection

    Refactoring 024 - Replace Global Variables with Dependency Injection

    Break Hidden Dependencies for Cleaner Code TL;DR: Replace global variables with dependency injection to improve…

  • 10 More Simple Tips to Boost Your Productivity x2

    10 More Simple Tips to Boost Your Productivity x2

    The previous article on life hacks was a huge success. Let's start the year with more productivity tips! TL;DR: More…

  • Code Smell 293 - isTesting

    Code Smell 293 - isTesting

    Don’t let test code sneak into production TL;DR: Avoid adding isTesting or similar flags. Problems ?? Leaky abstraction…

  • Refactoring 001 - Remove Setters

    Refactoring 001 - Remove Setters

    Setters violate immutability and add accidental coupling TL;DR: Make your attributes private to favor mutability…

  • Code Smell 292 - Missing Return

    Code Smell 292 - Missing Return

    When your code loses its way TL;DR: Missing return statements cause unexpected behavior. Problems ?? Silent failures…

  • Code Smell 291 - Mutable Keys

    Code Smell 291 - Mutable Keys

    Changing Keys, Losing Values TL;DR: When you use mutable objects as keys in hashed collections, changing them breaks…

    2 条评论
  • Refactoring 023 - Replace Inheritance with Delegation

    Refactoring 023 - Replace Inheritance with Delegation

    Transform your rigid inheritance into flexible delegations TL;DR: Replace restrictive inheritance hierarchies with…

社区洞察

其他会员也浏览了