In general, I personally find C to be the most readable and easy for everyone to grasp. When I come across A (expr && b()), I have to do a quick doublecheck in my head about whether b(); gets run if expr is true or false. Meat time is expensive. Also, shouldn't we use the 'and' and 'or' operators instead of && and ||? When I come across these, I also have to take a moment and evaluate if whether you meant a logical AND or conditional AND or bitwise AND. Seeing the word 'and' instead whould make it easier for people to read and mean the same thing.

Option B is clean, but doesn't translate to English as clean as I would prefer. In general, I prefer the 'if' and 'unless' clauses after the statement only under rare circumstances. Because the statement is in front of the condition, the statement appears more important to the human mind. So, only use it when it is very likely that the code block WILL be run. For example:

print "Welcome, Mr. President." if from_whitehouse.gov(); instead of launch_nukes() if found_file("/bin/laden");
This same idea goes for unless and or: Remember, 'unless' is much more readable to most new coders than 'if(!$a)'
run_program() unless user_is_idiot(); open_file() or die;
Never put the conditional after the code block if the code block is more than one statement....
print "Welcome, Mr. President" { launch_nukes(); send_ground_troops(); enjoy_a_milkbone_in_a_commie_free_world(); } unless password_failed(); #This can be bad. You have to read so many lines, get #confused, then go back and figure out where the block #began. Pain in the patukis.
Although it is a subset of answer C, my personal prefered method of seeing this is:
if (expr) {b()}
Since the actual expression and the block to be executed are small, put them on one line; Easy to read, translates cleanly to mental English, and easy to add 'else' and other statements in the future. It also doesn't hog 3 lines with mostly whitespace.

Again, keep in mind that this is all a matter of personal preference, and no one is right and wrong. Make your own code and goes with what makes the most sense to use. From my understanding of corporate coding jobs, their guidelines will take all that guess work out for you. :)

Toodles


In reply to Re: Conditional style (if, &&) query by Toodles
in thread Conditional style (if, &&) query by traveler

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.