Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: GOTO or not GOTO

by monarch (Priest)
on Jan 27, 2009 at 06:06 UTC ( [id://739110]=note: print w/replies, xml ) Need Help??


in reply to GOTO or not GOTO

As others have said it is not about politics but about safety. And after enough experience a programmer learns that they are their own worst enemy, so anything you can do to lessen the incidence of problems is a bonus.

The main problem with goto is not understanding potential side-effects. If you goto a particular location then you run the risk of using code that assumes a different state to the one you arrive in.

My issue with goto has always been the stack - I will (albeit rarely) use goto if it does not involving changing contexts (i.e. never jumping into or out of a set of curly braces). But I really don't know the implications to memory usage and contexts when I do something like the following (in C or in Perl):

for ( my $i = 0; $i < 10; $i++ ) { my $j = $i; if ( $i == 5 ) { goto newlocation; } } newlocation: print( "jumped out" );

Now my question is what happened to $j there? Is the compiler "smart enough" to know that I exited the loop and therefore the memory allocated to $j can now be reclaimed?

Even worse, what happens when I use goto to jump between subroutines?

It is because I don't know the answers to these questions that I do my best to avoid the use of goto (except when necessary and not involving context changes).

Replies are listed 'Best First'.
Re^2: GOTO or not GOTO
by ikegami (Patriarch) on Jan 27, 2009 at 07:06 UTC

    Is the compiler "smart enough" to know that I exited the loop and therefore the memory allocated to $j can now be reclaimed?

    Perl doesn't reclaim lexicals, it just clears them. And yes, $j gets cleared.

    sub DESTROY { print("DESTROYED\n"); } for ( my $i = 0; $i < 10; $i++ ) { my $j = bless({}); if ( $i == 0 ) { goto newlocation; } } newlocation: print( "jumped out" );
    DESTROYED jumped out
Re^2: GOTO or not GOTO
by JavaFan (Canon) on Jan 27, 2009 at 08:17 UTC
    for ( my $i = 0; $i < 10; $i++ ) { my $j = $i; if ( $i == 5 ) { goto newlocation; } } newlocation: print( "jumped out" );
    Now my question is what happened to $j there? Is the compiler "smart enough" to know that I exited the loop and therefore the memory allocated to $j can now be reclaimed?
    Imagine you had written:
    for ( my $i = 0; $i < 10; $i++ ) { my $j = $i; if ( $i == 5 ) { last; } } print( "jumped out" );
    you have code that does exactly the same. I presume you have the same worries about $j, and will hence decide to never use 'last'?

    Or else, could you indicate why you have worries about the first construct, but not the last?

    Even worse, what happens when I use goto to jump between subroutines?
    Had you actually bothered to read past the first sentence of the document you link to, you would have known the answer. Here's the second sentence of the document: It may not be used to go into any construct that requires initialization, such as a subroutine or a foreach loop.
    It is because I don't know the answers to these questions that I do my best to avoid the use of goto
    Since I've now answered the questions, are you now going to start using goto?

    I have to admit, you weren't arguing against the use of goto in particular. You were arguing against using constructs you don't know the details about. That's a smart thing. The wise thing would be to actually learn about them instead of bragging ones ignorance.

      The big difference between last and next is that those functions give deterministic answers to the compiler as to code flow. On the other hand goto may not offer as clear an indication of the target; although given a label the compiler may be able to work out whether the destination is outside the given closure anyway.

      So here's the next example, what does the following do:

      for ( my $i = 0; $i < 10; $i++ ) { my $j = $i; if ( $i == 5 ) { goto nextpoint; } } for ( my $k = 0; $k < 10; $k++ ) { my $m = $k; nextpoint: print( "$m\n" ); }

      Well I tried it, but the point is I didn't know what it would do before hand. If that is "bragging one's ignorance" then so be it. Rightly or wrongly I came from a C background where if you tried risky behaviour like this you got into real trouble. Perl is far more forgiving.

      I do prefer to program in a manner that has a degree of portability between languages; as I said before, goto is risky: if you consider yourself an expert in the language at the time and the compiler behaviour and implications (if any) to the stack then by all means go ahead. I'll stick to what I know is safe.

        So here's the next example, what does the following do:
        Well, that's answered by the same second line of the goto documentation: It may not be used to go into any construct that requires initialization, such as a subroutine or a foreach loop.

        For the third example, will to be trying to goto in a while block?

Log In?
Username:
Password:

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

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

    No recent polls found