Code Duplicate

What are code duplicates, how do they arise and why should they be eliminated?

Identical or similar source code

Code Duplicate means source code which is used in identical form several times within a software. Similar code sections or fragments are also referred to as code duplicates – alternatively also as software clones or source code clones. Code duplicates occur when existing functionalities are copied from one place to another within a software. This is also called copy & paste programming.

Reasons for code duplicates

There are several reasons that lead to code duplicates:

  • Existing code is copied in order to adapt it elsewhere (e.g. by renaming variables or deleting or adding lines of code) or to test it.
  • Functioning code is reused to minimise the risk of new errors.
  • Developers are under time pressure and want to use existing code to save implementation time.
  • Developers do not have the knowledge to produce a specific code and therefore duplicate existing code.
    Code is generated automatically, e.g. by Model Driven Development or Software Factories.

The use of libraries can also lead to the creation of software clones.

Reasons for eliminating code duplicates

In the course of refactoring – which addresses any code smell – organisations often attempt to resolve code duplicates in order to

  • reduce maintenance costs. A source code clone must be read and understood at every point of use. In contrast to other lines of code to which this also applies, it is important to find out whether minor differences (e.g. differences in identifiers, the use of gaps or comments) are intended. Once optimizations have been identified, they must be implemented at all points of use.
  • to facilitate bugfixing. It is easy to overlook source clones and errors copied with them. This results in inconsistent changes.
  • Minimise memory requirements. Cloning increases the amount of code and thus the amount of memory required. This is particularly critical in the area of embedded systems, as there is usually only little memory available. In addition, duplication also increases the time required for compilation.
  • to facilitate the readability of code.

When identifying code duplicates – also known as clone detection – tools can help with both textual and lexical or abstract analysis. The use of metrics is also often supported by tools.

Software clones can be resolved with the help of abstractions (e.g. by outsourcing recurring algorithms into procedures or methods) or by using a common base class. If this is not possible so easily and quickly, it is recommended to “observe” the code duplicate in order to at least avoid further, unwanted inconsistent changes.

We like to support you.

Software Development from Berlin


Here you will find additional information from our Smartpedia section:

Smartpedia: What is a Bug Fix?

What is a Bug Fix?

Smartpedia: How does Refactoring work?

How does Refactoring work?