Main Content

Duplicated code

A section of code is duplicated in other places

Since R2023a

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

expand all

This example contains two blocks of code that are exact duplicates of each other.

typedef struct {
    int pin1;
    int pin2;
    int pin3;
    int pin4;
    int pin5;
    int pin6;
    int pin7;
    int pin8;
} board;

board Aboard;
extern int getPinVal();

void setAllPins() {
     int val1 = getPinVal();
     int val2 = getPinVal();
     int val3 = getPinVal();
     int val4 = getPinVal();
     int val5 = getPinVal();
     int val6 = getPinVal();
     int val7 = getPinVal();
     int val8 = getPinVal();
     Aboard.pin1 = val1; //Beginning of duplicate section #1
     Aboard.pin2 = val2;
     Aboard.pin3 = val3;
     Aboard.pin4 = val4;
     Aboard.pin5 = val5;
     Aboard.pin6 = val6;
     Aboard.pin7 = val7;
     Aboard.pin8 = val8; //End of duplicate section #1

}

void resetAllPins() {
     int val1 = 0, val2 = 1, val3 = 0,
         val4 = 0, val5 = 1, val6 = 0,
         val7 = 0, val8 = 1;
     Aboard.pin1 = val1; //Beginning of duplicate section #2
     Aboard.pin2 = val2;
     Aboard.pin3 = val3;
     Aboard.pin4 = val4;
     Aboard.pin5 = val5;
     Aboard.pin6 = val6;
     Aboard.pin7 = val7;
     Aboard.pin8 = val8; //End of duplicate section #2
}

The event list on the Result Details pane shows the beginning and end of each block of code. Click on an event to navigate to the corresponding location in the source code.

Event list shows beginning and end of each duplicate section

In the Polyspace® user interface, you can see the duplicated code blocks side by side on the Source pane. See Navigate in Separate Window.

Correction – Refactor Duplicate Sections Into Common Function

You can refactor the duplicate sections of code into a common function and replace the sections with calls to this function. In this example, the duplicate block has been refactored into the init() function.

typedef struct {
    int pin1;
    int pin2;
    int pin3;
    int pin4;
    int pin5;
    int pin6;
    int pin7;
    int pin8;
} board;

board Aboard;
extern int getPinVal();

void init(int val1, int val2, int val3, int val4,
          int val5, int val6, int val7, int val8) {
     Aboard.pin1 = val1;
     Aboard.pin2 = val2;
     Aboard.pin3 = val3;
     Aboard.pin4 = val4;
     Aboard.pin5 = val5;
     Aboard.pin6 = val6;
     Aboard.pin7 = val7;
     Aboard.pin8 = val8; 
}

void setAllPins() {
     int val1 = getPinVal();
     int val2 = getPinVal();
     int val3 = getPinVal();
     int val4 = getPinVal();
     int val5 = getPinVal();
     int val6 = getPinVal();
     int val7 = getPinVal();
     int val8 = getPinVal();
     init(val1, val2, val3, val4, val5, val6, val7, val8);
}

void resetAllPins() {
     int val1 = 0, val2 = 1, val3 = 0,
         val4 = 0, val5 = 1, val6 = 0,
         val7 = 0, val8 = 1;
     init(val1, val2, val3, val4, val5, val6, val7, val8);
}

Result Information

Group: Good practice
Language: C | C++
Default: Off
Command-Line Syntax: DUPLICATED_CODE
Impact: Low

Version History

Introduced in R2023a

expand all