Flatten deeply nested conditionals into readable logic
Deeply nested if/else chains are a common source of bugs and reviewer complaints. This prompt walks an LLM through systematically flattening them using guard clauses and early returns.
You are a senior software engineer doing a focused refactor. I have a block of code with deeply nested conditionals that I want to simplify.
Here is the code:
```
{{NESTED_CODE_BLOCK}}
```
Language: {{LANGUAGE}}
Follow these steps:
1. Read the code and describe in plain English what each branch is doing. Do not skip any branch.
2. Identify which conditions can be inverted into guard clauses (early returns or throws) to reduce nesting depth.
3. Identify any conditions that are logically equivalent and can be merged.
4. Rewrite the code using guard clauses, early returns, or a lookup structure where appropriate. Preserve the exact same observable behavior — do not change logic.
5. Annotate each significant change with a one-line comment explaining the transformation.
6. Flag any branch where the original intent is ambiguous and you had to make an assumption. List these at the end under 'Assumptions made'.
Do not introduce new dependencies or change the function signature unless I ask.
Edge cases to flag: if the nesting is caused by missing abstraction (a whole branch should be its own function), say so but do not refactor it automatically — just note it. {{NESTED_CODE_BLOCK}}{{LANGUAGE}}
How to use this prompt
- Copy the prompt above (Copy button on the top-right).
- Replace each
{{VAR}}with your own value. Variables:{{NESTED_CODE_BLOCK}}{{LANGUAGE}}. - Paste it into one of the recommended tools below.
- Iterate: tighten constraints in the prompt if the output is generic.
Why this prompt is structured this way
The prompt is split into explicit steps because LLMs do better when the path is named, not implied. Each variable forces specificity at the input layer — vague inputs get vague outputs.
Pair this prompt with a tool
Cursor
$20/moAI-first code editor forked from VS Code.
Cursor is VS Code with first-class LLM integration. Multi-file edits, codebase Q&A, autocomplete that's actually useful. The default IDE for many shipping engineers in 2026.
Claude (Anthropic)
$0/mo (Pro at $20)Frontier model with long context and strong reasoning.
Claude (Opus / Sonnet / Haiku tiers) is the assistant favored by writers and engineers who care about reasoning quality and tone. 1M token context on Opus.
ChatGPT (OpenAI)
$0/mo (Plus at $20)The category-defining general-purpose AI assistant.
ChatGPT has the broadest feature surface: image gen, voice, custom GPTs, web browsing, code execution. Often the right default; sometimes beaten on specific tasks by Claude or Perplexity.
The PlaybookPrompts weekly
One short email per week. The five sharpest prompts we found, one tool worth your attention, one anti-pattern to avoid. Unsubscribe in one click.
Newsletter signup is not configured. Set PUBLIC_NEWSLETTER_USERNAME in the build env.