23
Replace Nested Conditional with Guard Clauses
Goal
Edge cases bail out early at the top of the function; the main flow is unindented and tells the happy path linearly.
Before the refactoring
function payAmount(employee) {
if (employee.isSeparated) {
return separationPay(employee);
} else {
if (employee.isRetired) {
return retirementPay(employee);
} else {
return regularPay(employee);
}
}
}After the refactoringfunction payAmount(employee) {
if (employee.isSeparated) return separationPay(employee);
if (employee.isRetired) return retirementPay(employee);
return regularPay(employee);
}Savings
Indentation drops; the dominant case is obvious; new edge cases land at the top without disturbing the rest.
Note
If multiple paths share work, premature returns can duplicate that work — extract first, then guard.