Copy-and-paste programming, sometimes referred to as just pasting, is the production of highly repetitive
computer programming
Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
code, as produced by
copy and paste
Cut, copy, and paste are essential Command (computing), commands of modern human–computer interaction and user interface design. They offer an interprocess communication technique for transferring data (computing), data through a computer's us ...
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 (e.g., an insufficiently expressive development environment) 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
Loop unrolling, also known as loop unwinding, is a loop transformation technique that attempts to optimize a program's execution speed at the expense of its binary size, which is an approach known as space–time tradeoff. The transformation c ...
(when not supported automatically by the compiler), languages with limited
metaprogramming
Metaprogramming is a computer programming technique in which computer programs have the ability to treat other programs as their data. It means that a program can be designed to read, generate, analyse, or transform other programs, and even modi ...
facilities, or certain programming idioms, and it is supported by some
source code editor
A source-code editor is a text editor program designed specifically for editing source code of computer programs. It may be a standalone application or it may be built into an integrated development environment (IDE).
Features
Source-code editor ...
s in the form of
snippets.
Origins
Copy-and-paste programming is often done by inexperienced or student programmers, who find the act of writing code from scratch difficult or irritating and prefer to search for a pre-written solution or partial solution they can use as a basis for their own problem solving.
[
]
(See also
Cargo cult programming)
Inexperienced programmers who copy code often do not fully understand the pre-written code they are taking. As such, the problem arises more from their inexperience and lack of
courage in programming than from the act of copying and pasting, per se. The code often comes from disparate sources such as friends' or co-workers' code,
Internet forums, open-source projects, code provided by the student's professors/TAs, or
computer science
Computer science is the study of computation, information, and automation. Computer science spans Theoretical computer science, theoretical disciplines (such as algorithms, theory of computation, and information theory) to Applied science, ...
textbooks. The result risks being a disjointed clash of styles, and may have superfluous code that tackles problems for which new solutions are no longer required.
A further problem is that
bugs can easily be introduced by assumptions and design choices made in the separate sources that no longer apply when placed in a new environment.
Such code may also, in effect, be unintentionally
obfuscated
Obfuscation is the obscuring of the intended meaning of communication by making the message difficult to understand, usually with confusing and ambiguous language. The obfuscation might be either unintentional or intentional (although intent u ...
, as the names of variables, classes, functions and the like are typically left unchanged, even though their purpose may be completely different in the new context.
Copy-and-paste programming may also be a result of poor understanding of features common in computer languages, such as loop structures, functions and subroutines.
Duplication
Applying library code
Copying and pasting is also done by experienced programmers, who often have their own libraries of well tested, ready-to-use code snippets and generic
algorithm
In mathematics and computer science, an algorithm () is a finite sequence of Rigour#Mathematics, mathematically rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algo ...
s that are easily adapted to specific tasks.
Being a form of
code duplication In computer programming, duplicate code is a sequence of source code that occurs more than once, either within a program or across different programs owned or maintained by the same entity. Duplicate code is generally considered undesirable for a n ...
, copy-and-paste programming has some intrinsic problems; such problems are exacerbated if the code doesn't preserve any semantic link between the source text and the copies. In this case, if changes are needed, time is wasted hunting for all the duplicate locations. (This can be partially mitigated if the original code and/or the copy are properly commented; however, even then the problem remains of making the same edits multiple times. Also, because code maintenance often omits updating the comments, comments describing where to find remote pieces of code are notorious for going out-of-date.)
Adherents of
object oriented
Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impleme ...
methodologies further object to the "code library" use of copy and paste. Instead of making multiple mutated copies of a generic algorithm, an object oriented approach would
abstract the algorithm into a reusable
encapsulated class
Class, Classes, or The Class may refer to:
Common uses not otherwise categorized
* Class (biology), a taxonomic rank
* Class (knowledge representation), a collection of individuals or objects
* Class (philosophy), an analytical concept used d ...
. The class is written flexibly, with full support of
inheritance
Inheritance is the practice of receiving private property, titles, debts, entitlements, privileges, rights, and obligations upon the death of an individual. The rules of inheritance differ among societies and have changed over time. Offi ...
and
overloading, so that all calling code can be interfaced to use this generic code directly, rather than mutating the original. As additional functionality is required, the library is extended (while retaining
backward compatibility
In telecommunications and computing, backward compatibility (or backwards compatibility) is a property of an operating system, software, real-world product, or technology that allows for interoperability with an older legacy system, or with Input ...
). This way, if the original algorithm has a bug to fix or can be improved, all software using it stands to benefit.
Generic programming
Generic programming is a style of computer programming in which algorithms are written in terms of data types ''to-be-specified-later'' that are then ''instantiated'' when needed for specific types provided as parameters. This approach, pioneer ...
provides additional tools to create abstractions.
Branching code
Branching code is a normal part of large-team software development, allowing parallel development on both branches and hence, shorter development cycles. Classical branching has the following qualities:
* Is managed by a
version control
Version control (also known as revision control, source control, and source code management) is the software engineering practice of controlling, organizing, and tracking different versions in history of computer files; primarily source code t ...
system that supports branching
* Branches are re-merged once parallel development is completed.
Copy and paste is a less formal alternative to classical branching, often used when it is foreseen that the branches will diverge more and more over time, as when a new product is being spun off from an existing product.
As a way of spinning-off a new product, copy-and-paste programming has some advantages. Because the new development initiative does not touch the code of the existing product:
* There is no need to
regression test
Regression testing (rarely, ''non-regression testing'') is re-running functional and non-functional tests to ensure that previously developed and tested software still performs as expected after a change. If not, that would be called a '' regr ...
the existing product, saving on QA time associated with the new product launch, and reducing
time to market
In commerce, time to market (TTM) is the length of time it takes from a product being conceived until its being available for sale. The reason that time to market is so important is that being late erodes the addressable market into which produ ...
.
* There is no risk of introduced bugs in the existing product, which might upset the installed user base.
The downsides are:
* If the new product does not diverge as much as anticipated from the existing product, two code bases might need to be supported (at twice the cost) where one would have done. This can lead to expensive
refactoring
In computer programming and software design, code refactoring is the process of restructuring existing source code—changing the '' factoring''—without changing its external behavior. Refactoring is intended to improve the design, structure, ...
and manual merging down the line.
* The
duplicate code In computer programming, duplicate code is a sequence of source code that occurs more than once, either within a program or across different programs owned or maintained by the same entity. Duplicate code is generally considered Code smell, undesira ...
base doubles the time required to implement changes which may be desired across both products; this ''increases'' time-to-market for such changes, and may, in fact, wipe out any time gains achieved by branching the code in the first place.
Similar to above, the alternative to a copy-and-paste approach would be a modularized approach:
* Start by factoring out code to be shared by both products into libraries.
* Use those libraries (rather than a second copy of the code base) as the foundation for the development of the new product.
* If an additional third, fourth, or fifth version of the product is envisaged down the line, this approach is far stronger, because the ready-made code libraries dramatically shorten the development life cycle for any additional products after the second.
Repetitive tasks or variations of a task

One of the most harmful forms of copy-and-paste programming occurs in code that performs a repetitive task, or variations of the same basic task depending on some variable. Each instance is copied from above and pasted in again, with minor modifications. Harmful effects include:
* The copy and paste approach often leads to large methods (a bad
code smell
In computer programming, a code smell is any characteristic in the source code of a program that possibly indicates a deeper problem. Determining what is and is not a code smell is subjective, and varies by language, developer, and development met ...
).
* Each instance creates a code duplicate, with all the problems discussed in prior sections, but with a much greater scope. Scores of duplications are common; hundreds are possible. Bug fixes, in particular, become very difficult and costly in such code.
* Such code also suffers from significant readability issues, due to the difficulty of discerning exactly what differs between each repetition. This has a direct impact on the risks and costs of revising the code.
* The
procedural programming
Procedural programming is a programming paradigm, classified as imperative programming, that involves implementing the behavior of a computer program as Function (computer programming), procedures (a.k.a. functions, subroutines) that call each o ...
model strongly discourages the copy-and-paste approach to repetitive tasks. Under a procedural model, a preferred approach to repetitive tasks is to create a function or subroutine that performs a single pass through the task; this subroutine is then called by the parent routine, either repetitively or better yet, with some form of looping structure. Such code is termed "well decomposed", and is recommended as being easier to read and more readily extensible.
* The general
rule of thumb
In English language, English, the phrase ''rule of thumb'' refers to an approximate method for doing something, based on practical experience rather than theory. This usage of the phrase can be traced back to the 17th century and has been associat ...
applicable to this case is "
don't repeat yourself
"Don't repeat yourself" (DRY) is a principle of software development aimed at reducing repetition of information which is likely to change, replacing it with abstractions that are less likely to change, or using data normalization which avoids r ...
".
Deliberate design choice
Copy-and-paste programming is occasionally accepted as a valid programming technique. This is most commonly seen in boilerplate, such as class declarations or importing standard libraries, or in using an existing code template (with empty contents or stub functions) as a framework to fill in.
Use of programming idioms and
design patterns
''Design Patterns: Elements of Reusable Object-Oriented Software'' (1994) is a software engineering book describing software design patterns. The book was written by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, with a fore ...
are similar to copy-and-paste programming, as they also use formulaic code. In some cases, this can be expressed as a
snippet, which can then be pasted in when such code is necessary, though it is often simply recalled from the programmer's mind. In other cases idioms cannot be reduced to a code template. In most cases, however, even if an idiom can be reduced to code, it will be either long enough that it is abstracted into a function or short enough that it can be keyed in directly.
The
Subtext programming language is a research project aimed at "decriminalizing" cut and paste. Using this language, cut and paste is the primary interaction model, and hence not considered an anti-pattern.
Example
A simple example is a for loop, which might be expressed as
for (int i=0; i!=n; ++i) .
Sample code using such a for-loop might be:
void foo(int n)
The looping code could then have been generated by the following snippet (specifying types and variable names):
for ($type $loop_var = 0; $loop_var != $stop; ++$loop_var)
See also
*
Cargo cult programming
*
Vibe coding
Vibe coding is an approach to producing software by using artificial intelligence (AI), where a person describes a problem in a few natural language sentences as a Prompt engineering, prompt to a large language model (LLM) tuned for coding. The L ...
References
External links
*
c2:CopyAndPasteProgramming
* Andrey Karpov
Consequences of using the Copy-Paste method in C++ programming and how to deal with it* Andrey Karpov
The Last Line Effect* PMD'
CPD.
{{DEFAULTSORT:Copy And Paste Programming
Computer programming