Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-25 15:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found