Shotgun Surgery#
“Shotgun Surgery” is a code smell that occurs when a single change to the codebase requires multiple modifications across different classes or modules. This smell indicates that the codebase is not well-organized and that related functionality is spread out in a way that makes it difficult to maintain and evolve.
The term “Shotgun Surgery” is derived from the analogy of firing a shotgun, where a single pull of the trigger results in multiple pellets hitting different targets. In software development, this smell arises when a change in requirements or a bug fix necessitates making changes in multiple places, leading to a high degree of coupling between different parts of the system.
Shotgun Surgery can lead to several problems:
Maintenance Difficulty: When related functionality is scattered across different classes or modules, making changes becomes more complex and error-prone. Developers need to ensure that all affected areas are updated consistently, which can be challenging and time-consuming.
Code Duplication: Shotgun Surgery often results in duplicated code or logic being spread across different parts of the codebase. This redundancy can lead to maintenance issues, as changes need to be applied in multiple places.
High Coupling: The need to make changes in multiple places indicates a high degree of coupling between different components. This tight coupling makes the codebase less flexible and harder to maintain.
Reduced Readability: When related functionality is not grouped together, it becomes harder for developers to understand the codebase and its behavior. This lack of cohesion can lead to confusion and errors.
Risk of Errors: Making changes in multiple places increases the risk of introducing errors or inconsistencies. Developers may forget to update all affected areas, leading to bugs and unexpected behavior.
Solution#
- Consolidate Functionality: Identify related functionality that is spread across different classes and consolidate it into a single class or module. This will reduce the need for changes in multiple places.
Move Method to the class that should have this responsibility and then call it from the other classes.
Move Field
Inline Class