Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^3: GOTO or not GOTO

by tilly (Archbishop)
on Jan 27, 2009 at 11:30 UTC ( [id://739157]=note: print w/replies, xml ) Need Help??


in reply to Re^2: GOTO or not GOTO
in thread GOTO or not GOTO

I would personally replace it with a redo on a bare block. That bare block would also be a hint to indent the potentially redone section of code, which naturally draws attention to that control structure.

General rule of thumb. Any normal use of goto can be replaced by an entirely equivalent loop control statement, possibly using labels when breaking out of multiple levels of loops. Unless there is a good reason not to, I prefer that approach.

Replies are listed 'Best First'.
Re^4: GOTO or not GOTO
by JavaFan (Canon) on Jan 27, 2009 at 12:09 UTC
    Any normal use of goto can be replaced by an entirely equivalent loop control statement, possibly using labels when breaking out of multiple levels of loops.
    This is exactly why in some cases I prefer goto over loop control statements. A loop introduces a block. Variabled my()ed in the block don't live outside them - if they are needed after the block (or goto destination), you have to separate the my() and the first initialization. Which is something I prefer not to. Second, sometimes you encounter code that, if turned into nested loops instead of gotos, will be nested deeply. Which can lead to a narrow strip of code on the right hand side of the window.

    Now, that doesn't mean I use goto lightly. But I may opt for a goto instead of loop control two or three times a year.

      The problem of declaring a variable in a block I can appreciate as valid.

      We disagree though on the code which, when turned into nested loops becomes very deep. That is a code smell that tells me that something should probably be rewritten. Obscuring that problem by using gotos just compounds the problem. I would therefore like to see the ugliness of the structure made as apparent as possible, as a prelude to figuring out whether you can rewrite it.

      Perhaps we work on very different things. But I have never once used goto other than the goto &foo; form in anything outside of play code in Perl. And I think it unlikely that I ever will. (Though I would use it in C++.)

      You may wish to read Re (tilly) 4: Paradigm Shift - when to use goto for an explanation of where I theoretically would use goto. Note that this is theoretical since I have not personally needed it. (Other than for removing the current stack from from the stack.)

      Incidentally I am amused to see goto defended by a fan of a language without goto. :-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-26 03:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found