Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

gav (2710)

  (email not shown publicly)
AOL IM: flufflegavin (Add Buddy, Send Message)

Hacker in NYC.

Journal of gav (2710)

Tuesday April 29, 2003
10:37 AM

don't misplace the else

[ #11907 ]

If you have a complicated if-elsif-elsif block, make sure you have an else statement. This bit me in the ass today, what wasn't ever supposed to happen, happened.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • Good advice (Score:4, Insightful)

    by jordan (120) on 2003.04.29 12:22 (#19579) Homepage Journal

    Steve McConnell in Code Complete relates the following (p.314):

    Consider the else clause. If you think you need a plain if statement, consider whether you don't actually need an if-then-else statement. In a General Motors analysis of code written in PL/I, only 17 percent of the if statements had an else clause. Further analysis of the code showed that 50 to 80 percent should have had one (Elshoff 1976 - Ref: Elshoff, James L. 1976. "An Analysis of Some Commercial PL/I Programs." IEEE Transactions on Software Engineering SE-2, no. 2 (June): 113-20)

    It's good advice to always consider the else clause, not just when the if-elsif block is complicated.

    As an aside, I really enjoy Code Complete. It's crammed with research and experience as opposed to the preference and dogma you usually see on Software Construction issues. Anybody know of any other good books like this? Brook's The Mythical Man Month is similar, but I don't know of too many other examples.