Simplify Boolean Return

ID

java.simplify_boolean_return

Severity

info

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Java

Tags

code-style

Description

Reports if (cond) return true; else return false; patterns that should be simplified to return cond;.

Rationale

An if statement that returns true in one branch and false in the other is a verbose way of returning a boolean expression. The branching adds visual noise without adding any semantic value.

// Bad -- unnecessarily verbose
if (x > 0) {
    return true;
} else {
    return false;
}

Remediation

Return the boolean expression directly:

// Good -- direct return
return x > 0;

// Or, if the branches are inverted:
return !(x > 0);

References