ArticleslgStudy

computer science

Copy-and-paste programming

Copy-and-paste programming is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Copy-and-paste programming rather than just read about it. In short: 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.

Copy-and-paste programming — main illustration
Copy-and-paste programming — illustration

Key takeaways

  • Copy-and-paste programming belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Copy-and-paste programming to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Copy-and-paste programming from memory before moving on to harder problems.

Reference excerpt

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 (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 (when not supported automatically by the compiler), languages with limited metaprogramming facilities, or certain programming idioms, and it is supported by some source code editors 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 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, 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 algorithms that are easily adapted to specific tasks. Being a form of code duplication, 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 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. The class is written flexibly, with full support of inheritance 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). This way, if the original algorithm has a bug to fix or can be improved, all software using it stands to benefit. Generic programming 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 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 the existing product, saving on QA time associated with the new product launch, and reducing time to market. 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 and manual merging down the line. The duplicate code 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:

… excerpt ends here. Continue reading the full article.

Illustrations

Copy-and-paste programming: The Programmer's keyboard only needs three keys: .mw-parser-output .keyboard-key{border:1px solid var(--border-color-base,#a2a9b1);border-radius:0.2em;box-shadow:0.1em 0.1em 0.2em rgba(0,0,0,0.1);background-color:var(--background-color-neutral-subtle,#f8f9fa);background-image:linear-gradient(to bottom,var(--background-color-neutral,#eaecf0),var(--background-color-neutral-subtle,#f8f9fa),var(--background-color-neutral,#eaecf0));color:var(--color-base,#202122);padding:0.1em 0.3em;font-family:inherit;font-size:0.85em}C, V, and Ctrl. It's funny because programmers always copy and paste.
The Programmer's keyboard only needs three keys: .mw-parser-output .keyboard-key{border:1px solid var(--border-color-base,#a2a9b1);border-radius:0.2em;box-shadow:0.1em 0.1em 0.2em rgba(0,0,0,0.1);background-color:var(--background-color-neutral-subtle,#f8f9fa);background-image:linear-gradient(to bottom,var(--background-color-neutral,#eaecf0),var(--background-color-neutral-subtle,#f8f9fa),var(--background-color-neutral,#eaecf0));color:var(--color-base,#202122);padding:0.1em 0.3em;font-family:inherit;font-size:0.85em}C, V, and Ctrl. It's funny because programmers always copy and paste.
Copy-and-paste programming: Repetitive code being refactored by using an abstraction mechanism such as a function.
Repetitive code being refactored by using an abstraction mechanism such as a function.
Copy-and-paste programming: Difficulty and risk of maintaining code written by copy-paste programming
Difficulty and risk of maintaining code written by copy-paste programming

Worked examples

Example 1 — a first encounter with Copy-and-paste programming

Start with the simplest possible case. Write down what Copy-and-paste programming claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Copy-and-paste programming before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Copy-and-paste programming ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Copy-and-paste programming

In research
Copy-and-paste programming appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Copy-and-paste programming in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Copy-and-paste programming is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer programming, so understanding it makes those chapters shorter.
In everyday life
Look for Copy-and-paste programming outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Copy-and-paste programming” →

How to study Copy-and-paste programming in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Copy-and-paste programming means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Copy-and-paste programming out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Copy-and-paste programming in simple terms?

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…

Why does Copy-and-paste programming matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Copy-and-paste programming?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Copy-and-paste programming.

Tags

  • Computer programming

Keep exploring