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

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

There is a newer version: 8.6.0.37351
Show newest version

Labels are not commonly used in Java, and many developers do not understand how they work. Moreover, their usage make the control flow harder to follow, which reduces the code's readability.

The following code:

int matrix[][] = {
  {1, 2, 3},
  {4, 5, 6},
  {7, 8, 9}
};

outer: for (int row = 0; row < matrix.length; row++) {   // Noncompliant
  for (int col = 0; col < matrix[row].length; col++) {
    if (col == row) {
      continue outer;
    }
    System.out.println(matrix[row][col]);                // Prints the elements under the diagonal, i.e. 4, 7 and 8
  }
}

should be refactored into:

for (int row = 1; row < matrix.length; row++) {          // Compliant
  for (int col = 0; col < row; col++) {
    System.out.println(matrix[row][col]);                // Also prints 4, 7 and 8
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy