Grouping constant computations
When several items in an expression are constant, ensure that the optimizer is able to optimize them. The compiler is bound by the left-to-right evaluation rules of COBOL. Therefore, either move all the constants to the left side of the expression or group them inside parentheses.
About this task
For example, if V1
, V2
,
and V3
are variables and C1
, C2
,
and C3
are constants, the expressions on the left
below are preferable to the corresponding expressions on the right:
More efficient | Less efficient |
---|---|
V1 * V2 * V3 * (C1 * C2 * C3) |
V1 * V2 * V3 * C1 * C2 * C3 |
C1 + C2 + C3 + V1 + V2 + V3 |
V1 + C1 + V2 + C2 + V3 + C3 |
In production programming, there is often a tendency to place constant factors on the right-hand side of expressions. However, such placement can result in less efficient code because optimization is lost.