Shotgun Surgery

Making a single change requires simultaneous changes to multiple locations.

Sign of Smell

Shotgun Surgery, also known as Solution Sprawl, is one of my favorite code smells because its name vividly describes what happens when we try to modify code. In other words, it's like firing a "shotgun" at your codebase, resulting in scattered and widespread changes. When we try to make a single change, we often need to modify multiple pieces of code at the same time.

This smell is the opposite of "Divergent Change". When we compare these two smells, "Shotgun Surgery" causes many changes, while "Divergent Change" is caused by many changes.

Reason of Smell

Here's why "Shotgun Surgery" is considered bad and problematic:

  • Violation of the Open/Closed Principle (OCP): The OCP is one of the SOLID principles of object-oriented design, and it states that every code entity should be open for extension but closed for modification. Each time you need to add new behavior or make a change, you're forced to open and alter existing code, which increases the risk of introducing bugs and destabilizing the system.

  • High Coupling: Coupling is a measure of how closely one class or module is connected to or dependent on another. When you have Shotgun Surgery, it often indicates high coupling between different parts of your codebase.

  • Complexity: Frequent changes in multiple locations can make the code more complex and harder to follow. This complexity can hinder the ability to debug, test, and understand the code.

  • Maintainability: When a small change requires updates in multiple places, it increases the risk of introducing errors. Developers might forget to update one of the affected areas, leading to inconsistencies and bugs. It also makes it harder to understand the code since you need to keep track of all the different places where a change might occur.

  • Increased Development Time: Shotgun Surgery can significantly slow down the development process. Each small change becomes a time-consuming task because you have to make those changes in multiple locations, which can lead to inefficiency and increased development time.

Refactoring Recipes

  • Move Method

  • Move Field

  • Inline Class

  • Move Creation Knowledge to Factory

Move Method / Move Field

When you encounter the Shotgun Surgery smell in your codebase, it indicates that the related code that should be grouped together is scattered throughout the codebase. To address this, we can utilize refactoring techniques such as Move Method or Move Field to consolidate the existing behaviors in one class.

In the end, we can transition from a Shotgun to a Rifle, hitting our target precisely with a single shot.

Inline Class

If moving code to the same class leaves the original class almost empty, we should consider applying the Inline Class technique to avoid redundant code. Inline Class is the opposite of Extract Class refactoring and helps us eliminate unnecessary classes.

Reference

https://refactoring.guru/smells/shotgun-surgery https://luzkan.github.io/smells/shotgun-surgery

Last updated