I tend to prefer the multiple returns. Nothing like modifying someone else's 1500 line foreach loop filled with conditionals nested four deep slinging hashes, or HoHoAoHoHoHoHoAoH's 9 levels deep to make you yearn for an exit or two. :-)
from Refactoring: Improving The Design Of Existing Code by Martin Fowler.
Using "Replace Nested Conditional With Guard Clauses" refactoring method.
Original Code
double getPayAmount() { double result; if(m_isDead) result = deadAmount(); else { if(m_isSeparated) result = separateAmount(); else { if(m_isRetired) result = retireAmount(); else result = normalPayAmount(); }; } return result; }
Refactored Code
double getPayAmount() { if(m_isDead) return deadAmount(); if(m_isSeparated) return separatedAmount(); if(m_isRetired) return retiredAmount(); return normalPayAmount(); }
In reply to Re: Control Flow - Multiple returns or nested conditionals
by jakeease
in thread Control Flow - Multiple returns or nested conditionals
by JediWizard
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |