Shotgun Surgery — Code Smells Catalog Skip to content

Shotgun Surgery

Also known as: Solution Sprawl

Change Preventers Responsibility Code SmellDesign Smell Between Class

One small feature change. A dozen files to edit. You submit the PR, then find two more files you missed.

2 min read 1 source

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]

Causation

Wake says it could have happened due to an “overzealous attempt to eliminate Divergent Change” [3]. A missing class could understand the entire responsibility and handle the existing cluster of changes by itself. That scenario could also happen with cascading relationships of classes [4].

Problems

Single Responsibility Principle Violation

The codebase is non-cohesive.

📋
Duplicated Code

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        ...
PYTHON

Refactoring

  • Extract Method
  • Combine Functions into Class
  • Combine Functions into Transform
  • Split Phase
  • Move Method and Move Field
  • Inline Function/Class

Sources

Browse All 56 Smells