Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: Number of times I've used goto in Perl (sub, return++)

by tye (Sage)
on Mar 26, 2015 at 00:03 UTC ( #1121350=note: print w/replies, xml ) Need Help??


in reply to Re: Number of times I've used goto in Perl
in thread Number of times I've used goto in Perl

1) avoiding excessive nested blocks in terms of length and depth, 2) having a single point of return or exit from a routine or subroutine and 3) avoiding goto.

That's why Larry created subroutines.

And (2) is just a stupid restriction. Adding extra returns is one of the most effect refactoring tools I found.

- tye        

  • Comment on Re^2: Number of times I've used goto in Perl (sub, return++)

Replies are listed 'Best First'.
Re^3: Number of times I've used goto in Perl (sub, return++)
by AnomalousMonk (Chancellor) on Mar 26, 2015 at 01:02 UTC
    ... subroutines ... extra returns ...

    I don't understand these points.

    Certainly using multiple returns and/or refactoring code into subroutines can, in general, be extremely useful for improving the readability/reliability/maintainability of code.

    But the particular code example presented in Re: Number of times I've used goto in Perl has a rather unusual structure. There are two interleaved streams of function calls and instructions:

    • optional functions that may be called only if no previous optional function failed;
    • required function calls and instructions that must all be called in order even if any previous optional function call has failed.

    Using multiple returns and/or refactoring into subroutines (or even (shudder) using nested if-elsif-else blocks or conditional goto statements), it would certainly be possible to replicate the control flow of the OPed code, but again, I can't see how it would be any improvement on (anonymized user)'s basic approach; indeed, it seems to me that it could only be much worse. Can you give an example of what you envision?


    Give a man a fish:  <%-(-(-(-<

      Yeah, I saw several things in that node. There was the topic of this thread (goto), the intro paragraph that said that this can conflict with 2 other things, and a prominent code comment suggesting that a label could be inserted if goto were used. And none of that seems to have anything to do (as far as I could figure out) with the alternating-&&= part of the code that you commented on. I agree that the &&= part of the code is interesting. I also fail to see where a goto would help any of the code shown.

      So I was only addressing the topic that matched the thread, the opening paragraph, and the prominent comment in the code.

      Perhaps it was actually desirable for the &&= short-circuiting to apply to all of the code so the better alternative was a lot of "goto failed if ! ...;" lines? That's my best guess, anyway.

      For such a case, the better approach I would do would be to change:

      sub do_lots_and_log { ... goto failed if ! method1(...); ... log("yay!"); return; failed: log("boo!"); }

      to:

      sub do_lots_and_log { if( do_lots(...) ) { log("yay!"); } else { log("boo!"); } } sub do_lots { ... method1(...) or return 0; ... return 1; }

      Sometimes "throw exception" is an even better solution than "early return", of course.

      - tye        

Re^3: Number of times I've used goto in Perl (sub, return++)
by shagbark (Acolyte) on May 06, 2018 at 02:36 UTC
    Adding extra returns makes it much more time-consuming for someone who didn't write the code to figure out what the return value of a routine is or means. (It also means extra code if you're using any debugging tools to check assertions or track runtime with code that executes at the start and end of important routines. Which I always do in Perl, because Perl has so little typing.)

    In fact, about the only time I use goto is to goto the exit point of a routine. The necessary label will notify anyone looking at that code that there is some internal path directly to it.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2019-03-20 11:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How do you Carpe diem?





    Results (98 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!