Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Control Flow - Multiple returns or nested conditionals

by jakeease (Friar)
on Oct 08, 2010 at 10:08 UTC ( #864175=note: print w/replies, xml ) Need Help??


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 (Chancellor) on Oct 09, 2010 at 01:07 UTC
    Please, if you're posting exemplar code, fix the indenting before you post. Even if it's screwed up by copying and pasting, there's still the spacebar.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://864175]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2021-07-26 12:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?