Alternative Classes with Different Interfaces — Code Smells Catalog Skip to content

Alternative Classes with Different Interfaces

Also known as: Duplicate Abstraction

Object Oriented Abusers Duplication Code SmellDesign Smell Between Class

Two classes. Same job. Different spelling. One says hug_zombie(), the other says hug_snowman(), and neither realizes they're duplicating logic behind method names that could share a single interface.

2 min read 1 source

Overview

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

Each individual of Humanoid have similar but personalised logic but under different method names
1class Snowman(Humanoid):
2    def hug_snowman():
3        ...
4
5class Zombie(Humanoid):
6    def hug_zombie():
7        ...
PYTHON

Refactoring

  • Move Method

Sources

Browse All 56 Smells