Ada 95 Quality and Style Guide Chapter 4

Chapter 4: Program Structure - TOC - 4.1 HIGH-LEVEL STRUCTURE

4.1.8 Data Coupling

guideline

  • Avoid declaring variables in package specifications.

  • example

    This is part of a compiler. Both the package handling error messages and the package containing the code generator need to know the current line number. Rather than storing this in a shared variable of type Natural, the information is stored in a package that hides the details of how such information is represented and makes it available with access routines:

    -------------------------------------------------------------------------
    package Compilation_Status is
       type Line_Number is range 1 .. 2_500_000;
       function Source_Line_Number return Line_Number;
    end Compilation_Status;
    -------------------------------------------------------------------------
    with Compilation_Status;
    package Error_Message_Processing is
       -- Handle compile-time diagnostic.
    end Error_Message_Processing;
    -------------------------------------------------------------------------
    with Compilation_Status;
    
    package Code_Generation is
       -- Operations for code generation.
    end Code_Generation;
    -------------------------------------------------------------------------
    

    rationale

    Strongly coupled program units can be difficult to debug and very difficult to maintain. By protecting shared data with access functions, the coupling is lessened. This prevents dependence on the data structure, and access to the data can be controlled.

    notes

    The most prevalent objection to this guideline usually involves performance penalties. When a variable is moved to the package body, subprograms to access the variable must be provided and the overhead involved during each call to those subprograms is introduced. See Guideline 10.7.1 for a discussion about subprogram overhead.


    < 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