Find duplicate code

Enterprise Edition only

Duplicate code analysis (in the Enterprise menu) searches for repeated code blocks, including identical procedures and shorter code snippets. Use this analysis to decrease the reduncancy related to copy & paste coding.

Why be concerned about duplicated code?

Copy & paste coding can result in the same code being repeated with few or no modifications. Duplicate code analysis finds blocks of code that exist at two or more locations across the project or solution. A duplicated block can consist of a few lines, complete procedures or even entire modules. You may also find repeated declarations for constants, API procedures or user-defined types.

Use duplicate code analysis to eliminate redundant code and detect possible logical errors. The elimination of unnecessary duplicates yields the following benefits:

You can eliminate repeated code snippets by turning them into procedures. If entire procedures are repeated, you can usually delete the duplicate(s) and keep just one of them.

Sometimes you may find repeated code at a place where different code should actually have been used. Two routines that should perform a different thing may inadvertently contain the same code.

How the duplicate code analysis works

The analysis attempts to locate identical lines of code. If it finds an exact match longer than a predefined minimum number of lines, it is reported as a duplicated block. Any difference, even if it's just one character, will end the duplicated block.

Whitespace (empty lines and indentation) is ignored. Two blocks that only differ by the use of indentation or empty lines are considered similar.

The comparison is case-insensitive. As an exception, text inside string literals is treated in a case-sensitive way.

Minimum number of lines to match. This is the minimum number of matches required before a duplicated block is reported. A low limit may turn out a lot of short repeated blocks that cannot be eliminated.

Ignore comments. Use this option to work strictly on code. All comments are skipped. Thus, two blocks that contain the same code but different comments are considered similar. Uncheck this option to require the comments to match.

Ignore line numbers and line labels. Use this option if your project contains large sections of line numbered code. It allows you to find matches that differ only by the use of line numbers (or line labels). VB code does not usually utilize line numbers so most users can leave this option unchecked to make the analysis run a little faster.

Viewing the results

Click the buttons View 1 and View 2 to see the matching locations in the hypertext view.

The number of matching lines is shown in its own column. This number includes only those lines that were considered in the analysis. It does not count any ignored lines such as empty lines or comment lines.

Get a Duplicate code report by clicking the Report button. This report lists the matching blocks after each other so that you can consider what to do about the duplicates. The report includes a Summary section with duplication rate and other statistics.

Miscellaneous

A project may contain triplicates or even more occurrences of the same code. This analysis does a pairwise comparison. This means that you may see the same block reported a large number of times if it exists at multiple locations. If your code is likely to contain a lot of entire repeated procedures, you might want to spot them via the alphabetically sorted procedure list (in the Report menu).

It's usual for large projects to contain a considerable amount of short repeated blocks. It's recommended that you first set the minimum number of matching lines to a large number. You can then gradually decrease the limit until you find a reasonable result set to work with.

Not all possible duplicated blocks are reported. The analysis discards some blocks (such as repetitive End If lines) that do not represent problematic repetition.

Because a very large number of line-by-line comparisons is required to find all possible matches, this feature can take a long time to complete. Duplicate code analysis is available after analysis phase 1/2 is complete.

© Project Analyzer Help