Skip to content

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

Description

@sandraros

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    New RuleThe issue or PR proposes an new rule or set of rulesclean-abap

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions