2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-07-26 11:24:45 +02:00
A ``++switch++`` statement is a control flow statement that allows you to execute different blocks of code based on the value of an expression. It provides a more concise way to handle multiple conditions compared to using multiple ``++if-else++`` statements.
2023-10-30 10:33:56 +01:00
If you only have a single condition to check, using an ``++if++`` statement is simpler and more concise. ``++switch++`` statements are designed for handling multiple cases, so using them for a single condition can be overkill and less readable.
2023-07-26 11:24:45 +02:00
2023-10-30 10:33:56 +01:00
This rule raises an issue when a ``++switch++`` statement has only one ``++case++`` clause and possibly a ``++default++`` one.
2023-07-26 11:24:45 +02:00
[source,javascript,diff-id=1,diff-type=noncompliant]
----
switch (condition) { // Noncompliant: The switch has only one case and a default
case 0:
doSomething();
break;
default:
doSomethingElse();
break;
}
----
Use a ``++switch++`` statement when you have multiple cases to handle and an ``++if++`` statement when you have only one condition to check.
[source,javascript,diff-id=1,diff-type=compliant]
----
if (condition === 0) {
doSomething();
} else {
doSomethingElse();
}
----
== Resources
=== Documentation
2023-10-19 11:46:59 +02:00
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch[``++switch++``]
* MDN web docs - link:++https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else++[``++if...else++``]
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)
include::../message.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[]
2023-06-22 10:38:01 +02:00
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]