Shotgun Surgery
   HOME

TheInfoList



OR:

Shotgun surgery is an
anti-pattern An anti-pattern in software engineering, project management, and business processes is a common response to a recurring problem that is usually ineffective and risks being highly counterproductive. The term, coined in 1995 by computer programmer An ...
in software development which occurs when a developer adds features to an application codebase which span a multiplicity of implementors or
implementation Implementation is the realization of an application, execution of a plan, idea, scientific modelling, model, design, specification, Standardization, standard, algorithm, policy, or the Management, administration or management of a process or Goal ...
s in a single change. This is common practice in many programming scenarios, as a great amount of programming effort is usually expended on adding new features to increase the value of programming assets. As a consequence, these new features may require adding code in several places simultaneously where the code itself looks very similar and may only have slight variations. Owing to the fast-paced nature of commercial software development, there may not be sufficient time to remodel (or refactor) a system to support the new features trivially. As a consequence, the practice of
copy-and-paste programming Copy-and-paste programming, sometimes referred to as just pasting, is the production of highly repetitive computer programming code, as produced by copy and paste operations. It is primarily a pejorative term; those who use the term are often impl ...
is prevalent; the code is written in a single place then simply copied to all other places where that implementation is required (with any required changes applied in-place). This practice is generally frowned on by the refactoring community as a direct violation of the abstraction principle, also known as the ''Once and Only Once rule'' – ultimately any change to the new functionality may require widespread changes. Further, any potential software bug in this new feature will be replicated many-fold and can make bug fixing particularly difficult and tedious. Even in the absence of copied code, the implementations are guaranteed to be very similar and just as prone to requirements change or bug fixing. This form of software development tends to favour short-term improvement (in the form of additional features) at the cost of long-term maintainability and stability.


Example

The canonical example of this practice is ''logging'' which generally adds prologue code to many functions simultaneously, for example: void Func() void Func2() ... void FuncN() Could be transformed to: void Func() void Func2() ... void FuncN() Here a single requirement has added similar code to several functions simultaneously. As such any change in requirements here (namely adding line numbers to the log) would now require a considerable effort. Shotgun surgery is not synonymous with cut and paste coding, as highlighted by this trivial example. 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).


Consequences of shotgun surgery

The concerns with this style are by-and-large 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. Typically some combination of the following is to be expected: * Increased developer effort and reduced throughput * Associated monetary cost of the above (as in commercial development) * Psychological effects and potential neglect of code Of these the most insidious are the psychological effects (e.g. see
broken windows theory In criminology, the broken windows theory states that visible signs of crime, antisocial behavior and civil disorder create an urban environment that encourages further crime and disorder, including serious crimes. The theory suggests that po ...
) which can exponentially lead to software rot. When uncontrolled this can cause entire codebases to become unmaintainable. Generally the only solution to this problem is to completely rewrite the code (at substantial cost).


Mitigation

Aspect-oriented programming In computing, aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does so by adding behavior to existing code (an advice) ''without'' modifying t ...
(AOP) aims at reducing these forms of invasive modifications in favour of adopting an "aspect" or "concern". The solutions take the form of
boilerplate code In computer programming, boilerplate code, or simply boilerplate, are sections of code that are repeated in multiple places with little to no variation. When using languages that are considered ''verbose'', the programmer must write a lot of boile ...
which can be applied over a domain of functions simultaneously (through the process of
weaving Weaving is a method of textile production in which two distinct sets of yarns or threads are interlaced at right angles to form a fabric or cloth. Other methods are knitting, crocheting, felting, and braiding or plaiting. The longitudinal ...
) which vastly reduces the amount of duplicated code. The use of
domain-specific language A domain-specific language (DSL) is a computer language specialized to a particular application domain. This is in contrast to a general-purpose language (GPL), which is broadly applicable across domains. There are a wide variety of DSLs, ranging ...
s is also becoming more widespread where light-weight
compiler In computing, a compiler is a computer program that Translator (computing), translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primaril ...
s are written to generate most of the duplicated code on the behalf of the programmer. Both methods fall into the broader categories of code generation and automation.


See also

* Shotgun debugging *
Technical debt In software development and other information technology fields, technical debt (also known as design debt or code debt) refers to the implied cost of additional work in the future resulting from choosing an expedient solution over a more robust o ...
*
Viscosity Viscosity is a measure of a fluid's rate-dependent drag (physics), resistance to a change in shape or to movement of its neighboring portions relative to one another. For liquids, it corresponds to the informal concept of ''thickness''; for e ...
, a measurement of resistance to change for the design of
notation In linguistics and semiotics, a notation system is a system of graphics or symbols, Character_(symbol), characters and abbreviated Expression (language), expressions, used (for example) in Artistic disciplines, artistic and scientific disciplines ...
s.


References

{{Reflist Anti-patterns