Navs
Code Smells Catalog
Alternative Classes with Different Interfaces

Last Revision — April 19, 2022

1 Min Read


  • Also Known As

    Duplicate Abstraction

  • Obstruction

    Object Oriented Abusers

  • Occurrence

    Duplication

  • Expanse

    Between Classes

  • Related Smells

    - Oddball Solution (family)
    - Duplicated Code (co-exist)

  • History

    Martin Fowler in book (1999): "Refactoring: Improving the Design of Existing Code"

Alternative Classes with Different Interfaces

If two classes have the same functionality but different implementations, developers should merge them, or developers should extract a superclass to limit Code Duplication. This smell occurs whenever a class can operate on two alternative classes (for example, take Zombie and Snowman). However, the interface to these alternative classes is different - when it operates with Zombie, it calls hug_zombie(), and with Snowman, it has to call hug_snowman().

Causation

This may happen due to the oversight that a functionally equivalent class already exists or when two or more developers are working on code to handle a similar situation. However, they did not know about the other's work — lack of abstract methods that enforce the common implementation method names.

Problems

Don't Repeat Yourself Violation

DRY Principle - as the name suggests - is aimed to reduce repetition of the same code implementations.

Example

Smelly:

Each individual of Humanoid have similar but personalised logic but under different method names

class Snowman(Humanoid):
    def hug_snowman():
        ...

class Zombie(Humanoid):
    def hug_zombie():
        ...

Solution:

Use common method.

class Snowman(Humanoid):
    def hug():
        ...

class Zombie(Humanoid):
    def hug():
        ...

Refactoring:

  • Change Function Declaration
  • Move Function
  • Extract Superclass

Sources
  • [Origin] - Martin Fowler, "Refactoring: Improving the Design of Existing Code" (1999)