Parallel Inheritance Hierarchies — Code Smells Catalog Skip to content

Parallel Inheritance Hierarchies

Change Preventers Responsibility Code Smell Between Class

Add a BasicUser, and you need a BasicFunctions. Add a PremiumUser, and here comes PremiumFunctions. Every subclass in one hierarchy demands a mirror in the other, and the cost of every new feature...

2 min read 1 source

Overview

This occurs when an inheritance tree depends on another inheritance tree by composition, and to create a subclass for a class, one finds that he has to make a subclass for another class. Fowler specified that this is a special case of Shotgun Surgery code smell.

Causation

This smell can happen naturally when trying to model a problem in a domain. The problem arises when these hierarchies are created artificially and unnecessarily (for example, by adding a standard prefix throughout the classes).

Problems

📋
Duplication

Requires additional work to be done, which might be redundant.

Example

When solving, one must be cautious to not violate the Single Responsibility Principle.
1class User(ABC):
2    ...
3    functions: Functions
4
5class Functions(ABC):
6    ...
7
8
9
10class BasicUser(User):
11    ...
12
13class BasicFunctions(Functions):
14    ...
15
16
17
18
19class PremiumUser(User):
20    ...
21
22class PremiumFunctions(Functions):
23    ...
24
25# each time a new user is added, so is a new function subclass with the same prefix
PYTHON

Refactoring

  • Move Method
  • Move Field
  • Create Partial
  • Fold Hierarchy into One

Sources

Browse All 56 Smells