http://qs321.pair.com?node_id=11124098


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 (Canon) 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";

        🦛