Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Re (tilly) 2: Fear of Large Languages (was: "Would you use goto")

by dragonchild (Archbishop)
on Dec 06, 2001 at 17:10 UTC ( #130013=note: print w/replies, xml ) Need Help??


in reply to Re (tilly) 2: Fear of Large Languages (was: "Would you use goto")
in thread Would you use 'goto' here?

Actually, I have used goto in production when there was a perfectly good construct, namely the continue block.

(What, you had to click and find out what it did? Hrmmm... maybe goto is more maintainable from a knowledge standpoint.)

I had a large while loop that iterated over an array that was constructed earlier. I did some stuff, then had a set of if-then statements. Yes, I could've built this out to be a series of functions, but, as this was the meat of the script, didn't feel that was warranted. I did want to be able to "return" out of the first if-block and continue processing after the last else. So, I put a label up, called it DONE, and used goto quite liberally.

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

  • Comment on Re: Re (tilly) 2: Fear of Large Languages (was: "Would you use goto")

Replies are listed 'Best First'.
Re (tilly) 4: Fear of Large Languages (was: "Would you use goto")
by tilly (Archbishop) on Dec 06, 2001 at 18:09 UTC
    This is a not uncommon use of goto in many languages. However if you follow the node at the top, you will find out that I pointed out to Ovid and TheDamian emphasized (with a pre-processor hack showing how to get them in C++), that Perl has a cleaner way to do this. Namely loop control combined with named loops.

    So keep aware of the option of doing this with a goto. But remember in any language to look for named loops. If they exist, then that is a better solution to this problem.

      while my $foo (@bars) { if ($foo =~ /baz/i) { goto DONE unless $foo eq uc $foo; } else { goto DONE unless $foo ne uc $foo; } DONE: do_more_stuff($foo); }
      This doesn't work with next or redo. It works with continue.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        foreach my $foo (@bars) { { if ($foo =~ /baz/i) { last unless $foo eq uc $foo; } else { last unless $foo ne uc $foo; } # stuff } do_more_stuff($foo); }
        works just fine. There are several other options as well. First there is the continue that you mentioned:
        foreach my $foo (@foos) { if ($foo =~ /baz/i) { next unless $foo eq uc $foo; } else { next unless $foo ne uc $foo; } # stuff } continue { do_more_stuff($foo); }
        and then (depending on what you do in the if statement) there is reversing the order of your operations:
        foreach my $foo (@foos) { do_more_stuff($foo); if ($foo =~ /baz/i) { next unless $foo eq uc $foo; } else { next unless $foo ne uc $foo; } # stuff }
        You can have the chain of ifs be replaced by a function call that returns from multiple points:
        foreach my $foo (@foos) { do_stuff($foo); do_more_stuff($foo); } sub do_stuff { my $foo = shift; if ($foo =~ /baz/i) { return unless $foo eq uc $foo; } else { return unless $foo ne uc $foo; } # stuff }
        And so on.

        I have, in fact, used every one of these solutions except the goto one, and I prefer all of them to the goto solution. Why? Because I find each of them clearer, they allow me to see program flow in terms of following blocks. The goto forces me to see an element of program flow which is not some form of block. I don't like that. Furthermore, no matter how clearly the goto solution's intention may be, its use opens up the possibility of traps like this:

        foreach (1..5) { print "Hello\n"; goto DONE; } DONE: foreach (1..5) { print "World\n"; goto DONE; } DONE:
        By contrast anything only relying on return and loop control statements can be much more easily verified correct based on local examination. (OK, in this case your goto construct is verifiably correct without looking at outside code. But it will take more time for most people to figure out why the one is verifiably OK while the other is demonstrably bad than it will to figure out my alternate solution.)

        Again, this is not something I would use a goto for.

        UPDATE
        Fixed a thinko danger pointed out to me. last and next are not the same.

Re: Re: Re (tilly) 2: Fear of Large Languages (was: "Would you use goto")
by Anonymous Monk on Dec 06, 2001 at 17:33 UTC

    In such a case, it is generally more acceptable to spell goto as next or redo as needed, and the LABELs are often unnecessary as well :-)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2021-09-25 03:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?