Alternative Classes with Different Interfaces

It appears that two different classes are performing similar responsibilities, but have different interfaces.

Sign of Smell

"Alternative Classes with Different Interfaces" is a code smell that occurs when you have multiple classes in your codebase that serve similar purposes but have different method names, parameter lists, or interfaces. In other words, these classes should ideally share a common interface due to their conceptual similarities, but they don't.

Reason of Smell

  • Inconsistency: When multiple classes are supposed to perform similar functions but have different interfaces, it can create confusion for developers. Inconsistent naming, method signatures, or data structures make the codebase harder to understand and maintain.

  • Reduced Reusability: One of the primary advantages of object-oriented programming is the ability to reuse code through inheritance or interfaces. When classes with similar purposes have different interfaces, it becomes challenging to use them interchangeably, reducing code reusability.

  • Reduced Readability: Inconsistent interfaces make the code harder to read and understand. Developers need to learn and remember multiple ways of accomplishing the same task.

  • Code Duplication: Classes with different interfaces might end up duplicating similar code, even if they serve the same purpose. This redundancy increases the risk of bugs and inconsistencies.

  • Maintenance Complexity: Making changes to similar functionality spread across different classes becomes a maintenance challenge. Each change needs to be applied to multiple classes, leading to higher chances of errors.

  • Violation of DRY Principle: The Don't Repeat Yourself (DRY) principle encourages code reuse. When you have alternative classes with different interfaces, you're essentially repeating yourself by implementing similar logic in multiple places.

  • Increased Learning Curve: New developers joining the project have to learn and understand the different classes and their interfaces, which adds unnecessary complexity to their onboarding process.

  • Inefficient Communication: When team members communicate about the code, they might refer to different class names or method names for the same concept, leading to confusion and misunderstandings.

Refactoring Recipe

  • Unify Interfaces with Adapter

  • Rename Method

  • Move Method

Reference

https://refactoring.guru/smells/alternative-classes-with-different-interfaces

http://teddy-chen-tw.blogspot.com/2014/05/11alternative-classes-with-different.html

https://refactoring.guru/design-patterns/adapter

Last updated