Last Revision — April 19, 2022
1 Min Read
---
Change Preventers
Responsibility
Between Classes
- Shotgun Surgery (family)
- Combinatorial Explosion (family)
- Duplicated Code (causes)
Martin Fowler in book (1999): "Refactoring: Improving the Design of Existing Code"
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.
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).
Requires additional work to be done, which might be redundant.
class User(ABC):
...
functions: Functions
class Functions(ABC):
...
class BasicUser(User):
...
class BasicFunctions(Functions):
...
class PremiumUser(User):
...
class PremiumFunctions(Functions):
...
# each time a new user is added, so is a new function subclass with the same prefix
When solving, one must be cautious to not violate the Single Responsibility Principle.
class Animal(ABC):
...
food: Food
class Food(ABC):
...
class Elephant(Animal):
...
food: Vegan
class Vegan(Animal):
...
class Lion(Animal):
...
food: Carnivore
class Carnivore(Food):
...
# domain-like mapping might eventually be reused