Skip to content

Deep Nesting (ARCH6184)

Key ruby.quality.deep-nesting · Severity warning · Category quality

if/while/begin blocks nested beyond the threshold produce hard-to-read, hard-to-trace flow and can usually be fixed with early returns or extraction.

def grade(user)
if user
if user.active?
if user.score > 50
return "pass"
end
end
end
"fail"
end
def grade(user)
return "fail" unless user&.active?
return "fail" unless user.score > 50
"pass"
end

Return early with guard clauses or extract nested blocks into separate methods to flatten the depth.