in reply to Conditional style (if, &&) query
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:
This same idea goes for unless and or: Remember, 'unless' is much more readable to most new coders than 'if(!$a)'print "Welcome, Mr. President." if from_whitehouse.gov(); instead of launch_nukes() if found_file("/bin/laden");
Never put the conditional after the code block if the code block is more than one statement....run_program() unless user_is_idiot(); open_file() or die;
Although it is a subset of answer C, my personal prefered method of seeing this is: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.
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.if (expr) {b()}
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 |