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
DRY Principle - as the name suggests - is aimed to reduce repetition of the same code implementations.
Example
1class Snowman(Humanoid):
2 def hug_snowman():
3 ...
4
5class Zombie(Humanoid):
6 def hug_zombie():
7 ...Refactoring
- Move Method
Sources
- ORIGIN1999 · ISBN 978-0201485677