Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

•Please don't use foo && truething || falsething

by merlyn (Sage)
on Jul 30, 2002 at 14:50 UTC ( #186219=note: print w/replies, xml ) Need Help??

in reply to Re: an easier way with grep, map, and/or sort?
in thread an easier way with grep, map, and/or sort?

my $key = $institution && ($valid_institution{lc($institution)} || "other") || "unaffiliated";
When I'm doing code review for hire, I do not permit using testthing && truething || falsething in production code unless there's also a big comment in the margin that says literally:
## I'm promising that the true branch here can NEVER EVER EVER ## return a false value, which would have caused the ## false branch to be erroneously executed. If you're ## updating this code, please continue to maintain ## this precondition, which I have placed precariously ## and needlessly in this code. Have a nice day.
If you don't want to add a big friggin' comment like that, then change the very dangerous broken and-or construct to the entirely safe testthing ? truething : falsething, as in:
my $key = $institution ? ($valid_institution{lc($institution)} || "other") : "unaffiliated";

-- Randal L. Schwartz, Perl hacker

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2023-03-21 08:36 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (59 votes). Check out past polls.