This document aims at showing what is good way of using conditionals.
Highlights of this documents include:
If - Then - Else Chains
Choosing the Most Effective Ordering of Cases
Tips for Using Case Statements
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.
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.
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 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.
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.