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

Re: Control Flow - Multiple returns or nested conditionals

by Marshall (Canon)
on Oct 08, 2010 at 01:46 UTC ( #864123=note: print w/replies, xml ) Need Help??


in reply to Control Flow - Multiple returns or nested conditionals

I believe that the first example is better than then 2nd.
Why? Because it is easier to understand. It wins hands down - with no doubt at all in my mind - but obviously you have some doubts.

Clarity should be the key thing, not conformance to some arbitrary "coding style rule".

The "rules" about things like "no multiple returns" have to be taken in the context of the "art of programming". Judgment is required.

Option #1 has 10 program statements aside from boiler-plate, 3 of which have a return value or about 1/3 of the statements send something back to the caller (produce a result).

The arguments against having multiple return statements are the same as the ones against the "go to". But this is a bogus argument if you have a function that is expressed in some dozen lines or say 1/2 page and it has a very clear purpose and a very clear return value.

The "go to" is deadly when it jumps to widely separated parts of the code. That's not what we have here!

I just don't buy the argument that you are going to modify a 10 line (or even 1/2 page) subroutine without understanding what those 10 lines do!

Calling a sub that has multiple return points can simply the overall code's logic and increase the maintainability of the code. Reducing the level of indenting is a huge factor in clarity. Clarity is a good thing, not a bad thing.

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

Replies are listed 'Best First'.
Re^2: Control Flow - Multiple returns or nested conditionals
by JediWizard (Deacon) on Oct 08, 2010 at 03:10 UTC

    Actually Marshall, I agree with you whole heartedly. I was quite deliberate in my asking of this question to not state up front which of the implementations I prefer, so that the responses I got would not be influenced by my opinion. It has been my experience that when asking this type of question, the best way to get clear unbiased responses is to remove as much of my subjective position from the description as possible...

    That having been said, at this point it is quite clear from the responses in this thread, that my preference for multiple returns is the more popular opinion. That may not do me any good in my disagreement with my colleague, but it does hold some level of personal satisfaction.


    They say that time changes things, but you actually have to change them yourself.

    —Andy Warhol

      I posted some code earlier today at Re: Checking contents of array against contents of a hash. There is a subroutine, is_array_subset() which contains 2 return() statements. I am curious as to how your colleague would code this sub?

      I used less than 10 lines of code (I don't count curly braces or blank lines as lines of code). I actually added the "@extra" feature after doing the main part - and having multiple returns made that easier to do.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2021-07-27 22:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?