styleguides
styleguides copied to clipboard
"New rule" Tables > Prefer a Table Expression to READ TABLE
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".
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.
Prefer to use Table Expressions (SAP blog)
DATA(line) = value_pairs[ name = 'A' ]. " entry must exist otherwise cx_sy_itab_line_not_found DATA(line) = VALUE #( value_pairs[ name = 'A' ] OPTIONAL ). " entry can be missing
than
READ TABLE
:" anti-pattern READ TABLE value_pairs INTO DATA(line) WITH KEY name = '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 aSTANDARD
table andBINARY SEARCH
are used (after a preceding explicitSORT
), unless the table can be changed toSORTED
orHASHED
.
EDIT July 10th, 2024: little modifications after comments yesterday.