Partially duplicated code
A section of code is duplicated in other places with very minor changes
Since R2023a
Description
This defect occurs when a block of code is duplicated in multiple places with only slight changes.
The defect checker does not flag certain blocks as duplicates. For instance, the blocks of code considered almost duplicates have to typically consist of more than a certain number of lines. See also Duplicate Code Detection in Polyspace Bug Finder.
Sometimes, two blocks might appear as exact duplicates, but might be reported as partial duplicates because some of the variables in the duplicate sections have different data types. The event list below the result points to the lines containing such variables.
Risk
Sections of code that do almost the same operations might require unnecessary additional maintenance. Duplicated code also increases the chances you will update the code in one place but forget to update the other. See also Duplicated code
and Possible copy-paste error
.
Fix
Try to refactor the sections of code to reduce the duplication. For instance, identify parts of the code blocks that are duplicates of each other and refactor them into a dedicated function. You can then replace the duplicated code with calls to the dedicated function.
Examples
Result Information
Group: Good practice |
Language: C | C++ |
Default: Off |
Command-Line Syntax:
ALMOST_DUPLICATED_CODE |
Impact: Low |
Version History
Introduced in R2023aSee Also
Find defects (-checkers)
| Duplicated code
| Possible copy-paste error
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)