Ada 95 Quality and Style Guide Chapter 5

Chapter 5: Programming Practices - TOC - 5.5 EXPRESSIONS

5.5.3 Parenthetical Expressions

guideline

  • Use parentheses to specify the order of subexpression evaluation to clarify expressions (NASA 1987).
  • Use parentheses to specify the order of evaluation for subexpressions whose correctness depends on left to right evaluation.

  • example
    (1.5 * X**2)/A - (6.5*X + 47.0)
    2*I + 4*Y + 8*Z + C
    

    rationale

    The Ada rules of operator precedence are defined in the Ada Reference Manual (1995, §4.5) and follow the same commonly accepted precedence of algebraic operators. The strong typing facility in Ada combined with the common precedence rules make many parentheses unnecessary. However, when an uncommon combination of operators occurs, it may be helpful to add parentheses even when the precedence rules apply. The expression:

    5 + ((Y ** 3) mod 10)
    

    is clearer, and equivalent to:

    5 + Y**3 mod 10
    

    The rules of evaluation do specify left to right evaluation for operators with the same precedence level. However, it is the most commonly overlooked rule of evaluation when checking expressions for correctness.


    < 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