Although modern programming languages offer various abstraction mechanisms to facilitate reuse of code fragments, copy-paste is still a widely used reuse strategy. This often leads to numerous duplicated code fragments -so called clones- in large software systems. However, cloning is problematic for software maintenance for several reasons:

  • Cloning unnecessarily increases program size. Since many maintenance efforts correlate with program size, this increases the maintenance effort.
  • Changes to one clone, such as bug fixing, typically need to be made to the other clones as well, again increasing maintenance effort.
  • If changes to duplicated source code fragments are performed inconsistently, this can introduce bugs.

Here is a short code-cloning example from the NUnit project. It shows two duplicated code fragments from the file ProjectEditor.cs.

Clone in NUnit

ConQAT contains a code_clones bundle that implements an efficient clone detection algorithm. In order to maximise precision and recall (see here for a definition of these terms), clone detection can be tailored to specific programming languages or project characteristics. The ConQAT clone detection bundle offers various ways to trim a clone detection to your needs. Clone detection tailoring is however out of the scope of this tutorial. Instead, this tutorial explains the use of the clone detection blocks for C# and Java that offer general purpose clone detection for these languages.

Since autonomy is one of the main design goals behind ConQAT, clone detection does not require user interaction. Instead, ConQAT creates a report that summarizes the analysis results. However, sometimes understanding of analysis results is better served with an interactive tool. For this purpose, ConQAT can be used for interactive visualisation of clone detection results.

Clone Detection for Java

The code_clones bundle contains the JavaCloneAnalysis block that realizes a clone detection for Java. Its use consists of three simple steps:

  • Load the Java source code to analyze
  • Perform clone detection using JavaCloneChain
  • Write result report

Configuration File

The examples project (see Getting Started) contains a prepared configuration file clonedetection-example-junit.cqr for the JUnit project:

Configuration for clone detection in JUnit

To run on your own code or change the minimal length of a copied code fragment to be detected, just adjust the parameters of the run configuration.

Analysis Results

Running the configuration file creates a HTML dashboard that looks similar to the following image. The navigation on the left provides links to several lists of the detected clones and a page displaying the clone coverage metric as a treemap. This treemap displays the distribution of cloned code in the project. Each rectangle represents a source file. The rectangle’s size corresponds to the size of the source file (in lines of code). The color represents the percentage of lines that are part of a clone. If you move the mouse over the treemap, you get a tooltip that displays the class name and its metrics.

Resulting dashboard for JUnit clone
detection

Apart from the HTML pages, ConQAT creates an HTML file clone_report.xml with the analysis results. This file can be opened with ConQAT’s Eclipse integration using the menu entry Clone Detection -> Open Clone Report from Eclipse.

Clone Detection for C#

Clone detection on C# source code is very similar to the Java example. The code_clones bundle contains a block CsCloneAnalysis that offers the same functionality for C#. The configuration file in the examples is named clonedetection-example-nunit.cqr and runs on the source code of the NUnit project.