SAP / styleguides

This repository provides SAP style guides for coding and coding-related topics.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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