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

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

A chain of if/else if statements is evaluated from top to bottom. At most, only one branch of the chain will be executed: the first one with a condition that evaluates to true.

Therefore, duplicating a condition in a sequence of if/else if statements automatically leads to dead code. Usually, is due to a copy/paste error. Obviously it could lead to unexpected behavior.

Noncompliant Code Sample

if (param == 1)
  openWindow();
else if (param == 2)
  closeWindow();
else if (param == 1)  // For sure this is a bug
  moveWindowToTheBackground();

Compliant Solution

if (param == 1)
  openWindow();
else if (param == 2)
  closeWindow();
else if (param == 3)
  moveWindowToTheBackground();




© 2015 - 2025 Weber Informatics LLC | Privacy Policy