R21 – Avoid using CARDINALITY within loops (WMB)

Avoid using CARDINALITY within loops (WMB)

Avoid using CARDINALITY within loops (WMB)

This is best described by IBM’s advice found here.


The CARDINALITY function must be evaluated each time the loop is traversed, which is costly in performance terms. This is particularly true with large arrays because the loop is repeated more frequently. It is more efficient to determine the size of the array before the WHILE loop (unless it changes in the loop) so that it is evaluated only once

Refactor the code so that the loop boundary is calculated only once
Avoid using CARDINALITY within loops