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

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

There is a newer version: 10.17.0.28100
Show newest version

Why is this an issue?

Nested code - blocks of code inside blocks of code - is eventually necessary, but increases complexity. This is why keeping the code as flat as possible, by avoiding unnecessary nesting, is considered a good practice.

Merging if statements when possible will decrease the nesting of the code and improve its readability.

Code like

if (x != undefined) {
  if (y === 2) {
    // ...
  }
}

Will be more readable as

if (x != undefined && y === 2) {
  // ...
}

How to fix it

If merging the conditions seems to result in a more complex code, extracting the condition or part of it in a named function or variable is a better approach to fix readability.

Code examples

Noncompliant code example

if (file != undefined) {
  if (file.isFile() || file.isDirectory()) {        // Noncompliant
    /* ... */
  }
}

Compliant solution

function isFileOrDirectory(File file) {
  return file.isFile() || file.isDirectory();
}

/* ... */

if (file. != undefined && isFileOrDirectory(file)) { // Compliant
  /* ... */
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy