Skip to content

Prefer REDUCE #( ) Expression over Simple Aggregation LOOP ​

WarningClean ABAP & Modern SyntaxQuickFix Ready#EC PREF_REDUCERemote Enabled

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 ​

abap
LOOP AT lt_orders INTO ls_order.
  lv_total = lv_total + ls_order-amount.
ENDLOOP.

Compliant ​

abap
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):

  1. Position the cursor on the flagged statement.
  2. Press Ctrl + 1 (Quick Assist).
  3. Select the automated refactoring to apply the clean syntax immediately.

Suppression ​

To suppress findings for this rule, append the pseudo-comment to the statement:

abap
" Standard pseudo-comment:
... "#EC PREF_REDUCE

" SAP CI_ prefix format:
... "#EC CI_PREF_REDUCE

Technical Details ​

Released under the MIT License.