Ada 95 Quality and Style Guide Chapter 4

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

4.1.3 Subprograms

guideline

  • Use subprograms to enhance abstraction.
  • Restrict each subprogram to the performance of a single action.

  • example

    Your program is required to draw a menu of user options as part of a menu-driven user interface package. Because the contents of the menu can vary depending on the user state, the proper way to do this is to write a subprogram to draw the menu. This way, the output subprogram has one purpose and the way to determine the menu content is described elsewhere.

    ...
    ----------------------------------------------------------------------
    procedure Draw_Menu
          (Title   : in    String;
           Options : in    Menu) is
       ...
    begin  -- Draw_Menu
       Ada.Text_IO.New_Page;
       Ada.Text_IO.New_Line;
       Ada.Text_IO.Set_Col (Right_Column);
       Ada.Text_IO.Put_Line (Title);
       Ada.Text_IO.New_Line;
       for Choice in Alpha_Numeric loop
         if Options (Choice) /= Empty_Line then
             Valid_Option (Choice) := True;
             Ada.Text_IO.Set_Col (Left_Column);
             Ada.Text_IO.Put (Choice & " -- ");
             Ada.Text_IO.Put_Line (Options (Choice));
         end if;
         ...
       end loop;
    end Draw_Menu;
    ----------------------------------------------------------------------
    

    rationale

    Subprograms are an extremely effective and well-understood abstraction technique. Subprograms increase program readability by hiding the details of a particular activity. It is not necessary that a subprogram be called more than once to justify its existence.

    notes

    Guideline 10.7.1 discusses dealing with the overhead of subroutine calls.


    < 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