Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Control Flow - Multiple returns or nested conditionals

by jethro (Monsignor)
on Oct 07, 2010 at 16:20 UTC ( [id://864032]=note: print w/replies, xml ) Need Help??


in reply to Control Flow - Multiple returns or nested conditionals

Not using multiple returns is one of those old rules that emerged out of the 'Goto considered harmful' war, I think. Still the goal to reach is always to have the most readable and maintainable code. The danger of multiple returns is that maintaining it can be difficult. So if that danger is more than compensated by a clearer structure of the sub, it is acceptable IMHO.

I can think of two general types of subroutine where that is often the case:

1) When the logic of a subroutine has many exceptions or error conditions.

2) The "big switch" subroutine, i.e. the subroutine with a long switch/case statement

When I use multiple returns in a subroutine, I always mention it prominently in the comments at the head of the sub.

Whether your example is already exceptional enough for multiple returns is a matter of taste. I think I wouldn't have used them there, but I might have given a different answer yesterday ;-).

  • Comment on Re: Control Flow - Multiple returns or nested conditionals

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-25 22:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found