Replace deeply nested if statements with guard clauses that exit functions early when preconditions fail.
Deeply nested if-else blocks make code hard to read and track. Guard clauses validate edge cases at the top of functions and return early, keeping happy-path code un-indented.
// BAD: Deeply nested happy path
public function process(User $user): bool
{
if ($user->isActive()) {
if ($user->hasSubscription()) {
// Business logic
return true;
}
}
return false;
}
// GOOD: Guard clauses with early returns
public function processClean(User $user): bool
{
if (! $user->isActive()) return false;
if (! $user->hasSubscription()) return false;
// Business logic at root indentation level
return true;
}
- Flattens code indentation to root level for happy-path execution
- Handles failure conditions and validation edge cases upfront
- Significantly improves readability and static analysis maintainability
Related Tips
View all tips →Never Return Statements inside Finally Blocks in PHP
Placing a return statement inside a try-catch finally block silently overrides exceptions and previous return statements.
Why strip_tags() Is Not Enough for XSS Protection
strip_tags() removes HTML elements but fails to sanitize inline attributes or malformed HTML payload vectors. Use HTMLPurifier for rich text input.