I used to work at a company that had a policy of using the first method. ie of leaving off the
else. (They did code reviews.) It was claimed that this lead to more "linear" flow rather than deeply nested flow.
The idea is that the example
if block is checking for (and dealing with) an exceptional situation. That check/deal step is a logical unit with respect to whatever the code it is part of is trying to accomplish.
Once that check/deal step is done the program continues on with the next step. Including an
else clutters the code and slightly hides the main focus of the code.
Another effect of using
else where it is not needed is it increases the level of nesting. That often makes code harder to understand.
The company also had commenting standards so the code might look something like the following.
GOAL means "something we want to do".
CLAIM means "something that is true at this point in the code".
my $name = param("name");
# GOAL : ensure input values are valid
if ($name eq "")
{
print qq(Sorry, name can't be empty);
exit;
}
# CLAIM : all input values are valid
# GOAL : process input values
print "Thank you, $name. Your submission is complete.";
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.