"New rule" Tables > Prefer a Table Expression to READ TABLE
sandraros opened this issue · comments
Clean ABAP currently recommends to use functional constructs (Table Expression or line_exists
) over READ TABLE
, but it's a little bit lost here: https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#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....
Prefer to use Table Expressions (SAP blog
DATA(line) = value_pairs[ name = 'A' ]. " entry must exist DATA(line) = VALUE #( value_pairs[ name = 'A' ] OPTIONAL ). " entry can be missingthan
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
.