8.13.  Detecting Duplicate Code

Duplicate code analysis in Sonargraph is achieved through the Duplicate Blocks view and the Duplicates Source View. The Duplicate Blocks view lists duplicate code blocks that have been found in source files of the system that have not been excluded from analysis via a filter. For each duplicate block, all the occurrences are listed, with source file, length of the block in lines, start line of the block, and the tolerance, i.e., a number of lines that are different to another text block.

Duplicates Source View
Figure 8.55. Duplicate Blocks View

Duplicate code blocks are considered as issues in Sonargraph as they make the maintenance of the code base more difficult. Thus, duplicate code blocks can also be found in the form of Issues in the Issues view of Sonargraph (See Section 9.2, “ Examining Issues ” ). The context menu for a duplicate block (both in the Duplicate Blocks view and the Duplicates Source view) allows to take care of it as an issue, by either ignoring it or creating a fix resolution for it.

By double-clicking (or selecting "Show In Duplicates Source View" in the context menu) on a line that represents a duplicate code block, one jumps to the Duplicates Source View where the selected occurrence is presented side by side along with the next occurrence in the block so that the similarities and differences can be appreciated:

Duplicates Source View
Figure 8.56. Duplicates Source View

Normally, duplicate code blocks are computed automatically on every software system open or refresh via the duplicate code analyzer.