PlaybookPrompts

Flatten deeply nested conditionals into readable logic

Coding & Development refactoringcode-qualityreadability

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.

Prompt
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.
Variables to fill in
  • {{NESTED_CODE_BLOCK}}
  • {{LANGUAGE}}

How to use this prompt

  1. Copy the prompt above (Copy button on the top-right).
  2. Replace each {{VAR}} with your own value. Variables: {{NESTED_CODE_BLOCK}}{{LANGUAGE}}.
  3. Paste it into one of the recommended tools below.
  4. 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.

Heads up: some of the links on this page are affiliate links — meaning we may earn a commission if you sign up after clicking, at no extra cost to you. We only recommend tools we'd put on our own stack. You can see our full affiliate disclosure here.

Pair this prompt with a tool

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.