rspec/rules/S1871/rpg/rule.adoc

76 lines
2.2 KiB
Plaintext
Raw Normal View History

2021-01-27 13:42:22 +01:00
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.
2020-06-30 12:47:33 +02:00
== 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
2021-01-27 13:42:22 +01:00
Blocks in an ``++if++`` chain or ``++select++`` 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
else;
doTheRest();
endif;
----
2021-01-27 13:42:22 +01:00
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 S3923 raises a bug.
2020-06-30 12:47:33 +02:00
----
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;
----
ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]