It seems like the AT FIRST
statement of control level processing is ideal for any commands that have to be run or variables that have to be initialised before the loop itself starts running. Given how AT
commands clear columns in the work area it's one of the few uses I still see for them.
To illustrate, rather than doing this:
CLEAR lv_loopcounter.
LOOP AT lt_itab INTO ls_wa.
ADD 1 TO lv_loopcounter.
...
ENDLOOP.
We could do this:
LOOP AT lt_itab INTO ls_wa.
AT FIRST.
CLEAR lv_loopcounter.
ENDAT.
ADD 1 TO lv_loopcounter.
...
ENDLOOP.
Here it's immediately obvious that the variable needs to be initialised for the loop to function as intended. It also makes for cleaner looking code when you have a lot of statements that need to be run prior to a loop. The only downside I see is that it moves code inside a LOOP
statement when it logically shouldn't be.
Is there a best practice here? Are there hidden caveats I should be aware of?