Having two WHEN
in the same SELECT
statement or branches in the same IF
structure with the same
implementation is at best duplicate code, and at worst a coding error. If the same logic is truly needed for both instances, they should be
combined.
Noncompliant Code Example
C IF X = 1
C EXSR SR01
C EXSR SR01
C ELSEIF X = 2
C EXSR SR02
C ELSEIF X = 3
C EXSR SR01 Noncompliant; duplicates first condition
C EXSR SR01
C ENDIF
select;
when i = 1;
doFirst();
doSomething();
when i = 2;
doSomethingDifferent();
when i = 3: // Noncompliant; duplicates first when's implementation
doFirst();
doSomething();
endsl;
if (a >= 0 and a < 10);
doFirst();
doTheThing();
elseif (a >= 10 and a < 20);
doTheOtherThing();
elseif (a >= 20 and a < 50);
doFirst(); // Noncompliant; duplicates first condition
doTheThing();
else;
doTheRest();
endif;
Exceptions
Blocks in an if
chain or select
statement that contain a single line of code are ignored.
if (a >= 0 and a < 10);
doTheThing();
elseif (a >= 10 and a < 20);
doTheOtherThing();
elseif (a >= 20 and a < 50);
doTheThing(); //no issue, usually this is done on purpose to increase the readability
else;
doTheRest();
endif;
But this exception does not apply to if
chains without else
-s, or to select
-s without other
clauses when all branches have the same single line of code. In case of if
chains with else
-s, or of select
-s
with other
clauses, rule {rule:rpg:S3923} raises a bug.
if (a >= 0 and a < 10);
doTheThing();
elseif (a >= 20 and a < 50);
doTheThing(); //Noncompliant, this might have been done on purpose but probably not
endif;