styleguides icon indicating copy to clipboard operation
styleguides copied to clipboard

"New rule" Tables > Prefer a Table Expression to READ TABLE

Open sandraros opened this issue 9 months ago • 3 comments

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 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.

sandraros avatar May 07 '24 12:05 sandraros