Prefer REDUCE #( ) Expression over Simple Aggregation LOOP
Overview
Prefer REDUCE #( ) expression over simple aggregation LOOP.
ATC Finding Message:
Prefer REDUCE #( ) expression over simple aggregation LOOP
Rationale
A loop that only accumulates a sum or builds a single aggregated value mixes imperative control flow with a simple calculation and hides the real intent. Such loops are longer to read and easy to break when the accumulator initialization or the loop body changes. REDUCE #( ) expresses the aggregation declaratively inside one expression and removes the mutable helper state. Use REDUCE for straightforward sums, counts, and string concatenation, and keep a loop only when it performs additional side effects.
Non-Compliant
LOOP AT lt_orders INTO ls_order.
lv_total = lv_total + ls_order-amount.
ENDLOOP.Compliant
lv_total = REDUCE i( INIT total = 0
FOR ls_order IN lt_orders
NEXT total = total + ls_order-amount ).Automated QuickFix
This rule provides an automated QuickFix directly in Eclipse ABAP Development Tools (ADT):
- Position the cursor on the flagged statement.
- Press Ctrl + 1 (Quick Assist).
- Select the automated refactoring to apply the clean syntax immediately.
Suppression
To suppress findings for this rule, append the pseudo-comment to the statement:
" Standard pseudo-comment:
... "#EC PREF_REDUCE
" SAP CI_ prefix format:
... "#EC CI_PREF_REDUCETechnical Details
- Native ATC Class:
ZCL_SKYLINT_PREF_REDUCE - Implementation File: zcl_skylint_pref_reduce.clas.abap