SPUG: Loop styles: goto vs. redo

dleonard at dleonard.net dleonard at dleonard.net
Tue Jul 2 16:37:45 CDT 2002


On Mon, 1 Jul 2002, Richard Anderson wrote:

> After some wrangling with my CPAN co-author over a trivial point of style,
> I'd like to throw this before the group.  For production code, which is
> better (or suggest an alternative that is better than both):
>
> START_TESTS:
>     my $not_done = 0;
>     for (...) {
>         unless (...) {
>             ...
>             $not_done = 1;
>         }
>     }
>     goto START_TESTS if $not_done
>
> or
>
> {
>     my $done = 1;
>     for  (....) {
>         unless(...) {
>             ...
>             $done = 0;
>         }
>     }
>     redo unless $done;
> }

Personally I'm more of a fan of while().

my $done = 0;
while (!$done) {
 for (...) {
  if (...) {
   $done = 1;
  }
 }
}

Doesn't that just sound good?  'while not done' Sure it introduces another
loop scope but that shouldn't be a concern unless you are looking for that
last .01% performance.  The problem with redo and goto is code flow.  You
don't know what your conditional is until you are in the middle or end of
your block.

<Douglas Leonard>
<dleonard at dleonard.net>


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org




More information about the spug-list mailing list