Redundant if/else Returning Booleans

ID

javascript.redundant_if

Severity

low

Remediation Complexity

auto_fix

Remediation Risk

low

Remediation Effort

low

Resource

Code Smell

Language

JavaScript

Tags

code-smell, simplification

Description

Reports an if/else where each branch is a single return of a boolean literal with opposite values:

// Bad
if (cond) return true;
else      return false;

// Bad
if (cond) {
  return false;
} else {
  return true;
}

Rationale

The structure adds five lines of indirection for what amounts to "return the truth value of the condition". Reading code is faster when one expression says what the conditional says in five.

Remediation

Return the condition (or its negation) directly. Use !! if you want to convert a truthy non-boolean to an exact boolean:

// Good
return !!cond;
return !cond;

If the condition is already known to be a boolean, the !! is unnecessary:

return cond;

Notes

The rule deliberately does not fire when the branches return the same value, when one branch contains additional statements, when the else branch is itself another if, or when the else is missing — those shapes are not equivalent to a single boolean expression.