rspec/rules/S1871/abap/rule.adoc

53 lines
1.5 KiB
Plaintext
Raw Normal View History

2020-12-23 14:59:06 +01:00
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.
2020-06-30 12:47:33 +02:00
== 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
2020-12-23 14:59:06 +01:00
Blocks in an ``IF`` chain or in ``CASE`` statement that contain a single line of code are ignored.
2020-06-30 12:47:33 +02:00
----
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.
----
2020-12-23 14:59:06 +01:00
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 S3923 raises a bug.
2020-06-30 12:47:33 +02:00
----
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.
----