Discova
Back to cards
Programming2 min readWikipedia, updated 26 November 2026

Shotgun surgery

Anti-pattern in software development

In software development, shotgun surgery is an anti-pattern which occurs when a developer adds features to an application codebase in multiple places, rather than implementing the desired functionality in a single place to make it reusable.

Characteristics

Shotgun surgery takes form of many simultaneous changes in different places to implement a feature or fix across all of them, typically as a result of excessive separation of functionality or insufficient separation of functionality.

It is not synonymous with copy-and-paste programming. The practice of copying code can be viewed as a "means to an end", where shotgun surgery is merely an "end" (i.e. there are many ways to reach the same conclusion).

Example

The canonical example of this practice is logging, which generally adds prologue code to many functions simultaneously. For example:

Could be changed to:

Here, a single change requires the addition of similar code to several functions simultaneously. As such any change in requirements here (namely adding line numbers to the log) would now require considerable effort.

Consequences

The concerns with this practice are mostly the same as those for any duplication in a software system; that is, duplicating the same logic in many places can vastly increase the costs of making changes to the same logic later. Some of the aforementioned costs are measurable, others are not (at least not trivially). There is also some evidence that this anti-pattern is correlated with higher defect rates.

Shotgun surgery can reduce developer throughput, introduce collaborative issues due to merge conflicts, increase cognitive load, increase the costs of maintaining software, and lead to software rot.

Mitigation

Aspect-oriented programming aims at reducing these forms of invasive modifications in favour of adopting an "aspect" or "concern". The solutions take the form of boilerplate code which can be applied over a domain of functions simultaneously (through the process of weaving) which vastly reduces the amount of duplicated code. Domain-specific languages can be used to generate duplicated code on the behalf of the programmer. Both methods fall into the broader categories of code generation and automation.

Sometimes the only solution is to completely rewrite code, at substantial cost.

Adapted from the Wikipedia article, CC BY-SA 4.0. Continue on Wikipedia

Go deeper

Ideas this article links to.