Acknowledgement: the content of these design recipes pages has been adapted from: UBC CPSC 110
A data definition consists of:
We now examine some common forms of information and data.
Use simple atomic data when the information that you are trying to represent is itself atomic. For example, the temperature or pressure of an object, or the x-coordinate of a particle.
Use an interval when the information you are trying to represent is numeric and in a certain range. Intervals can be closed (e.g.
Testing: be sure to test the endpoints at the closed-end of any interval as well as a point somewhere in the middle of the interval.
Use an enumeration when the information consists of a fixed number of distinct values. Note that in this case, examples are usually considered redundant as the types comment includes all possible values for the data.
Testing: you should have at least as many tests as there are cases in the enumeration.
Use compound data when two or more values naturally belong together. For example, the x and y coordinates of a point, or the air temperature, air pressure and time of a weather report.
Use an itemization when information comprises two or more subclasses, at least one of which is not a distinct data item. (Note that if all cases are distinct data items, you should use an enumeration.) Itemizations often consists of two or more intervals.
Testing: you should have at least as many tests as there are items in the itemization. In this particular example, as each item is an interval, we test the closed-end of each interval as well as a point in the middle of each interval.
Use a self-referential data definition when information in the problem domain is of an arbitrary size.
Note that the above data definition is used so commonly that we represent it with the special notation
Testing: you must test the base case and the case where the list has at least 2 elements.