# How to use guard clauses to simplify nested ifs

> Replace nested conditions with guard clauses one early exit at a time. Use an upload example to preserve validation order, messages and side effects.

Source: https://cueapril.com/knowledge/how-to/refactor-nested-conditionals
Updated: 2026-12-02
Index: https://cueapril.com/llms.txt

## How do I replace nested if statements with guard clauses safely?

List the outcomes of the existing branches, then move one early exit at a time towards the start of the function. Preserve the order of decisions and side effects. A guard clause should make a condition easier to read while callers continue to see the same results.

## Find the first decision that matters

For each branch, write what the caller receives and what changes outside the function. Two conditions can both be true for the same input. Their order then determines which error the caller gets and which work has already happened.

1. Capture one ordinary input, each early-exit case and one input that meets two exit conditions.
2. Move a single guard and run the examples. Compare return values, errors and visible effects such as a saved record.
3. Repeat until the main operation is readable. Leave a nested decision intact if moving it would obscure a transaction or change its timing.

## Separate a refactor from a policy change

Sometimes the examples reveal that the existing order surprises the team. Record the intended policy with its owner. Changing which message the user sees is a behaviour change and deserves an explicit expectation in review.

## Worked example: An expired request with an empty attachment

An upload function checks that the request is still valid before inspecting its file. An expired request with an empty file currently returns an expiry message and saves nothing. Moving the empty-file guard ahead of the expiry check changes the message. Keeping the expiry guard first preserves the result while making the valid upload path easier to follow.

Adapted by Cue April from The Engineering Handbook v1.0.0 by Tenhaw (© 2026 Tenhaw LTD). The examples and pull request checks are Cue April adaptations.

[Tenhaw on guard clauses and control flow](https://github.com/Tenhaw/engineering-handbook/blob/10d618fde978727e419a524606f351721b0b1d23/01-code-writing.md#control-flow) · [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)

## Try it on your pull request

On your own PR, check an input that meets two early-exit conditions and compare the observable result before and after the refactor.

[Try Cue April on your repository](https://app.cueapril.com/sign-up/?next=%2Fquality-start)

Create an account, connect your repository and review the test plan.

## Related reading

- [What a characterisation test can tell you](https://cueapril.com/knowledge/explanations/characterisation-and-correctness)

- [How to build a decision table for a business rule](https://cueapril.com/knowledge/how-to/build-a-decision-table)

- [How to write code review feedback someone can act on](https://cueapril.com/knowledge/how-to/write-an-actionable-code-review)

[Browse the Knowledge base](https://cueapril.com/knowledge)
