53 lines
1.6 KiB
Plaintext
53 lines
1.6 KiB
Plaintext
== Why is this an issue?
|
|
|
|
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.
|
|
|
|
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.
|
|
|
|
This rule raises an issue when a ``++switch++`` statement has only one ``++case++`` clause and possibly a ``++default`` one.
|
|
|
|
[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
|
|
|
|
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch[MDN - ``++switch++``]
|
|
* link:++https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else++[MDN - ``++if...else++``]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::../message.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::../comments-and-links.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|