Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I would like an opinion if adding "NEXT" at the end of the "IFs" will improve the process on this particular block of code. Or not.
Thanks!
Here is the block of code in spec.
... for (my $i = 0; $i < @{$mydata}; $i++) { $account_number = $mydata->[$i]{'account_number'} || ''; $name = $mydata->[$i]{'name'} || ''; $email = $mydata->[$i]{'email'} || ''; if($email eq '') { bad_email("No email address found '$account_number'."); next; # <------ here } elsif ($email !~ /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i ) { $msg = "Invalid email address - '$email' on account '$account_nu +mber'."; bad_email($msg); next; # <------ here } elsif (($name eq '') || ($account_number eq '')) { $msg = "Missing information: Number = '$account_number', Name = +'$name'."; next; # <------ here } else { # Send emails send_email( send my stuff here); my $data = "<tr><td align=\"left\">".$account_number."</td><td>" +.$email."</td></tr>"; push @all_data, $data; } $sql = "update table set date = '$datetime' where number = '$accou +nt_number' and date = '$mydata->[$i]{'date'}'"; exec_single_sql($sql, $db) || die "Unable to update table"; } ...

Replies are listed 'Best First'.
Re: Next in IF statement question!
by Corion (Patriarch) on Nov 16, 2010 at 16:55 UTC

    The code does not act the same with or without the next. Whoever asked you the question wants you to understand how next changes the flow of the program.

Re: Next in IF statement question!
by bobr (Monk) on Nov 16, 2010 at 18:16 UTC
    If next is present, you don't need elsif (can be just if) and last else can be plain code.
    for (my $i = 0; $i < @{$mydata}; $i++) { $account_number = $mydata->[$i]{'account_number'} || ''; .... if($email eq '') { bad_email("No email address found '$account_number'."); next; } ... if(($name eq '') || ($account_number eq '')) { $msg = "Missing information: Number = '$account_number', Name = '$ +name'."; next; } # Send emails send_email( send my stuff here); ... exec_single_sql($sql, $db) || die "Unable to update table"; }

    This is giving you less indentation and you don't have to worry about covering error cases in end code (email, SQL).

    On the other hand, I've read Dijkstra arguing that multiple exit/repeat points make it more difficult to comprehend and analyse the loop.

    -- Roman

      next makes it harder (or impossible) to prove your program correct, and Djikstra was all about proof of correctness.

      I personally think the way next is employed can make a huge difference in readability and understandability. I would rather be able to say next if this; next if that; next if the_other; and then the rest of the loop; I find that much easier to understand.

      I don't care much whether I can prove it if I can read it.

Re: Next in IF statement question!
by GrandFather (Saint) on Nov 17, 2010 at 03:16 UTC

    Write Perl not C (see the for loop below). Use strictures and lexical variables. Use early exits (that's yes use next to your question btw). Oh, and use place holders in your SQL:

    for my $datum (@{$mydata}) { my $account_number = $datum->{'account_number'} || ''; my $name = $datum->{'name'} || ''; my $email = $datum->{'email'}; if (! $email) { bad_email("No email address found '$account_number'."); next; } if ($email !~ /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i) { bad_email( "Invalid email address - '$email' on account '$account_num +ber'."); next; } next if ($name eq '') || ($account_number eq ''); # Send emails send_email("send my stuff here"); my $data = qq(<tr><td align="left">$account_number</td><td>$email< +/td></tr>); push @all_data, $data; my $sql = "update table set date = ? where number = ? and date = ? +"; exec_single_sql($sql, $db, $datetime, $account_number, $datum->{da +te}) or die "Unable to update table"; }
    True laziness is hard work
Re: Next in IF statement question!
by anonymized user 468275 (Curate) on Nov 16, 2010 at 17:22 UTC
    As programmers we can only comment on how or how best a requirement may be achieved with Perl, not on whether that requirement itself is better or worse -- that's for the designers and analysts to figure out! As Corion implies, the "next" changes the functionality and hence changes the requirement the program meets or doesn't. If this is a real requirement it might be an idea to state it.

    One world, one people