Home Knowledge Base Shotgun Surgery

Shotgun Surgery is a code smell where a single conceptual change to the system requires making small, scattered modifications across many different classes, files, or modules simultaneously — the exact inverse of Divergent Change, indicating that a single cohesive concept is spread across the codebase rather than being localized in one place, so every time that concept must be modified, the developer must hunt down and update all its scattered fragments.

What Is Shotgun Surgery?

The smell manifests when one logical change requires touching many locations:

Why Shotgun Surgery Matters

Shotgun Surgery vs. Divergent Change

These two smells are opposite manifestations of the same cohesion problem:

SmellSymptomMeaning
Shotgun SurgeryOne change → many classesOne concept is scattered across many classes
Divergent ChangeOne class → many reasons to changeMany concepts are crammed into one class

Both indicate violation of the Single Responsibility Principle — either too much spread or too much concentration.

Refactoring: Move Method / Extract Class

The standard fix is consolidating scattered logic into a single location:

1. Identify the concept that requires shotgun surgery changes. 2. Create a new class (or identify the most appropriate existing class) to own that concept entirely. 3. Move all scattered implementations of the concept into that single class. 4. Replace all the scattered call sites with calls to the single consolidated class.

For the currency example: Create a CurrencyRegistry class that is the single source of truth for all currency-related data and logic. Every component that needs currency information asks CurrencyRegistry rather than implementing its own handling.

Tools

Shotgun Surgery is scattered logic — the smell that reveals when a single business concept has been distributed across a codebase rather than encapsulated in one location, turning every future enhancement of that concept into a multi-file archaeological expedition with a significant probability of missed sites and introduced bugs.

shotgun surgerycode ai

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.