code pal for ABAP > Documentation > Scope of Variable
If a variable is declared inside a code block that forms a meaningful subdivision of scope (such as the branches of an IF
statement), it should be used only inside that subdivision. ABAP lacks this sort of subdivided scope handling on a language level and does not prevent such confusing accesses - the smallest scope in ABAP is the method (excluding helper variables in certain constructor expressions) and any variable declared in a method remains visible until the end of the method.
The check searches for variable declarations (DATA
, FIELD-SYMBOLS
) inside of IF
, ELSEIF
, ELSE
, DO
, CASE/WHEN
, LOOP
, and WHILE
blocks and for the usage of these variables outside these blocks.
Relocate the declaration if the access to the variable is intended or use a different variable for the outside access.
In exceptional cases, you can suppress this finding by using the pseudo comment "#EC SCOPE_OF_VAR
which should be placed right after the variable usage/reference:
IF has_entries = abap_true.
DATA(value) = 1.
ELSE.
value = 2. "#EC SCOPE_OF_VAR
ENDIF.
Before:
IF has_entries = abap_true.
DATA(value) = 1.
ELSE.
value = 2.
ENDIF.
After:
DATA(value) = 0.
IF has_entries = abap_true.
value = 1.
ELSE.
value = 2.
ENDIF.