rspec/rules/S1871/abap/rule.adoc

72 lines
1.8 KiB
Plaintext
Raw Normal View History

2021-01-27 13:42:22 +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
2022-02-04 17:28:24 +01:00
[source,abap]
2020-06-30 12:47:33 +02:00
----
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
2021-01-27 13:42:22 +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
2021-02-02 15:02:10 +01:00
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.
----
2021-01-27 13:42:22 +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.
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
include::../highlighting.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]