I couldn’t remember who said that one of his favorite refactoring techniques is Replace Nested Conditional with Guard Clauses. When I looked into it, it did put a smile on my face despite its simplicity.
This is NOT about coding aesthetics, this is all about clarity.
A method has conditional behavior that does not make clear the normal path of execution.
Use guard clauses for all the special cases.
double getPayAmount() {
double result;
if (isDead) {
result = deadAmount();
} else if (isSeparated) {
result = separatedAmount();
} else if (isRetired) {
result = retiredAmount();
} else {
result = normalPayment();
}
}
double getPayAmount() {
if (isDead) return deadAmount();
if (isSeparated) return separatedAmount();
if (isRetired) return retiredAmount();
return normalPayment();
}
So much better!
Share this post
Twitter
Google+
Facebook
Reddit
LinkedIn
StumbleUpon
Email