Ada 95 Quality and Style Guide Chapter 5

Chapter 5: Programming Practices - TOC - 5.1 OPTIONAL PARTS OF THE SYNTAX

5.1.1 Loop Names

guideline

  • Associate names with loops when they are nested (Booch 1986, 1987).
  • Associate names with any loop that contains an exitstatement.

  • example
    Process_Each_Page:
       loop
          Process_All_The_Lines_On_This_Page:
             loop
                ...
                exit Process_All_The_Lines_On_This_Page when Line_Number = Max_Lines_On_Page;
                ...
                Look_For_Sentinel_Value:
                   loop
                      ...
                      exit Look_For_Sentinel_Value when Current_Symbol = Sentinel;
                      ...
                   end loop Look_For_Sentinel_Value;
                ...
             end loop Process_All_The_Lines_On_This_Page;
          ...
          exit Process_Each_Page when Page_Number = Maximum_Pages;
          ...
       end loop Process_Each_Page;
    

    rationale

    When you associate a name with a loop, you must include that name with the associated end for that loop (Ada Reference Manual 1995). This helps readers find the associated end for any given loop. This is especially true if loops are broken over screen or page boundaries. The choice of a good name for the loop documents its purpose, reducing the need for explanatory comments. If a name for a loop is very difficult to choose, this could indicate a need for more thought about the algorithm.

    Regularly naming loops helps you follow Guideline 5.1.3. Even in the face of code changes, for example, adding an outer or inner loop, the exit statement does not become ambiguous.

    It can be difficult to think up a name for every loop; therefore, the guideline specifies nested loops. The benefits in readability and second thought outweigh the inconvenience of naming the loops.


    < 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