Rules / routine-size
PLC routine too long: when to split it into function blocks
A routine with more than 120 lines of logic is hard to read and to change as a whole. It usually contains repeated sections that become a single FUNCTION_BLOCK or a loop over an array.
Severity: Minor
Rule id: routine-size
Bloated routine (decomposition candidate)
What the rule checks
An overly long routine is hard to read and edit. Extract the repeated logic into a FUNCTION_BLOCK: shorter and reusable.
What it does to the plant
A change to one loop affects its neighbour, and a defect in a long listing slips through review.
Example
PROGRAM ZonesVAR aTemp, aSet : ARRAY[1..32] OF REAL; aHeat : ARRAY[1..32] OF BOOL;END_VARIF aTemp[1] < aSet[1] - 1.0 THEN aHeat[1] := TRUE;ELSIF aTemp[1] > aSet[1] + 1.0 THEN aHeat[1] := FALSE;END_IFIF aTemp[2] < aSet[2] - 1.0 THEN aHeat[2] := TRUE;ELSIF aTemp[2] > aSet[2] + 1.0 THEN aHeat[2] := FALSE;END_IF(* … *)IF aTemp[25] < aSet[25] - 1.0 THEN aHeat[25] := TRUE;ELSIF aTemp[25] > aSet[25] + 1.0 THEN aHeat[25] := FALSE;END_IF
PROGRAM ZonesVAR aTemp, aSet : ARRAY[1..32] OF REAL; aHeat : ARRAY[1..32] OF BOOL; i : INT;END_VARFOR i := 1 TO 32 DO IF aTemp[i] < aSet[i] - 1.0 THEN aHeat[i] := TRUE; ELSIF aTemp[i] > aSet[i] + 1.0 THEN aHeat[i] := FALSE; END_IFEND_FOR
The listing is shortened: the check runs on all 130 lines. 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
- Extract the repeated parts into a parameterised FUNCTION_BLOCK.
- Split by zones (control / protections / metering) into separate FBs or methods.
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