Skip to content

ABAP Keyword Documentation →  ABAP Programming Guidelines →  Structure and Style →  Program Structure and Procedure Structure 

Local Declarations

Other versions: 7.31 | 7.40 | 7.54

Background

You can implement local declarations in a procedure (method). These are data types, data objects, and field symbols that are valid in the context of the procedure only. Therefore, they can only be addressed in the code of the procedure and are only available during the execution of the procedure (the data objects declared with STATICS are an exception).

Procedure-local declarations can be listed at any position of a procedure. However, the position of the declaration does not influence the validity area of the declared object (which always comprises the entire procedure), but only the static visibility.

Rule

Implement local declarations at the beginning of the procedure

Position the local declarations of a procedure ( method coherently and at the beginning of a procedure. The local declarations must not be distributed across the implementation of the procedure.

Details

Local declarations within a procedure ( method) are statically visible starting from the point of the program where they are positioned to the end of the procedure. However, because they are valid in the entire procedure, you can dynamically access the declared entities throughout the entire procedure. The following program example illustrates the different behavior:

METHOD demo_method.
  FIELD-SYMBOLS <field_symbol> TYPE any.
  ...
* ASSIGN dobj TO <field_symbol>. "Syntax error ...
  ASSIGN ('DOBJ') TO <field_symbol>. "No error
  ASSERT <field_symbol> IS ASSIGNED.
  ...
  DATA dobj TYPE i.
ENDMETHOD.

Because the different behavior of the dynamic and the static variant of the ASSIGN statement is rather unexpected, all declarations are supposed to be carried out at the beginning of the procedure, that is, between the introductory and the first executable statement. Then, the static and the dynamic visibility sections match.

This rule contradicts the common recommendations for other programming languages. They recommend declaring local variables as close to their use as possible to tightly restrict their validity area. In ABAP, however, there is no block-local validity of local variables. By positioning a declaration within the statement block of a loop, you cannot restrict the validity of this declaration to this statement block. Rather, the variable is valid within the entire procedure. So a declaration at the position where it is used can be misleading to developers or readers of a program who are not aware of this.

According to the rule, the size of a procedure should be selected in such a way that the procedure remains clear for the reader, so there is no good reason why you should not declare all variables as a whole at the beginning of a procedure.


Note

Within processing blocks that do not support any local data (dialog modules and event blocks), you must completely omit declarative statements.

In function modules and subroutines, there should be no local data, only a method call.

Bad example

The following source code shows a local data declaration in a loop. Readers who are familiar with another programming language or even the developer of the program himself would probably expect the number variable to be set to value 10 for each loop pass. Indeed, number is set to 10 exactly once when the method starts because the variable is generated only once for the context of the method and provided with a start value.

METHOD main.
  ...
  DO 10 TIMES.
    DATA number TYPE i VALUE 10.
    ...
    "number = 11, 13, 16, 20, ...
    number = number + sy-index.
    ...
  ENDDO.
  ...
ENDMETHOD.

Good example

The following source code shows the corrected version of the above example, which behaves as you would expect the above example to behave if you did not have any deeper ABAP knowledge. Because there is no block-local validity of data in ABAP, you must proceed as shown below.

METHOD main.
  DATA number TYPE i.
  ...
  DO 10 TIMES.
    number = 10.
    ...
    "number = 11, 12, 13, 14, ...
    number = number + sy-index.
    ...
  ENDDO.
  ...
ENDMETHOD.