Ada 95 Quality and Style Guide Chapter 2

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

2.1.3 Alignment of Operators

guideline

  • Align operators vertically to emphasize local program structure and semantics.
  • example

        if Slot_A >= Slot_B then
           Temporary := Slot_A;
           Slot_A    := Slot_B;
           Slot_B    := Temporary;
        end if;
        ----------------------------------------------------------------
        Numerator   := B**2 - 4.0 * A * C;
        Denominator := 2.0 * A;
        Solution_1 := (B + Square_Root(Numerator)) / Denominator;
        Solution_2 := (B - Square_Root(Numerator)) / Denominator;
        ----------------------------------------------------------------
         := A * B +
             C * D +
             E * F;
        Y := (A * B + C) +  (2.0 * D - E) -  -- basic equation
             3.5;                            -- account for error factor
    

    rationale

    Alignment makes it easier to see the position of the operators and, therefore, puts visual emphasis on what the code is doing.

    The use of lines and spacing on long expressions can emphasize terms, precedence of operators, and other semantics. It can also leave room for highlighting comments within an expression.

    exceptions

    If vertical alignment of operators forces a statement to be broken over two lines, especially if the break is at an inappropriate spot, it may be preferable to relax the alignment guideline.

    automation notes

    The last example above shows a kind of "semantic alignment" that is not typically enforced or even preserved by automatic code formatters. If you break expressions into semantic parts and put each on a separate line, beware of using a code formatter later. It is likely to move the entire expression to a single line and accumulate all the comments at the end. However, there are some formatters that are intelligent enough to leave a line break intact when the line contains a comment. A good formatter will recognize that the last example above does not violate the guidelines and would, therefore, preserve it as written.


    < 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