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


In reply to Re: Style: expr or warn vs. if..warn? by blokhead
in thread Style: expr or warn vs. if..warn? by FoxtrotUniform

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.