Having two WHEN
in a CASE
statement or two branches in an IF
chain with the same implementation is at best
duplicate code, and at worst a coding error. If the same logic is truly needed for both cases, they should be combined.
Noncompliant Code Example
CASE i.
WHEN 1.
doFirst.
doSomething.
WHEN 2.
doSomethingDifferent.
WHEN 3. // Noncompliant; duplicates case 1's implementation
doFirst.
doSomething.
WHEN OTHERS.
doTheRest.
ENDCASE.
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.
ENDIF.
Exceptions
Blocks in an IF
chain or in CASE
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
ENDIF.
But this exception does not apply to IF
chains without final ELSE
-s, or to CASE
-s without WHEN
OTHERS
clauses when all branches have the same single line of code. In case of IF
chains with ELSE
-s, or of
CASE
-s with WHEN OTHERS
clauses, rule {rule:abap:S3923} raises a bug.
if a >= 0 AND a < 10. //Noncompliant, this might have been done on purpose but probably not
doTheThing.
elseif a >= 10 AND a < 20.
doTheThing.
endif.