Refactor Nested Conditionals into Guard Clauses
Deeply nested if/else blocks are one of the hardest things to read in any codebase — and one of the easiest things for Claude to untangle using guard clauses and early returns.
// Before: three levels of nesting
function processOrder(Order $order): void {
if ($order->isPaid()) {
if ($order->hasItems()) {
if (!$order->isFulfilled()) {
// do the actual work
}
}
}
}
Ask Claude: "Refactor this function using guard clauses and early returns. Keep the behaviour identical."
// After: flat, readable, easy to scan
function processOrder(Order $order): void {
if (!$order->isPaid()) return;
if (!$order->hasItems()) return;
if ($order->isFulfilled()) return;
// do the actual work
}
Claude handles the full range of patterns: ternaries collapsed into early returns, negated conditions flipped to positives, nested switch statements flattened. You can paste an entire class and ask it to apply the pattern everywhere it finds it.
It will also flag cases where an early return would change observable behaviour — so you don't accidentally swallow exceptions or skip side effects.
Guard clauses aren't just style — they reduce cognitive load by letting you rule out invalid states before you've read a single line of business logic.
Log in to leave a comment.
The /security-review command scans your uncommitted changes for injection vectors, auth gaps, hardcoded secrets, and other common vulnerabilities.
The SessionStart hook fires when any session begins or resumes, making it ideal for loading environment variables and running one-time setup scripts.
Ask Claude to write property-based tests for your functions using fast-check — it identifies the mathematical invariants in your code and generates tests that cover inputs you'd never enumerate by hand.