Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Conditional style (if, &&) query

by Toodles (Monk)
on Oct 17, 2001 at 07:28 UTC ( [id://119318]=note: print w/replies, xml ) Need Help??


in reply to Conditional style (if, &&) query

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

Replies are listed 'Best First'.
Re: Re: Conditional style (if, &&) query
by blakem (Monsignor) on Oct 17, 2001 at 08:47 UTC
    I don't think its even valid to do a simple "BLOCK unless". However "do BLOCK unless" is valid.
    do { blah; blah; blah; } unless 0;

    -Blake

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-19 04:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found