Ada 95 Quality and Style Guide Chapter 5

Chapter 5: Programming Practices - TOC - 5.4 DATA STRUCTURES

5.4.4 Nested Records

guideline

  • Record structures should not always be flat. Factor out common parts.
  • For a large record structure, group related components into smaller subrecords.
  • For nested records, pick element names that read well when inner elements are referenced.
  • Consider using type extension to organize large data structures.

  • example

    type Coordinate is
       record
          Row    : Local_Float;
          Column : Local_Float;
       end record;
    type Window is
       record
          Top_Left     : Coordinate;
          Bottom_Right : Coordinate;
       end record;
    

    rationale

    You can make complex data structures understandable and comprehensible by composing them of familiar building blocks. This technique works especially well for large record types with parts that fall into natural groupings. The components factored into separately declared records, based on a common quality or purpose, correspond to a lower level of abstraction than that represented by the larger record.

    When designing a complex data structure, you must consider whether type composition or type extension is the best suited technique. Type composition refers to creating a record component whose type is itself a record. You will often need a hybrid of these techniques, that is, some components you include through type composition and others you create through type extension. Type extension may provide a cleaner design if the "intermediate" records are all instances of the same abstraction family. See also Guidelines 5.4.2 and 9.2.1.

    notes

    A carefully chosen name for the component of the larger record that is used to select the smaller enhances readability, for example:

    if Window1.Bottom_Right.Row > Window2.Top_Left.Row then . . . 
    


    < 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