Skip to main content

Command Palette

Search for a command to run...

SonarLint : Java - Collapsible "if" statements should be merged (squid:S1066)

Published
1 min read
SonarLint : Java - Collapsible "if" statements should be merged (squid:S1066)
S

I love learning about technology and sharing that with others

in some cases are multiple if statement whereas both the if statement can be merged together , so collapsible if statement should be merged it will improve the readability and maintainability of the codebase.

Non Compliant Code

image.png

Compliant Code

screenshot_15.png

Example from sonar Source

Noncompliant Code Example

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

Compliant Solution

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

private static boolean isFileOrDirectory(File file) {
  return file.isFile() || file.isDirectory();
}
24 views

More from this blog

H

hashcodehub

271 posts

Consistent, Passionate and Organized :)