Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

by Tux (Canon)
on Nov 25, 2020 at 13:31 UTC ( #11124194=note: print w/replies, xml ) Need Help??


in reply to Re^8: 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?

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
  • Comment on Re^9: What's the right way to write a method which returns one line at a time from a file?
  • Download Code

Replies are listed 'Best First'.
Re^10: What's the right way to write a method which returns one line at a time from a file?
by jcb (Parson) on Nov 26, 2020 at 00:10 UTC

    Fair enough; that example was poorly formatted for that exact code. That approach is far more useful when there is more than just a return in each branch and the program must choose one of several relatively expensive calculations. The formatting I used is better when the expressions are long enough that if (...) { return A } will not fit on one line, but I would collect the conditions into a "table" if they fit. Like:

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

    That said, while I see the point that else can be unnecessary, I still consider forbidding it to be absurd. I also expect that a future optimizing compiler could make use of an if-elsif chain more easily, although rewriting multiple if (...) { ...; return } into if-elsif is only slightly more complex.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2021-04-16 12:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?