Shotgun surgery

Last updated
A shotgun surgery can also be when a change to a "Class A" causes several minor changes to other classes in a software system. Shotgun Surgery.png
A shotgun surgery can also be when a change to a "Class A" causes several minor changes to other classes in a software system.

Shotgun surgery is an anti-pattern in software development which occurs when a developer adds features to an application codebase which span a multiplicity of implementors or implementations 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 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).

Contents

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:

voidFunc(){...}voidFunc2(){...}...voidFuncN(){...}

Could be transformed to:

voidFunc(){printf("Entering Func\n");...}voidFunc2(){printf("Entering Func2\n");...}...voidFuncN(){printf("Entering FuncN\n");...}

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. [1]

Typically some combination of the following is to be expected:

Of these the most insidious are the psychological effects (e.g. see broken windows theory) which can exponentially lead to software rot.[ citation needed ] When uncontrolled this can cause entire codebases to become unmaintainable. Generally the only solution to this problem is to completely rewrite the code [ citation needed ] (at substantial cost).

Mitigation

Aspect-oriented programming (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 which can be applied over a domain of functions simultaneously (through the process of weaving) which vastly reduces the amount of duplicated code. The use of domain-specific languages is also becoming more widespread where light-weight compilers 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

Related Research Articles

C is a general-purpose computer programming language. It was created in the 1970s by Dennis Ritchie, and remains very widely used and influential. By design, C's features cleanly reflect the capabilities of the targeted CPUs. It has found lasting use in operating systems, device drivers, and protocol stacks, but its use in application software has been decreasing. C is commonly used on computer architectures that range from the largest supercomputers to the smallest microcontrollers and embedded systems.

<span class="mw-page-title-main">GNU Debugger</span> Source-level debugger

The GNU Debugger (GDB) is a portable debugger that runs on many Unix-like systems and works for many programming languages, including Ada, Assembly, C, C++, D, Fortran, Haskell, Go, Objective-C, OpenCL C, Modula-2, Pascal, Rust, and partially others.

Shotgun debugging can be defined as:

Multiple dispatch or multimethods is a feature of some programming languages in which a function or method can be dynamically dispatched based on the run-time (dynamic) type or, in the more general case, some other attribute of more than one of its arguments. This is a generalization of single-dispatch polymorphism where a function or method call is dynamically dispatched based on the derived type of the object on which the method has been called. Multiple dispatch routes the dynamic dispatch to the implementing function or method using the combined characteristics of one or more arguments.

In computer programming, unit testing is a software testing method by which individual units of source code—sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures—are tested to determine whether they are fit for use. It is a standard step in development and implementation approaches such as Agile.

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 implying a lack of programming competence and ability to create abstractions. It may also be the result of technology limitations as subroutines or libraries would normally be used instead. However, there are occasions when copy-and-paste programming is considered acceptable or necessary, such as for boilerplate, loop unrolling, or certain programming idioms, and it is supported by some source code editors in the form of snippets.

In mathematics and computer science, a higher-order function (HOF) is a function that does at least one of the following:

In computing, a polyglot is a computer program or script written in a valid form of multiple programming languages or file formats. The name was coined by analogy to multilingualism. A polyglot file is composed by combining syntax from two or more different formats.

In computer programming, a callback or callback function is any reference to executable code that is passed as an argument to another piece of code; that code is expected to call back (execute) the callback function as part of its job. This execution may be immediate as in a synchronous callback, or it might happen at a later point in time as in an asynchronous callback. They are also called blocking and non-blocking.

<span class="mw-page-title-main">Continuous integration</span> Software development practice based on frequent submission of granular changes

In software engineering, continuous integration (CI) is the practice of merging all developers' working copies to a shared mainline several times a day. Nowadays it is typically implemented in such a way that it triggers an automated build with testing. Grady Booch first proposed the term CI in his 1991 method, although he did not advocate integrating several times a day. Extreme programming (XP) adopted the concept of CI and did advocate integrating more than once per day – perhaps as many as tens of times per day.

In computer programming, the term hooking covers a range of techniques used to alter or augment the behaviour of an operating system, of applications, or of other software components by intercepting function calls or messages or events passed between software components. Code that handles such intercepted function calls, events or messages is called a hook.

In parallel computing, a barrier is a type of synchronization method. A barrier for a group of threads or processes in the source code means any thread/process must stop at this point and cannot proceed until all other threads/processes reach this barrier.

C++11 is a version of the ISO/IEC 14882 standard for the C++ programming language. C++11 replaced the prior version of the C++ standard, called C++03, and was later replaced by C++14. The name follows the tradition of naming language versions by the publication year of the specification, though it was formerly named C++0x because it was expected to be published before 2010.

Programming by permutation, sometimes called "programming by accident" or "shotgunning", is an approach to software development wherein a programming problem is solved by iteratively making small changes (permutations) and testing each change to see if it behaves as desired. This approach sometimes seems attractive when the programmer does not fully understand the code and believes that one or more small modifications may result in code that is correct.

In computer programming, a branch table or jump table is a method of transferring program control (branching) to another part of a program using a table of branch or jump instructions. It is a form of multiway branch. The branch table construction is commonly used when programming in assembly language but may also be generated by compilers, especially when implementing optimized switch statements whose values are densely packed together.

stdarg.h is a header in the C standard library of the C programming language that allows functions to accept an indefinite number of arguments. It provides facilities for stepping through a list of function arguments of unknown number and type. C++ provides this functionality in the header cstdarg.

In software development, or any other IT field technical debt is the implied cost of future reworking required when choosing an easy but limited solution instead of a better approach that could take more time.

In computer programming, variadic templates are templates that take a variable number of arguments.

<span class="mw-page-title-main">Go (programming language)</span> Programming language

Go is a statically typed, compiled high-level programming language designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. It is syntactically similar to C, but also has memory safety, garbage collection, structural typing, and CSP-style concurrency. It is often referred to as Golang because of its former domain name, golang.org, but its proper name is Go.

<span class="mw-page-title-main">Nim (programming language)</span> Programming language

Nim is a general-purpose, multi-paradigm, statically typed, compiled high-level systems programming language, designed and developed by a team around Andreas Rumpf. Nim is designed to be "efficient, expressive, and elegant", supporting metaprogramming, functional, message passing, procedural, and object-oriented programming styles by providing several features such as compile time code generation, algebraic data types, a foreign function interface (FFI) with C, C++, Objective-C, and JavaScript, and supporting compiling to those same languages as intermediate representations.

References

  1. Shatnawi, R.; Wei Li (2006). "An Investigation of Bad Smells in Object-Oriented Design". Third International Conference on Information Technology: New Generations (ITNG'06). pp. 161–165. doi:10.1109/ITNG.2006.31. ISBN   0-7695-2497-4. S2CID   13107711.