Create a new account
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.
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.
Reply to This
Get More Comments