Ada 95 Quality and Style Guide Chapter 3

Chapter 3: Readability - TOC - 3.4 USING TYPES

3.4.1 Declaring Types

guideline

  • Limit the range of scalar types as much as possible.
  • Seek information about possible values from the application.
  • Do not reuse any of the subtype names in package Standard.
  • Use subtype declarations to improve program readability (Booch 1987).
  • Use derived types and subtypes in concert (see Guideline 5.3.1).

  • example
    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 base range will show up buried in the code or as a system parameter:

    Employee_Count : Integer;
    

    rationale

    Eliminating meaningless values from the legal range improves the compiler's ability to detect errors when an object is set to an invalid value. This also improves program readability. In addition, it forces you to carefully think about each use of objects declared to be of the subtype.

    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.

    You can rename a type by declaring a subtype without a constraint (Ada Reference Manual 1995, §8.5). You cannot overload a subtype name; overloading only applies to callable entities. Enumeration literals are treated as parameterless functions and so are included in this rule.

    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.

    notes

    Subtype declarations do not define new types, only constraints for existing types.

    Any deviation from this guideline detracts from the advantages of the strong typing facilities of the Ada language.

    exceptions

    There are cases where you do not have a particular dependence on any range of numeric values. Such situations occur, for example, with array indices (e.g., a list whose size is not fixed by any particular semantics). See Guideline 7.2.1 for a discussion of appropriate uses of predefined types.


    < Previous Page Search Contents Index Next Page >
    1 2 3 4 5 6 7 8 9 10 11
    TOC TOC TOC TOC TOC TOC TOC TOC TOC TOC TOC
    Appendix References Bibliography