ArticleslgStudy

computer science

Duplicate code

Duplicate code 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 Duplicate code rather than just read about it. In short: In computer programming, duplicate code is multiple occurrences of equivalent source code in a codebase. A duplicate code fragment is also known as a code clone, and the process of finding clones in source code is called clone detection.

Duplicate code — main illustration
Duplicate code — illustration

Key takeaways

  • Duplicate code 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 Duplicate code to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Duplicate code from memory before moving on to harder problems.

Reference excerpt

In computer programming, duplicate code is multiple occurrences of equivalent source code in a codebase. A duplicate code fragment is also known as a code clone, and the process of finding clones in source code is called clone detection. Duplicate code has multiple undesirable aspects. Whether fragments are classified as duplicate can be subjective. Fragments that are very small – such as a single statement – are probably not classified as duplicate. Additionally, fragments that are not exactly the same text might be considered duplicate code if they match except for less important aspects such as whitespace, comments and variable names. Even fragments that are only functionally equivalent may be classified as duplicate.

Cost Code that includes duplicate functionality is more difficult to maintain because if it needs updating, there is a risk that only some of the duplicates will be updated, leaving the others as-is. When code with a vulnerability is duplicated, the vulnerability exists in the duplicate even after it is fixed in one copy. Refactoring to eliminate duplicate code can improve many software metrics, such as lines of code, cyclomatic complexity, and coupling. This may lead to shorter compilation time, lower cognitive load, less human error, and fewer forgotten or overlooked pieces of code. However, not all code duplication can be refactored. Clones may be the most effective solution if the programming language provides inadequate or overly complex abstractions, particularly if supported with user interface techniques such as simultaneous editing. Furthermore, the risk of breaking code when refactoring may outweigh maintenance benefit. A study by Wagner, Abdulkhaleq, and Kaya concluded that while additional work must be done to keep duplicates in sync, if the programmers involved are aware of the duplicate code there weren't significantly more faults caused than in unduplicated code. Another cost is memory size as duplicate code requires memory to store each copy.

Emergence Some practices that lead to duplicate code include:

Scrounging Via copy and paste programming, a section of code is duplicated in the codebase instead of factored it into a reusable function. Code snippets Some development tools like LLMs automate the process of inserting code snippets: code that is identical or that has equivalent functionality. Coincidence Similar code may be developed independently although studies suggest that such code is typically not syntactically similar. Generated Automatically generated code may be duplicate but this may be done for runtime performance or ease of development.

Fixing

Duplicate code is most commonly eliminated by moving the code to a function and replacing each duplicate with a call to that function. For example, the following code calculates the average of an array of integers.

The two loops can be rewritten as the function:

Using this function eliminates the duplicated code.

The compiler might inline the calls such that the resulting machine code is identical for both versions. If the function is not inlined, then the additional overhead of the function calls will take longer to run by a relatively small amount.

Detecting A number of algorithms have been proposed to detect duplicate code. For example:

Baker's algorithm. Rabin–Karp string search algorithm. Using abstract syntax trees. Visual clone detection. Count matrix clone detection. Locality-sensitive hashing Anti-unification

See also Abstraction principle (programming) Anti-pattern – Solution to a problem that may be commonly used but is generally a bad choice Data deduplication – Data processing technique to eliminate duplicate copies of repeating data Don't repeat yourself – Principle of software development List of tools for static code analysis Redundant code – Unnecessary source code Rule of three (computer programming) – Refactoring rule of thumb Snippet (programming) – Small amount of source code used for productivity

References

External links The University of Alabama at Birmingham: Code Clones Literature[link removed] Finding duplicate code in C#, VB.Net, ASPX, Ruby, Python, Java, C, C++, ActionScript, or XAML

Worked examples

Example 1 — a first encounter with Duplicate code

Start with the simplest possible case. Write down what Duplicate code 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 Duplicate code 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 Duplicate code 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 Duplicate code

In research
Duplicate code 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 Duplicate code 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
Duplicate code is common in secondary-school and first-year university syllabi. It links to neighbouring topics Software metrics, Source code, so understanding it makes those chapters shorter.
In everyday life
Look for Duplicate code 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 “Duplicate code” →

How to study Duplicate code in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Duplicate code 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 Duplicate code out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Duplicate code in simple terms?

In computer programming, duplicate code is multiple occurrences of equivalent source code in a codebase. A duplicate code fragment is also known as a code clone, and the process of finding clones in source code is called clone detection.

Why does Duplicate code 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 Duplicate code?

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 Duplicate code.

Tags

  • Software metrics
  • Source code

Keep exploring