Ada 95 Quality and Style Guide Chapter 8

Chapter 8: Reusability - TOC - 8.1 UNDERSTANDING AND CLARITY

8.1.3 Generic Formal Parameters

guideline

  • Document the expected behavior of generic formal parameters just as you document any package specification.

  • example

    The following example shows how a very general algorithm can be developed but must be clearly documented to be used:

    ------------------------------------------------------------------------
    generic
       -- Index provides access to values in a structure.  For example,
       -- an array, A.
       type Index is (<>);
       type Element is private;
       type Element_Array is array (Index range <>) of Element;
       -- The function, Should_Precede, does NOT compare the indexes
       -- themselves; it compares the elements of the structure.
       -- The function Should_Precede is provided rather than a "Less_Than" function
       -- because the sort criterion need not be smallest first.
       with function Should_Precede (Left  : in     Element;
                                     Right : in     Element)
         return Boolean;
       -- This procedure swaps values of the structure (the mode won't
       -- allow the indexes themselves to be swapped!)
       with procedure Swap (Index1 : in     Index;
                            Index2 : in     Index;
                            A      : in out Element_Array);
       -- After the call to Quick_Sort, the indexed structure will be
       -- sorted:
       --     For all i,j in First..Last :  i<j  =>  A(i) < A(j).
    procedure Quick_Sort (First : in     Index := Index'First;
                          Last  : in     Index := Index'Last);
    ------------------------------------------------------------------------
    

    rationale

    The generic capability is one of Ada's strongest features because of its formalization. However, not all of the assumptions made about generic formal parameters can be expressed directly in Ada. It is important that any user of a generic know exactly what that generic needs in order to behave correctly.

    In a sense, a generic specification is a contract where the instantiator must supply the formal parameters and, in return, receives a working instance of the specification. Both parties are best served when the contract is complete and clear about all assumptions.


    < 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