2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-03-14 14:27:46 +01:00
A chain of ``++if-else-if++`` or ``++switch-case++`` statements is evaluated from top to bottom. At most, only one branch will be executed: the first one with a condition that evaluates to ``++true++`` or that matches the discriminant of the ``++switch++``.
2020-06-30 12:47:33 +02:00
2021-02-02 15:02:10 +01:00
2020-06-30 12:47:33 +02:00
Therefore, duplicating a condition automatically leads to dead code. Usually, this is due to a copy/paste error. At best, it's simply dead code and at worst, it's a bug that is likely to induce further bugs as the code is maintained, and obviously it could lead to unexpected behavior.
2021-02-02 15:02:10 +01:00
2020-06-30 12:47:33 +02:00
2021-02-02 15:02:10 +01:00
2020-06-30 12:47:33 +02:00
_Note that this rule requires Node.js to be available during analysis._
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2020-06-30 12:47:33 +02:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2020-06-30 12:47:33 +02:00
----
if (param == 1)
openWindow();
else if (param == 2)
closeWindow();
else if (param == 1) // Noncompliant
moveWindowToTheBackground();
2023-03-14 14:27:46 +01:00
switch (param) {
case 1:
openWindow();
break;
case 2:
closeWindow();
break;
case 1: // Noncompliant
moveWindowToTheBackground();
break;
}
2020-06-30 12:47:33 +02:00
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2020-06-30 12:47:33 +02:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2020-06-30 12:47:33 +02:00
----
if (param == 1)
openWindow();
else if (param == 2)
closeWindow();
else if (param == 3)
moveWindowToTheBackground();
2023-03-14 14:27:46 +01:00
switch (param) {
case 1:
openWindow();
break;
case 2:
closeWindow();
break;
case 3:
moveWindowToTheBackground();
break;
}
2020-06-30 12:47:33 +02:00
----
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
This branch duplicates the one on line n.
2021-09-20 15:38:42 +02:00
include::../highlighting.adoc[]
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]