Best Practices for Table Expressions (NW 7.4)

An official SAP presentation that discusses the new ABAP programming features in NetWeaver 7.4 (CD261) is of great importance with regard to table expressions, replacing the old syntax with reading from the internal table:

READ TABLE lt_aufk INTO ls_aufk WITH KEY aedat = sy-datum.
lv_order = ls_aufk-aufnr.

with one line lv_order = lt_aufk[ aedat = sy-datum ]-aufnr.

However, it is not mentioned that if a table expression cannot find a row, it throws an exception CX_SY_ITAB_LINE_NOT_FOUND, so it really is that the notation should be:

TRY.
        lv_order = lt_aufk[ aedat = sy-datum ]-aufnr.
    CATCH cx_sy_itab_line_not_found.
ENDTRY.

which is longer and makes any simple reading logic incredibly complex and hard to read. Since each individual reading may have to fail or succeed individually, these fast balloons are out of proportion:

TRY.
        wa1 = lt_itab[ col1 = ... ].
    CATCH cx_sy_itab_line_not_found.
ENDTRY.
TRY.
        wa2 = lt_itab[ col2 = ... ].
    CATCH cx_sy_itab_line_not_found.
ENDTRY.
TRY.
        wa3 = lt_itab[ col3 = ... ].
    CATCH cx_sy_itab_line_not_found.
ENDTRY.
TRY.
        wa4 = lt_itab[ col4 = ... ].
    CATCH cx_sy_itab_line_not_found.
ENDTRY.
TRY.
        wa5 = lt_itab[ col5 = ... ].
    CATCH cx_sy_itab_line_not_found.
ENDTRY.

Is there a way to improve the readability of these table expressions? Am I using them improperly?

+4
2

, ...

. , 2 : , , , , . , SUBRC, . - , - aedat , . , , , , .

, , DRY, .

, :

:

.

, CX_SY_ITAB_LINE_NOT_FOUND ,

  • ,
  • ASSIGN, sy-subrc 4,
  • line_exists, "false",
  • line_index, 0.

.

, SP08 :

itab [...] sy-subrc. , , , . .

VALUE REF, OPTIONAL DEFAULT. , OPTIONAL , DEFAULT , , .

TYPES:
  BEGIN OF line,
    id    TYPE i,
    value TYPE string,
  END OF line,
  itab TYPE SORTED TABLE OF line WITH UNIQUE KEY id.

DATA(def) = VALUE line( id = 0 value = `not found` ).


...

DATA(result) = VALUE #( itab[ id = ... ] DEFAULT def ).
+3

, catch

-1

All Articles