Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Style: expr or warn vs. if..warn?

by blokhead (Monsignor)
on Jul 09, 2004 at 23:52 UTC ( [id://373292]=note: print w/replies, xml ) Need Help??


in reply to Style: expr or warn vs. if..warn?

I used to use "this if that" and "this unless that" a lot until very recently. Without even making a conscious effort, I've been using short-circuit "and"/"or" in my last few projects. And now that I look over some recent code, I seem to be using quite an inconsistent melange of the two. Yikes! I'll have to nail this down and make a rule for myself...

I like this rule-of-thumb: Use if/unless for checking a condition. Use and/or when the statement does something with side-effects.

do_something_complex() or die; do_something_complex() and then_do_something_else(); # condition checking: die "terribly" if $condition; die "terribly" unless $obj->can("foo");
Clearly there's a bit of overlap between "checking a condition" and "doing something with side-effects." In the event of a tie, I wholeheartedly agree with hossman that the most important part of the statement should be at the beginning, because it's easier to skim the code.

Also, don't forget to consider the surrounding code. If you have a bunch of guard statements in a row that try to do/check something or else die/return, make them all the same structure!

blokhead

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-19 06:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found