Skip to content

Latest commit

 

History

History
57 lines (39 loc) · 1.59 KB

prefer-line-exists.md

File metadata and controls

57 lines (39 loc) · 1.59 KB

code pal for ABAP > Documentation > Prefer LINE_EXISTS or LINE_INDEX to READ TABLE or LOOP AT

Prefer LINE_EXISTS or LINE_INDEX to READ TABLE or LOOP AT

What is the intent of the check?

Prefer LINE_EXISTS or LINE_INDEX over READ TABLE or LOOP AT statements as they express the intent of the table access more specifically.

How to solve the issue?

Preferably, use LINE_EXISTS to check whether the row of an internal table exists, and LINE_INDEX to check the row index.

What to do in case of exception?

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.

Example

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' ] ) ).

Further Readings & Knowledge