Skip to content

Guard Clauses Beat Nested Ifs

Quick Tips•
October 23, 2026

When you wrap the real work of a method inside three layers of if, the actual logic ends up at the deepest indentation level. Adding a fourth check, say a refund window, means deciding which branch it nests in and re-indenting the rest. Anyone reading the method has to track which conditions are still open.

Don't do this:

PHP
<?php declare(strict_types=1); namespace App\Billing; use App\Models\Order; final readonly class RefundProcessor { public function refund(Order $order, int $amount): RefundResult { if ($order->isPaid()) { if ($order->isRefundable()) { if ($amount <= $order->total()) { return RefundResult::issued($amount); } else { return RefundResult::tooLarge(); } } else { return RefundResult::notRefundable(); } } else { return RefundResult::notPaid(); } } }

Do this instead:

PHP
<?php declare(strict_types=1); namespace App\Billing; use App\Models\Order; final readonly class RefundProcessor { public function refund(Order $order, int $amount): RefundResult { if (! $order->isPaid()) { return RefundResult::notPaid(); } if (! $order->isRefundable()) { return RefundResult::notRefundable(); } if ($amount > $order->total()) { return RefundResult::tooLarge(); } return RefundResult::issued($amount); } }

Each guard handles one failure and exits. By the time you reach the last line, every precondition has already passed, so the happy path sits at the top indentation level. Adding the refund-window check means adding one more guard above the final return.