Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: For clarity and cleanliness, I'd recommend...

by TheDamian (Priest)
on Mar 06, 2005 at 19:29 UTC ( #437076=note: print w/ replies, xml ) Need Help??


in reply to Re: For clarity and cleanliness, I'd recommend...
in thread looking for a good idiom: return this if this is true

I agree with you. I would certainly never recommend that people write:
if ($result = thatroutine()) { return $result; }
But, as your while-based variant shows, non-constant assignments in a conditional are always fine if you're declaring the lvalue variable at that point:
if (my $result = thatroutine()) { return $result; }
In such cases, the = must be intentional, since == on a newly-created variable doesn't make any sense.

As for your alternative suggestion, I don't feel it's a clear or as clean as using an if, because the use of a while implies looping behaviour, which the return undermines in a not-entirely-obvious way. That could be especially misleading to subsequent readers of the code if the number of statements in the while block later increases, pushing the return away from the loop keyword it's subverting.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2016-08-28 06:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The best thing I ever won in a lottery was:















    Results (391 votes). Check out past polls.