Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

(tye)Re: GOTO considered (a necessary) evil?

by tye (Cardinal)
on Jul 15, 2002 at 15:43 UTC ( #181826=note: print w/ replies, xml ) Need Help??


in reply to GOTO considered (a necessary) evil?

My favorite way to refactor loops that have the conditional in the middle is to use a subroutine because return in the middle of a subroutine seems an easy thing to understand (and to notice) when you come back to the code. It also avoids arguments with people who can't handle goto.

I also prefer to force eval to return a true value when it succeeds rather than checking $@ since there are cases where $@ can give the wrong impression about whether the eval was successful.

sub tieSession { while( 1 ) { if( eval { tie %session, 'Apache::Session::MySQL', $session, { Handle => HH::DB->db_Main, LockHandle => HH::DB->db_Main }; 1; } ) { return; } if( $@ !~ /^Object does not exist/ ) { print $cgi->header; die_nice( 'Database Error', 'Unable to connect at this time' ); } undef $session; } }
So I don't consider goto inherently evil. I prefer your original code over your coworkers in at least some respects. I really hate the use of Perl bare blocks as loops and think it often leads to more spagetti code than even goto does (expecting people to notice a "redo" burried so deep inside in order to realize that a loop was suddenly created out of a bare block).

But I wouldn't over golf for "excellence". Is the code working? There is probably something more important to be working on. (:

        - tye (but my friends call me "Tye")


Comment on (tye)Re: GOTO considered (a necessary) evil?
Download Code
Replies are listed 'Best First'.
Re: (tye)Re: GOTO considered (a necessary) evil?
by RMGir (Prior) on Jul 15, 2002 at 16:26 UTC
    It also avoids arguments with people who can't handle goto.

    I'd agree with you, except I ran into at least one developer who thought that returns from the middle of subroutines were just as unreasonable as goto's. :)

    I'm in the "if I can understand what it's doing, it's fine" camp, myself. The goto in the original message was pretty clear, as is your example.
    --
    Mike

        You seem to have misunderstood my position...

        I wasn't saying they were bad, I was just saying I'd worked with someone who thought
        a) goto's BAD, no exceptions
        b) return from middle of subroutine == goto.

        That's NOT my position at all. I believe in _necessary_ gotos, and don't mind return/redo/last/next at all, as long as they're not used to cause chaos :)
        --
        Mike

Re: (tye)Re: GOTO considered (a necessary) evil?
by Anonymous Monk on Jul 15, 2002 at 19:40 UTC
    there are cases where $@ can give the wrong impression about whether the eval was successful.

    Can you give an example of that?

      Sure:

      sub DESTROY { eval "1" } eval 'my $x = bless {}; die "ouch\n"'; print "($@)\n";
      which prints "()" not "(ouch\n)".

              - tye (but my friends call me "Tye")

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2015-07-08 03:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (94 votes), past polls