Ada 95 Quality and Style Guide Chapter 2

Chapter 2: Source Code Presentation - TOC - 2.1 CODE FORMATTING

2.1.8 Number of Statements Per Line

guideline

  • Start each statement on a new line.
  • Write no more than one simple statement per line.
  • Break compound statements over multiple lines.
  • example

    Use:

        if End_Of_File then
           Close_File;
        else
           Get_Next_Record;
        end if;
    

    rather than:

        if End_Of_File then Close_File; else Get_Next_Record; end if;
    

    exceptional case:

        Put("A=");    Natural_IO.Put(A);    New_Line;
        Put("B=");    Natural_IO.Put(B);    New_Line;
        Put("C=");    Natural_IO.Put(C);    New_Line;
    

    rationale

    A single statement on each line enhances the reader's ability to find statements and helps prevent statements being missed. Similarly, the structure of a compound statement is clearer when its parts are on separate lines.

    exceptions

    If a statement is longer than the remaining space on the line, continue it on the next line. This guideline includes declarations, context clauses, and subprogram parameters.

    According to the Ada Reference Manual (1995, §1.1.4), "The preferred places for other line breaks are after semicolons."

    automation notes

    The guidelines in this section are easily enforced with an automatic code formatter, with the single exception of the last example, which shows a semantic grouping of multiple statements onto a single line.

    exceptions

    The example of Put and New_Line statements shows a legitimate exception. This grouping of closely related statements on the same line makes the structural relationship between the groups clear.


    < 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