Rules / copy-paste
Copy-pasted logic in a PLC program: why duplication is risky
Structurally identical blocks (even with renamed tags) mean every fix must be applied to each copy, and one copy usually gets missed. A shared FUNCTION_BLOCK with parameters removes the duplication.
Severity: Minor
Rule id: copy-paste
Duplicated logic block (copy-paste)
What the rule checks
Structurally identical blocks (even with renamed tags) are a source of bugs: a fix in one place is forgotten in the others, and copy-pasting someone else's loop drags along its gating flags. A candidate for a shared parameterised FB.
What it does to the plant
A defect is fixed in one loop and stays in the next one; a copied loop also drags along the interlock flags of the original.
Example
PROGRAM PumpsVAR xRun1, xRun2, xFault1, xFault2, xOut1, xOut2 : BOOL; rP1, rP2, rSp1, rSp2 : REAL; xLow1, xLow2 : BOOL;END_VARIF xRun1 AND NOT xFault1 THEN xOut1 := TRUE; IF rP1 < rSp1 THEN xLow1 := TRUE; ELSE xLow1 := FALSE; END_IFELSE xOut1 := FALSE;END_IFIF xRun2 AND NOT xFault2 THEN xOut2 := TRUE; IF rP2 < rSp2 THEN xLow2 := TRUE; ELSE xLow2 := FALSE; END_IFELSE xOut2 := FALSE;END_IF
FUNCTION_BLOCK FB_PumpVAR_INPUT xRun, xFault : BOOL; rP, rSp : REAL;END_VARVAR_OUTPUT xOut, xLow : BOOL;END_VARxOut := xRun AND NOT xFault;xLow := xOut AND rP < rSp;PROGRAM PumpsVAR fbPump1, fbPump2 : FB_Pump; xRun1, xRun2, xFault1, xFault2 : BOOL; rP1, rP2, rSp1, rSp2 : REAL;END_VARfbPump1(xRun := xRun1, xFault := xFault1, rP := rP1, rSp := rSp1);fbPump2(xRun := xRun2, xFault := xFault2, rP := rP2, rSp := rSp2);
Both listings are run through the linter by an automated test: the rule fires on the first and stays silent on the second.
How to fix
- Create one FUNCTION_BLOCK with input parameters (setpoints, loop tags) and instantiate it per loop.
- For per-channel repeats, use a FOR loop over an array of structures.
Upload a PLCopen XML or CODESYS .export file: the report is free and the code is not stored.
The same linter without uploading a file: command line and GitHub Action.
Related rules
2026-11-26