code pal for ABAP > Documentation > Prefer LINE_EXISTS or LINE_INDEX to READ TABLE or LOOP AT
Prefer LINE_EXISTS
or LINE_INDEX
over READ TABLE
or LOOP AT
statements as they express the intent of the table access more specifically.
Preferably, use LINE_EXISTS
to check whether the row of an internal table exists, and LINE_INDEX
to check the row index.
In exceptional cases, you can suppress this finding by using the pseudo comment "#EC PREF_LINE_EX
:
READ TABLE my_table TRANSPORTING NO FIELDS WITH KEY key = 'A'. "#EC PREF_LINE_EX
LOOP AT my_table REFERENCE INTO DATA(line) WHERE key = 'A'. "#EC PREF_LINE_EX
...
ENDLOOP.
Before the check:
READ TABLE my_table TRANSPORTING NO FIELDS WITH KEY key = 'A'.
IF sy-subrc = 0.
line_index = sy-tabix.
line_exists = abap_true.
ENDIF.
LOOP AT my_table REFERENCE INTO DATA(line) WHERE key = 'A'.
line_index = sy-tabix.
line_exists = abap_true.
EXIT.
ENDLOOP.
After the check:
DATA(index) = line_index( my_table[ key = 'A' ] ).
DATA(exists) = xsdbool( line_exists( my_table[ key = 'A' ] ) ).