Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Control Flow - Multiple returns or nested conditionals

by jethro (Monsignor)
on Oct 07, 2010 at 16:20 UTC ( #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?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2021-07-26 13:40 GMT
Find Nodes?
    Voting Booth?

    No recent polls found