Overview
Similar to Divergent Change, but with a broader spectrum, the smell symptom of the Shotgun Surgery code is detected by the unnecessary requirement of changing multiple different classes to introduce a single modification. Things like that can happen with the failure to use the correct design pattern for the given system. This expansion of functionality can lead to an easy miss (and thus introduce a bug) if these small changes are all over the place and they are hard to find. Most likely, too many classes solve a simple problem.
Joshua Kerievsky noted this smell as Solution Sprawl [1]. Monteiro stated that the tiny difference between these two comes from how they are sensed. In the Divergent Change, one becomes aware of the smell while making the changes, and in the Solution Sprawl, one is aware by observing the issue. [2]
Problems
The codebase is non-cohesive.
The increased learning curve for new developers to effectively implement a change.
Example
1class Minion:
2 energy: int
3
4 def attack(self):
5 if self.energy < 20:
6 animate('no-energy')
7 skip_turn()
8 return
9 ...
10
11 def cast_spell(self):
12 if self.energy < 50:
13 animate('no-energy')
14 skip_turn()
15 return
16 ...
17
18 def block(self):
19 if self.energy < 10:
20 animate('no-energy')
21 skip_turn()
22 return
23 ...
24
25 def move(self):
26 if self.energy < 35:
27 animate('no-energy')
28 skip_turn()
29 return
30 ...Refactoring
- Extract Method
- Combine Functions into Class
- Combine Functions into Transform
- Split Phase
- Move Method and Move Field
- Inline Function/Class
Sources
- ORIGIN1999 · ISBN 978-0201485677