Simplify complex if/else chains and nested conditions
✓Works with OpenClaudeYou 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
- Extract complex boolean expressions into named variables or helper functions — this makes conditions readable at a glance
- Use early returns (guard clauses) to eliminate unnecessary nesting and reduce cognitive load
- Replace long if/else chains with switch statements when checking a single variable against multiple values
- Apply ternary operators for simple two-branch conditionals, but only if the condition and both branches fit on one readable line
- Use polymorphism or strategy pattern when conditionals check object type to determine behavior
- Consolidate related conditions using logical operators (&&, ||) instead of nested blocks
- Extract conditional logic into separate boolean methods with descriptive names (e.g.,
isEligibleForDiscount()) - 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
Related Refactoring Skills
Other Claude Code skills in the same category — free to download.
Extract Function
Extract code blocks into well-named functions
Rename Symbol
Safely rename variables, functions, classes across codebase
Remove Dead Code
Find and remove unused code
Extract Component
Extract UI code into reusable components
DRY Refactor
Find code duplication and extract shared logic
Promise to Async
Convert Promise chains to async/await
Class to Functions
Convert class-based code to functional style
Callback to Promise
Convert callback-based code to Promises
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.