ABAP Keyword Documentation → ABAP Programming Guidelines → Robust ABAP → System Fields
Using System Fields as Actual Parameters
Other versions: 7.31 | 7.40 | 7.54
Background
The structure sy
exists once in an internal session and is shared by all
programs in this internal session. The components of sy represent the system fields. System fields are global for all programs of an internal session and their procedures.
Rule
Do not use system fields as actual parameters
Never use system fields as actual parameters - especially not for passing by reference.
Details
This rule reinforces the rule do not pass global data to local contexts by reference. Since system fields are set implicitly, you need to be even more careful. If the value of a system field changes implicitly within a procedure, the value of the parameter passed by reference, which refers to this system field, also changes. Procedures are never prepared for this behavior.
You should even not pass system fields by value. This is because a procedure might be switched to 'pass by reference' in an enhancement process, without the user of the procedure being notified. The only secure method is to assign the value of a system field to a normal variable and then use this variable as the actual parameter when calling the program.
Bad example
Look at the do_something
method in the following source code. You would expect
the index
parameter in the loop to contain the unchanged value that was passed
to the procedure. In fact, index
references sy-index
, which is set to the current loop counter in the DO
loop.
PUBLIC SECTION.
METHODS main.
PRIVATE SECTION.
METHODS do_something IMPORTING index TYPE i.
ENDCLASS.
CLASS class IMPLEMENTATION.
METHOD main.
DO 2 TIMES.
do_something( sy-index ).
ENDDO.
ENDMETHOD.
METHOD do_something.
DO 3 TIMES.
... index ... .
ENDDO.
ENDMETHOD.
ENDCLASS.
Good example
The following source code corrects the call of the do_something method
. A helper variable is passed with the corresponding value instead of sy-index
.
CLASS class IMPLEMENTATION.
METHOD main.
DATA index TYPE sy-index.
DO 2 TIMES.
index = sy-index.
do_something( index ).
ENDDO.
ENDMETHOD.
...
ENDCLASS.
...