In this section...
3.4.1 Declaring Types
3.4.2 Enumeration Types
|Summary of Guidelines from this section|
subtype Card_Image is String (1 .. 80); Input_Line : Card_Image := (others => ' '); -- restricted integer type: type Day_Of_Leap_Year is range 1 .. 366; subtype Day_Of_Non_Leap_Year is Day_Of_Leap_Year range 1 .. 365;
By the following declaration, the programmer means, "I haven't the foggiest idea how many," but the actual range will show up buried in the code or as a system parameter:
Employee_Count : Integer;
Different implementations provide different sets of values for most of the predefined types. A reader cannot determine the intended range from the predefined names. This situation is aggravated when the predefined names are overloaded.
The names of an object and its subtype can clarify their intended use and document low-level design decisions. The example above documents a design decision to restrict the software to devices whose physical parameters are derived from the characteristics of punch cards. This information is easy to find for any later changes, thus enhancing program maintainability.
Section 8.5 of the Ada Language Reference Manual says that declaring a subtype without a constraint is one method for renaming a type.
Types can have highly constrained sets of values without eliminating useful values. Usage as described in Guideline 5.3.1 eliminates many flag variables and type conversions within executable statements. This renders the program more readable while allowing the compiler to enforce strong typing constraints.
Recognize that any deviation from this guideline detracts from the advantages of the strong typing facilities of the Ada language.
Language Ref Manual references: 3.3 Types and Subtypes, 3.4 Derived Types, 3.5 Scalar Types, 8.5 Renaming Declarations, C. Predefined Language Environment
type Color is (Blue, Red, Green, Yellow);
Blue : constant := 1; Red : constant := 2; Green : constant := 3; Yellow : constant := 4;
and add the following if necessary.
for Color use (Blue => 1, Red => 2, Green => 3, Yellow => 4);
In addition, Ada provides a number of attributes (
'Value) for enumeration types which, when used, are more reliable
than user-written operations on encodings.
A numeric code may at first seem appropriate to match external values. Instead, these situations call for a representation clause on the enumeration type. The representation clause documents the "encoding." If the program is properly structured to isolate and encapsulate hardware dependencies (see Guideline 7.1.5), the numeric code ends up in an interface package where it can be easily found and replaced should the requirements change.
Language Ref Manual references: 3.5.1 Enumeration Types, 13.3 Enumeration Representation Clauses, A. Predefined Language Attributes
This file was converted with TextToHTML - (c) Logic n.v.