You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Clean ABAP currently recommends to use functional constructs (Table Expression or line_exists) over READ TABLE, but it's a little bit lost amongst other functional constructs in the chapter "General > Prefer functional to procedural language constructs".
DATA(line) = value_pairs[ name='A' ]. " entry must exist otherwise cx_sy_itab_line_not_foundDATA(line) =VALUE #( value_pairs[ name='A' ] OPTIONAL ). " entry can be missing
than READ TABLE:
" anti-patternREAD TABLE value_pairs INTODATA(line) WITH KEYname='A'.
Possibly, this exception can be added about the only case I know:
Note that READ TABLE cannot be replaced with a Table Expression in case both a STANDARD table and BINARY SEARCH are used (after a preceding explicit SORT), unless the table can be changed to SORTED or HASHED.
EDIT July 10th, 2024: little modifications after comments yesterday.
The text was updated successfully, but these errors were encountered:
Table Expressions also have other caveats which make them bit more complicated compared to READ TABLE. I use them regularly but it is important to keep in mind the limitations and quirks.
This is unsafe because you will get a dump if the line does not exists:
DATA(line) = value_pairs[ name='A' ]. " entry must exist
...so you need to use this one, which is longer and arguably more complex than READ TABLE:
DATA(line) =VALUE #( value_pairs[ name='A' ] OPTIONAL ). " entry can be missing
Unlike with READ TABLE, the SY-SUBRC is not set when using Table Expression to indicate whether record was found, so you can verify that only indirectly by checking if line is initial. This is crucial to keep in mind when working amongst legacy code where READ TABLE result is commonly checked with IF sy-subrc = 0
On a sidenote: When using table expressions, the exception class CX_SY_ITAB_LINE_NOT_FOUND can be used (instead of sy-subrc) for checking, whether an entry was found:
TRY.
DATA(line) = value_pairs[ name = 'A' ].
" entry was found
CATCH cx_sy_itab_line_no_found.
" Entry was not found
ENDTRY.
Clean ABAP currently recommends to use functional constructs (Table Expression or
line_exists
) overREAD TABLE
, but it's a little bit lost amongst other functional constructs in the chapter "General > Prefer functional to procedural language constructs".In Tables, there's a dedicated chapter Prefer LINE_EXISTS to READ TABLE or LOOP AT but there's no dedicated chapter for Table Expressions. It's important to have one because it's a frequent discussion among ABAP developers whether
READ TABLE
should be systematically used or not, and probably people won't look at General > Prefer functional to procedural language constructs.Possibly, this exception can be added about the only case I know:
EDIT July 10th, 2024: little modifications after comments yesterday.
The text was updated successfully, but these errors were encountered: