in reply to GOTO or not GOTO

There is almost never a need to use goto in modern programming languages with a plethora of good flow control structures. If your intent is "do this block of code while some condition pertains" then do while expresses that very nicely.

If you don't find that a satisfying argument in the context of your code you better show us your code so we can debate specifics.


Perl's payment curve coincides with its learning curve.

Replies are listed 'Best First'.
Re^2: GOTO or not GOTO
by JavaFan (Canon) on Jan 27, 2009 at 11:42 UTC
    There's never a need to use 'while' either, given there's a plethora of other good flow control structures as well. But from that doesn't follow we should shy away from 'while'. Perl gives the programmer a full toolbox, full enough to make almost any tool 'redundant', in the sense that there are enough other tools to not use a particular tool. But we don't use that argument to select some tools, and reject others.

    Personally, I use almost every tool* in the toolbox. Some more than others. I don't use goto often, but I do use it. For instance, in code like this:

    my $max_tries = 10; GETSTORE: my $url = ask_url(); my $status = getstore($url, $file); goto GETSTORE if $status == 200 && !-s $file && --$max_tries > 0; ... do stuff ... return $status;
    I find that more attractive than
    my $max_tries = 10; my $status; { my $url = ask_url(); $status = getstore($url, $file); redo if $status == 200 && !-s $file && --$max_tries > 0; } .. do stuff .. return $status

    *Tools I never use that I can think of right now: formats, 1-arg open, study, and /o.

      I'd write that as (untested)
      while ( uninteresting() ) {}; : : sub uninteresting { # I assume $file and $max_tries are globals my $url = ask_url(); my $status = getstore($url, $file); return ( $status == 200 && !-s $file && --$max_tries > 0 ); }
        Well, neither $file nor $max_tries are globals. During the live time of the program the routine is called hundreds of times, and for each new call, I've a new file name, and 10 new tries. Besides, isn't using global variables a sin as bad as a goto?

        But more importantly, the reason I wrote the code with the goto is that I need the $status afterwards. And I need to know whether the download eventually succeeded or not. Now, you can achieve all that by having uninteresting returning a list of two things (the status code, and whether it succeeded or not), but the code gets more complex that way.

        I prefer the simplicity of the goto solution.

      You don't say why you find the goto version more attractive. I guess because of the separate declaration and initialization of $status. However:

      my $status; do { $status = getstore (ask_url (), $file); } while $status == 200 && !-s $file && --$max_tries > 0;

      makes it clear that there is a loop involved and what the conditions on the loop are. Having $status declared outside the loop makes it clear that $status is required after the loop has terminated.


      Perl's payment curve coincides with its learning curve.
        But that suggests the loop is "normal". It isn't. Repeating a piece of the code is the exception - it may be repeated once every couple of thousand of times. Normal program flow just goes top to bottom. Using a rare construct (goto) signifies that. Using a regular loop doesn't.

        And I prefer not to have a separate declaration and initilization. But that only plays a part in the decision.

Re^2: GOTO or not GOTO
by KurtZ (Friar) on Jan 27, 2009 at 10:33 UTC
    There is almost never a need to use goto in modern programming languages

    Right, but especially in Perl there are some cases where you can't get around goto, e.g. in AUTOLOAD!

      Well, it's called goto, but that's a little misleading. In many ways the use (often found in the context of AUTOLOAD) that you allude to is more akin to a magical call to a subroutine than to the conventional use of goto suggested by the OP.


      Perl's payment curve coincides with its learning curve.