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

org.sonar.l10n.java.rules.squid.S1871.html Maven / Gradle / Ivy

Having two cases in the same switch statement with the same implementation is at best duplicate code, and at worst a coding error. If the same logic is truly needed for both cases, then one should fall through to the other.

Noncompliant Code Example

switch (i) {
  case 1:
    doSomething();
    break;
  case 2:
    doSomethingDifferent();
    break;
  case 3:  // Noncompliant; duplicates case 1's implementation
    doSomething();
    break;
}

Compliant Solution

switch (i) {
  case 1:
  case 3:
    doSomething();
    break;
  case 2:
    doSomethingDifferent();
    break;
}
or
switch (i) {
  case 1:
    doSomething();
    break;
  case 2:Check
    doSomethingDifferent();
    break;
  case 3:
    doThirdThing();
    break;
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy