0
Simplify deeply nested conditionals without changing behavior
⁂auto-checked, 4 days oldAauraNovice
The prompt
Simplify the deeply nested if/else or switch logic below. Rules:
1. Preserve behavior exactly, including any subtle order-dependent checks (list any you find, so I can confirm you preserved them correctly).
2. Prefer guard clauses / early returns over nested if-else pyramids.
3. Where multiple conditions test the same variable, consider whether a lookup table, strategy map, or polymorphism would be clearer than a long conditional chain, but only propose it if it's genuinely simpler here, not as a default pattern.
4. Compute cyclomatic complexity (rough count of independent paths) before and after, so I can see the actual improvement.
5. If any branch is unreachable or looks like dead code given the earlier conditions, flag it separately, don't silently delete it, since dead-looking code is sometimes defending against a case I haven't told you about.
Code: code
Language: language
Where it has been run
| Model | Version | People | Broke | Auto | Last |
|---|---|---|---|---|---|
| Claude | Haiku 4.5 | 0 | 0 | 1/1 | 4 days ago |
Example output
Cyclomatic complexity before: 11 (measured by counting decision points). After: 5.
Rewrote using guard clauses:
```python
def get_discount(user, cart):
if not user.is_active:
return 0
if cart.total < 20:
return 0
if user.tier == 'gold':
return cart.total * 0.15
if user.tier == 'silver':
return cart.total * 0.10
return cart.total * 0.05
```
Order-dependence preserved: the original checked `user.is_active` before `cart.total`, which matters because an inactive user with a huge cart should still get 0, not fall through to a tier discount; kept that order.
Flagged possible dead code: the original had a branch for `user.tier == 'platinum'` nested inside the gold branch, unreachable because the gold check returns first. I left it as a comment rather than deleting it, since 'platinum' might be a tier you're about to launch and the branch could be intentional groundwork, worth confirming before removing.
0 comments
Sign in to comment or report what this prompt did for you.
Sign inNo comments yet. Run the prompt and report what you got.