Course contentsShow
← Computer Science
Lesson 2324 of 2,87252. Clean Code and RefactoringFree lesson

Consolidate Conditional Expressions

Learn to combine multiple conditional checks into single, well-named boolean expressions or methods.

Consolidate Conditional Expressions

What you'll learn: How to simplify code by merging multiple related conditional checks into a single, clearly-named expression.


The Problem: Scattered Logic

When multiple if statements check related conditions but lead to the same outcome, your code becomes cluttered and hard to understand. Each separate check forces readers to mentally track multiple branches, even when they all mean the same thing logically.

Think of it like asking "Do I need an umbrella?" by separately checking: Is it raining? Are there dark clouds? Did the forecast say rain? Is the ground wet? Instead of four separate questions, you could ask one: "Are conditions rainy?"

The Refactoring

Consolidate Conditional Expressions means combining multiple conditional checks into a single boolean expression or extracting them into a well-named method. This makes the intent obvious and reduces duplication.

Before consolidation:

if (age < 18) return false;
if (hasParentalConsent == false) return false;
if (hasGuardianSignature == false) return false;

After consolidation:

if (isNotEligibleForAccount()) return false;

// Extracted method:
function isNotEligibleForAccount() {
    return age < 18 || !hasParentalConsent || !hasGuardianSignature;
}

Or simply inline if straightforward:

if (age < 18 || !hasParentalConsent || !hasGuardianSignature) {
    return false;
}

When to Apply

Use this refactoring when:

  • Multiple conditionals produce the same result
  • The conditions are logically related
  • Naming the combined check would clarify why the code branches

This ties back to Meaningful Names and Reducing Complexity—consolidation eliminates repetitive structure and communicates intent through naming.

Key Takeaway: Merge related conditional checks into single, well-named expressions or methods to reveal intent and reduce cognitive load.