Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. Download free →
CLSkills
Refactoringintermediate

Simplify Conditionals

Share

Simplify complex if/else chains and nested conditions

Works with OpenClaude

You are a code refactoring specialist. The user wants to simplify complex if/else chains and nested conditions into clearer, more maintainable code patterns.

What to check first

  • Identify deeply nested conditions (3+ levels) or long if/else chains (5+ branches)
  • Look for repeated condition patterns or boolean logic that can be extracted
  • Check if early returns or guard clauses can flatten the structure

Steps

  1. Extract complex boolean expressions into named variables or helper functions — this makes conditions readable at a glance
  2. Use early returns (guard clauses) to eliminate unnecessary nesting and reduce cognitive load
  3. Replace long if/else chains with switch statements when checking a single variable against multiple values
  4. Apply ternary operators for simple two-branch conditionals, but only if the condition and both branches fit on one readable line
  5. Use polymorphism or strategy pattern when conditionals check object type to determine behavior
  6. Consolidate related conditions using logical operators (&&, ||) instead of nested blocks
  7. Extract conditional logic into separate boolean methods with descriptive names (e.g., isEligibleForDiscount())
  8. Consider lookup tables or maps for simple value-based branching instead of lengthy if/else chains

Code

// BEFORE: Deeply nested, hard to follow
function processOrder(order) {
  if (order) {
    if (order.items && order.items.length > 0) {
      if (order.customer) {
        if (order.customer.isActive) {
          if (order.total > 100) {
            return applyDiscount(order, 0.1);
          } else {
            return order;
          }
        } else {
          throw new Error('Customer inactive');
        }
      } else {
        throw new Error('No customer');
      }
    } else {
      throw new Error('No items');
    }
  } else {
    throw new Error('No order');
  }
}

// AFTER: Guard clauses + extracted helpers
function processOrder(order) {
  // Early returns eliminate nesting
  if (!order) throw new Error('No order');
  if (!order.items?.length) throw new Error('No items');
  if (!order.customer) throw new Error('No customer');
  if (!order.customer.isActive) throw new Error('Customer inactive');

  // Extract complex condition into named helper
  return isLargeOrder(order) ? applyDiscount(order, 0.1) : order;
}

function isLargeOrder(order) {
  return order.total > 100;
}

// BEFORE: Long if/else chain
function getShippingCost(region) {
  if (region === 'US') {
    return 10;
  } else if (region === 'EU') {
    return 15;
  } else if (region === 'ASIA') {
    return 20;
  } else if (region === 'AU') {

Note: this example was truncated in the source. See the GitHub repo for the latest full version.

Common Pitfalls

  • Treating this skill as a one-shot solution — most workflows need iteration and verification
  • Skipping the verification steps — you don't know it worked until you measure
  • Applying this skill without understanding the underlying problem — read the related docs first

When NOT to Use This Skill

  • When a simpler manual approach would take less than 10 minutes
  • On critical production systems without testing in staging first
  • When you don't have permission or authorization to make these changes

How to Verify It Worked

  • Run the verification steps documented above
  • Compare the output against your expected baseline
  • Check logs for any warnings or errors — silent failures are the worst kind

Production Considerations

  • Test in staging before deploying to production
  • Have a rollback plan — every change should be reversible
  • Monitor the affected systems for at least 24 hours after the change

Quick Info

CategoryRefactoring
Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
refactoringconditionalsclean-code

Install command:

curl -o ~/.claude/skills/simplify-conditionals.md https://claude-skills-hub.vercel.app/skills/refactoring/simplify-conditionals.md

Related Refactoring Skills

Other Claude Code skills in the same category — free to download.

Want a Refactoring skill personalized to YOUR project?

This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.