Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

by merlyn (Sage)
on Jul 30, 2002 at 14:50 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-26 06:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found