Ada 95 Quality and Style Guide Chapter 5

### Chapter 5: Programming Practices - TOC - 5.5 EXPRESSIONS

5.5.5 Short Circuit Forms of the Logical Operators

guideline

• Use short-circuit forms of the logical operators to specify the order of conditions when the failure of one condition means that the other condition will raise an exception.

• example

Use:

```if Y /= 0 or else (X/Y) /= 10 then
```

or:

```if Y /= 0 then
if (X/Y) /= 10 then
```

rather than either:

```if Y /= 0 and (X/Y) /= 10 then
```

or:

```if (X/Y) /= 10 then
```

to avoid Constraint_Error.

Use:

```if Target /= null and then Target.Distance < Threshold then
```

rather than:

```if Target.Distance < Threshold then
```

to avoid referencing a field in a nonexistent object.

rationale

The use of short-circuit control forms prevents a class of data-dependent errors or exceptions that can occur as a result of expression evaluation. The short-circuit forms guarantee an order of evaluation and an exit from the sequence of relational expressions as soon as the expression's result can be determined.

In the absence of short-circuit forms, Ada does not provide a guarantee of the order of expression evaluation, nor does the language guarantee that evaluation of a relational expression is abandoned when it becomes clear that it evaluates to False (for and) or True (for or).

notes

If it is important that all parts of a given expression always be evaluated, the expression probably violates Guideline 4.1.4, which limits side-effects in functions.

 < 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