Ada 95 Quality and Style Guide Chapter 2

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

2.1.1 Horizontal Spacing


  • Use consistent spacing around delimiters.
  • Use the same spacing as you would in regular prose.

  • instantiation

    Specifically, leave at least one blank space in the following places, as shown in the examples throughout this book. More spaces may be required for the vertical alignment recommended in subsequent guidelines.

    - Before and after the following delimiters and binary operators:

    + - * / &
    < = > /= <= >=
    := => | ..
    - Outside of the quotes for string (") and character (' ) literals, except where prohibited.
    - Outside, but not inside, parentheses.
    - After commas (,) and semicolons (;). Do not leave any blank spaces in the following places, even if this conflicts with the above recommendations.
    - After the plus (+) and minus (-) signs when used as unary operators.
    - After a function call.
    - Inside of label delimiters (<< >>).
    - Before and after the exponentiation operator (**), apostrophe ('), and period (.)
    - Between multiple consecutive opening or closing parentheses.
    - Before commas (,) and semicolons (;).

    When superfluous parentheses are omitted because of operator precedence rules, spaces may optionally be removed around the highest precedence operators in that expression.


    Default_String : constant String :=
          "This is the long string returned by" &
          " default. It is broken into multiple" &
          " Ada source lines for convenience.";
    type Signed_Whole_16 is range -2**15 .. 2**15 - 1;
    type Address_Area  is array (Natural range <>) of Signed_Whole_16;
    Register : Address_Area (16#7FF0# .. 16#7FFF#);
    Memory   : Address_Area (       0 .. 16#7FEC#);
    Register(Pc) := Register(A);
    X := Signed_Whole_16(Radius * Sin(Angle));
    Register(Index) := Memory(Base_Address + Index * Element_Length);
    Get(Value => Sensor);
    Error_Term := 1.0 - (Cos(Theta)**2 + Sin(Theta)**2);
    Z      := X**3;
    Y      := C * X + B;
    Volume := Length * Width * Height;


    It is a good idea to use white space around delimiters and operators because they are typically short sequences (one or two characters) that can easily get lost among the longer keywords and identifiers. Putting white space around them makes them stand out. Consistency in spacing also helps make the source code easier to scan visually.

    However, many of the delimiters (commas, semicolons, parentheses, etc.) are familiar as normal punctuation marks. It is distracting to see them spaced differently in a computer program than in normal text. Therefore, use the same spacing as in text (no spaces before commas and semicolons, no spaces inside parentheses, etc.).


    The one notable exception is the colon (:). In Ada, it is useful to use the colon as a tabulator or a column separator (see Guideline 2.1.4). In this context, it makes sense to put spaces before and after the colon rather than only after it as in normal text.

    automation notes

    The guidelines in this section are easily enforced with an automatic code formatter.

    < Previous Page Search Contents Index Next Page >
    1 2 3 4 5 6 7 8 9 10 11
    Appendix References Bibliography