Duplicated code
Description
This defect occurs when a block of code is duplicated in multiple places.
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.
Risk
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. See also Almost duplicated code
and Possible copy-paste error
.
Fix
Refactor the sections of code into a dedicated function. In other words, if two blocks of code are duplicates of each other, write a new function containing this code block and replace the existing blocks with calls to the new function.
Examples
Result Information
Group: Good practice |
Language: C | C++ |
Default: Off |
Command-Line Syntax: DUPLICATED_CODE |
Impact: Low |
Version History
Introduced in R2023aSee Also
Find defects (-checkers)
| Possible copy-paste error
| Partially duplicated code
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)