Course contentsShow
← JavaScript
Lesson 318 of 2,7239. Control FlowFree lesson

Common If/Else Antipatterns

Identify unnecessary else clauses, boolean return redundancies, and other if/else code smells to avoid.

Common If/Else Antipatterns

What you'll learn: How to recognize and avoid unnecessary complexity in if/else statements that make code harder to read and maintain.

What Are If/Else Antipatterns?

An antipattern is a common solution that looks reasonable but actually creates more problems than it solves. In if/else statements, these often appear as unnecessary clauses, redundant logic, or overly complex conditions that obscure your code's intent.

Unnecessary Else After Return

One of the most common antipatterns is using else after a return statement:

// ❌ Antipattern: unnecessary else
if (age < 18) {
    return "minor";
} else {
    return "adult";
}

// ✅ Better: the else is redundant
if (age < 18) {
    return "minor";
}
return "adult";

Since return exits the function, the else is unnecessary—the second return only runs if the condition was false.

Boolean Return Redundancy

Another antipattern is explicitly returning true or false based on a condition:

// ❌ Antipattern: redundant if/else for boolean
if (score >= 60) {
    return true;
} else {
    return false;
}

// ✅ Better: just return the expression
return score >= 60;

The comparison already produces a boolean—wrapping it in if/else adds no value.

Negation Instead of Swapping Branches

Sometimes developers negate conditions when swapping the if/else branches would be clearer:

// ❌ Harder to read
if (!isValid) {
    // 20 lines of error handling
} else {
    doWork();
}

// ✅ Better: positive condition first (when practical)
if (isValid) {
    doWork();
} else {
    // 20 lines of error handling
}

Key Takeaway: Avoid unnecessary else clauses after returns, don't wrap boolean expressions in if/else to return true/false, and prefer positive conditions when they improve readability—cleaner conditionals make your intent obvious at a glance.