All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.javascript.rules.javascript.S1301.html Maven / Gradle / Ivy

There is a newer version: 10.20.0.29356
Show newest version

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.

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.

if (condition === 0) {
  doSomething();
} else {
  doSomethingElse();
}

Resources

Documentation





© 2015 - 2025 Weber Informatics LLC | Privacy Policy