Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^6: What's the right way to write a method which returns one line at a time from a file?

by jcb (Parson)
on Nov 24, 2020 at 00:50 UTC ( #11124098=note: print w/replies, xml ) Need Help??


in reply to Re^5: What's the right way to write a method which returns one line at a time from a file?
in thread What's the right way to write a method which returns one line at a time from a file?

While statement (3) in your example is dubious, statement (2) and its else block are completely legitimate if statements (1) and (2) both cause early exit from the block. If statement (2) does not cause an early exit, the else should be removed (or changed to a comment # else ...) and statement (2) promoted to the same position as statement (3), leaving statement (1) as a conditional early exit. If statements (1) and (2) both cause early exits, statement (3) is dead code and should be removed.

I think I see your rationale, in that an else block can be redundant in these situations, since the entire rest of the block after a conditional early exit is effectively an "else" block without the additional indentation level. I still say that a rule that else should never be used in this context is ridiculous because it can be used to cause the indentation to line up and produce clearer code, particularly in the case where one of two (or of multiple, if elsif is used) possible return values should be chosen. In such a context, using the final else allows all of the return statements to line up at the same indentation level, producing clearer code, especially if K&R brace placement is used.

  • Comment on Re^6: What's the right way to write a method which returns one line at a time from a file?
  • Select or Download Code

Replies are listed 'Best First'.
Re^7: What's the right way to write a method which returns one line at a time from a file?
by Tux (Abbot) on Nov 24, 2020 at 08:19 UTC

    You're almost in my lane :)

    The key for my claim is that the return/exit/die/croak is the last unconditional statement in the if block. I might have been clearer on that.


    Enjoy, Have FUN! H.Merijn

      Your rule would forbid:

      if (...) { return A } elsif (...) { return B } elsif (...) { return C } else { die "unmatched case" }

      This is why I say it is ridiculous, because I see that construct as a perfectly legitimate way to handle returning different values in different cases. The final die (or a final return undef) is indented along with all of the other cases. Omitting the else would place it one level out. Note that an if-elsif chain like this obviously can have no code following it, but the else here simply highlights that one of these will be executed. Also note that I tend to omit the semicolon after return/last/next/die/etc. in Perl because there should not be another statement after any of those and omitting the semicolon promotes such dead code to a syntax error.

        As others already stated, your code is confusing and yes, my rule would forbid that.

        Your code has no more value than any of these (all clearer):

        if (EXPR1) { return A }; if (EXPR2) { return B }; if (EXPR3) { return C }; die "unmatched case"; EXPR1 and return A; EXPR2 and return B; EXPR3 and return C; die "unmatched case"; return A if EXPR1; return B if EXPR2; return C if EXPR3; die "unmatched case";

        For short expressions, my personal preference is the middle one. The first is the best choice if it is likely that the blocks can have statements before the return.

        I personally do not like statement modifiers, so I would not choose the third option, but it is still by far better than your example.

        Opinions differ though. TIMTOWTDI. But you still have shown no reason why my ruling would be rediculous. You only showed an example that strenghtens my opinion on it.


        Enjoy, Have FUN! H.Merijn

        Somewhat tangential, but a less messy, more readable and preferable (IMHO) version of this is:

        return COND_A ? A : COND_B ? B : COND_C ? C : COND_D ? () : # returns undef or empty list per context die "unmatched case" ;


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

        I see that construct as a perfectly legitimate way to handle returning different values in different cases.

        It is legitimate but all those unnecessary elsifs and elses spread confusion. Why are they there if they serve no purpose? TIMTOWTDI but I prefer the clarity of

        return A if FOO; return B if BAR; return C if BAZ; die "unmatched case";

        🦛

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2021-03-03 20:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favorite kind of desktop background is:











    Results (88 votes). Check out past polls.

    Notices?