Using Conditionals


Highlights Of This Documents

This document aims at showing what is good way of using conditionals.

Highlights of this documents include:

  1. If - Then - Else Chains

  2. Choosing the Most Effective Ordering of Cases

  3. Tips for Using Case Statements

  4. Examples

    1. Good Examples
    2. Bad Examples

If - Then - Else Chains

For situations where case statements are impractical, or where a case statement is not implemented in the language, the alternative is usually chains of if - then - else statements. To keep these chains from getting out of hand, it helps to follow a few guidelines:

Use Booleans To Simplify Complicated Tests

Long chains of if - then - else statements quickly become difficult to read. If you break the conditions up into boolean statements or functions it greatly improves readability, by shortening statements and replacing long expressions with a few descriptive names.

Put The Most Used Conditions First

If you put the conditions that occur most often first, it will improve efficiency and readability. Because the most used conditions are first, the program will have to go through less conditions when operating. As well, it decreases the amount of code someone has to read through in the most common cases.

Use A Final Else To Cover All Cases

It is always a good idea in multiple if - then - else statements to include a final else, so as to catch any unexpected situations. It is useful to include an error message, and possibly some error checking code here so that if an unexpected situation does happen you can determine what in the program's execution caused it.

Use Constructs Like Case To Replace If - Then - Else Chains

In most cases your language's case statement is more effective, and legible, than a chain of if - then - else statements. If it is possible, use these constructs instead.

Choosing the Most Effective Ordering of Cases

When writing a case statement, is important to choose proper ordering.  If the case statement is simple and short it doesn't matter.  But if it is a complicated case statement it is best to go with out of the following ways.

Order cases alphabetically or numerically

If the each case in the case statement is equally important then it is best to do order it alphabetically A-B-C or numerically 1-2-3.

Put the normal case first

In the case it is best to put the normal case first, in other words it is best to have the case with will happen and other which are exception put them in a later case  statements, also keep in mind to put those exception in a proper order also.

Order cases by frequency

Order cases by frequency means, that is if the cases don't have equal importance. Then they should be ordered by which will be called the most.  By doing it in this fashion it makes easy for someone else to read your code and also makes the execution time more quicker.  For example if you have 20 cases and you put the most frequently used one at the very bottom then the complier would have to go through 19 other if statements, in order to reach the one used most.  So it is best to put it at the top.

Tips for Using Case Statements

Here are a few things to keep in mind when using case statements.
  1. Keep the code of each case short. If the code is too long the case structure becomes obscured. If you need long complicated code, put it in a routine to keep the case structure obvious.
  2. Don't make extra variables to use a case statement. For example don't truncate an input string into a single character so you can use the case statement. Instead use if-then-else chains that compare the entire string.
  3. Use the default case only for real default cases. In other words, don't make your last case default simply because it's the last one. Doing this makes it more difficult to add more cases later, and makes the code harder to understand.
  4. Use the default case for detect input errors. Add code the the default case to output an error message, or, if possible, code to fix the problem.
  5. In C, avoid 'dropping through' the bottom of a case. In C each case needs to end with a break command, without it the program will continue to execute the code in the next case (see Poor Use of Case Statements In C). If is your intention to drop through the end of a case, clearly indicate this through comments.

Code Complete, A Practical Handbook of Software Construction. Steve McConnell. Microsoft Press (1993)

This page was modified by Group Members: Mohit K. Gupta, Chris Mills, Chantal Laplante, Mel Lenz and Graeme Humphries at Time: Monday, June 19, 2000.
Copyright: Department of Computer Science, University of Regina of Regina.

[CS Department]