Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sandraros opened this issue May 7, 2024 · 3 comments
Open

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

sandraros opened this issue May 7, 2024 · 3 comments
Labels
clean-abap New Rule The issue or PR proposes an new rule or set of rules

Comments

@sandraros
Copy link

sandraros commented May 7, 2024

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 sandraros added clean-abap New Rule The issue or PR proposes an new rule or set of rules labels May 7, 2024
@samkumo
Copy link

samkumo commented Jul 9, 2024

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

@JozsefSzikszai
Copy link

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.

@pokrakam
Copy link
Contributor

pokrakam commented Jul 9, 2024

There is a small performance penalty, be especially wary of cx_sy_itab_line_not_found if a large number of failed lookups are expected. In 99.99% of cases this will be insignificant, but if it really matters then read table or line_exists are fastest.
https://community.sap.com/t5/application-development-blog-posts/abap-tips-checking-existence-within-an-internal-table/ba-p/13371859

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clean-abap New Rule The issue or PR proposes an new rule or set of rules
Projects
None yet
Development

No branches or pull requests

4 participants