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?? |
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. 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
In Section
Seekers of Perl Wisdom
|
|