Duplicate Code Detection in Polyspace Bug Finder
Sections of code that do the same operations require unnecessary additional maintenance. Duplicated code also increases the chances you will update the code in one place but forget to update the other. Using Polyspace® Bug Finder™, you can detect sections of code that are exact or partial duplicates of each other.
Duplicate Code Checkers
Polyspace Bug Finder reports duplicate code using these checkers. Using these checkers, you can find opportunities for refactoring your code into more maintainable modules.
Defect | Description | Defect Group |
---|---|---|
Duplicated code | A section of code is duplicated in other places. | Good Practice |
Partially duplicated code | A section of code is duplicated in other places with very minor changes. | Good Practice |
Possible copy-paste error | A section of code is duplicated in other places with exactly one minor change. | Programming |
Limitations of Duplicate Code Detection
The defect checker does not flag certain blocks as duplicates. For instance:
The blocks of code considered duplicates have to typically consist of more than six consecutive lines. This restriction is put in place to prevent excess false positives. For instance, if a single line or a couple of lines is repeated in multiple places, this repetition is not an issue.
In large functions, the checker does not detect duplicate code sections containing the following constructs:
Conditional statements –
if
,switch
, and so on.Loops –
for
,while
, and so on.
The checker detects only those duplicate code sections that do not involve branching.
The checker resolves preprocessor directives such as
#ifdef
-s before analyzing the code for duplicate blocks. For instance, depending on whether the macroPCDEBUG
is defined or not, the lines in the following#ifdef
form part of the analysis or get removed from the analysis:#ifdef PCDEBUG //Block of code #endif