in reply to Control Flow - Multiple returns or nested conditionals
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(); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Control Flow - Multiple returns or nested conditionals
by FunkyMonk (Bishop) on Oct 09, 2010 at 01:07 UTC |