rspec/rules/S1871/abap/rule.adoc

52 lines
1.6 KiB
Plaintext
Raw Normal View History

2020-06-30 12:47:33 +02:00
Having two <code>WHEN</code> in a <code>CASE</code> statement or two branches in an <code>IF</code> 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 <code>IF</code> chain or in <code>CASE</code> 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 <code>IF</code> chains without final <code>ELSE</code>-s, or to <code>CASE</code>-s without <code>WHEN OTHERS</code> clauses when all branches have the same single line of code. In case of <code>IF</code> chains with <code>ELSE</code>-s, or of <code>CASE</code>-s with <code>WHEN OTHERS</code> clauses, rule 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.
----