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 a then
unless b then # Noncompliant
# ...
end
end
Will be more readable as
if a && !b then # Compliant # ... end
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.
if !filename.nil?
if File.file?(filename) || File.directory?(filename) # Noncompliant
# ...
end
end
def isFileOrDirectory(filename) File.file?(filename) || File.directory?(filename) end # ... if !filename.nil? && isFileOrDirectory(filename) # Compliant # ... end