abap news
Home » Modern ABAP Syntax Cheat Sheet: Old vs. New, with Releases

Modern ABAP Syntax Cheat Sheet: Old vs. New, with Releases

ABAPBy xabap Published
A long block of code becoming a short one

This page shows classic ABAP next to the modern form. Most of it needs ABAP 7.40; the release column tells you when a feature became available.

Release overview

Feature Since
Inline declarations, NEW, VALUE, CONV, CAST, REF, EXACT, COND, SWITCH 7.40 SP02
Table expressions itab[ ... ] 7.40 SP02
CORRESPONDING, FOR (table comprehensions), LET 7.40 SP05
REDUCE, FILTER, BASE, OPTIONAL, DEFAULT, xsdbool, grouping with GROUP BY 7.40 SP08
SELECT ... FROM @itab 7.52
RETURN expr documented for 7.58

Declarations and objects

" Classic
DATA lv_count TYPE i.
lv_count = lines( lt_items ).
CREATE OBJECT lo_calc EXPORTING iv_start = 10.

" Modern
DATA(lv_count2) = lines( lt_items ).
DATA(lo_calc2)  = NEW lcl_calc( iv_start = 10 ).

More in inline declarations.

Fill an internal table

" Classic
ls_item-id = 1.
ls_item-name = 'Cable'.
APPEND ls_item TO lt_items.
CLEAR ls_item.

" Modern: replace the content
lt_items = VALUE #( ( id = 1 name = 'Cable' )
                    ( id = 2 name = 'Adapter' ) ).

" Modern: keep existing rows and add one
lt_items = VALUE #( BASE lt_items ( id = 3 name = 'Plug' ) ).

Without BASE, the previous content is lost.

Read a row

" Classic
READ TABLE lt_items INTO ls_item WITH KEY id = 2.
IF sy-subrc = 0.
  WRITE ls_item-name.
ENDIF.

" Modern
DATA(ls_found) = VALUE #( lt_items[ id = 2 ] OPTIONAL ).

Without OPTIONAL a missing row raises CX_SY_ITAB_LINE_NOT_FOUND.

Transform and filter

" Classic
LOOP AT lt_items INTO ls_item WHERE active = abap_true.
  APPEND ls_item-name TO lt_names.
ENDLOOP.

" Modern
lt_names = VALUE #( BASE lt_names
                    FOR ls IN lt_items WHERE ( active = abap_true )
                    ( ls-name ) ).

If the condition fits a key, FILTER is an alternative.

Sum values

" Classic
CLEAR lv_total.
LOOP AT lt_items INTO ls_item.
  lv_total = lv_total + ls_item-price.
ENDLOOP.

" Modern
lv_total = REDUCE #( INIT sum = 0
                     FOR ls IN lt_items
                     NEXT sum = sum + ls-price ).

INIT sum = 0 creates an integer. For packed prices write INIT sum TYPE ty_price, otherwise decimals are rounded. To sum per key, use COLLECT.

Conditions

" Classic
IF lv_score >= 50.
  lv_result = 'Pass'.
ELSE.
  lv_result = 'Fail'.
ENDIF.

" Modern
lv_result = COND #( WHEN lv_score >= 50 THEN 'Pass' ELSE 'Fail' ).
lv_text   = SWITCH #( lv_status WHEN 'O' THEN 'Open'
                                WHEN 'C' THEN 'Closed'
                                ELSE 'Other' ).
lv_is_valid = xsdbool( lv_score >= 50 ).

Strings

" Classic
CONCATENATE 'Order' lv_id 'saved' INTO lv_message SEPARATED BY space.

" Modern
lv_message = |Order { lv_id } saved|.
lv_iso     = |{ sy-datum DATE = ISO }|.
lv_padded  = |{ lv_matnr ALPHA = IN }|.

Copy between structures

" Classic
MOVE-CORRESPONDING ls_source TO ls_target.

" Modern
ls_target = CORRESPONDING #( ls_source MAPPING customer = kunnr ).
ls_target = CORRESPONDING #( BASE ( ls_target ) ls_source ).

CORRESPONDING without BASE clears the target fields that have no match. MOVE-CORRESPONDING keeps them.

ABAP SQL

" Classic
SELECT carrid carrname FROM scarr
  INTO TABLE lt_carriers
  WHERE currcode = lv_currency.

" Modern
SELECT carrid, carrname
  FROM scarr
  WHERE currcode = @lv_currency
  INTO TABLE @DATA(lt_result).

Modern ABAP SQL uses comma-separated columns, @ before ABAP variables and INTO at the end. See also JOIN @itab vs. FOR ALL ENTRIES.

Boolean method calls

" Classic
IF lo_order->is_open( ) IS NOT INITIAL.
ENDIF.

" Modern (predicative method call, 7.40 SP08)
IF lo_order->is_open( ).
ENDIF.

A predicative method call checks whether the result is not initial. It is equivalent to = abap_true only if the method returns nothing but abap_true or abap_false.

Do not modernize blindly

  • A nested COND with three levels is harder to read than an IF.
  • VALUE #( FOR ... ) copies rows. For large tables that are only read, a LOOP ... ASSIGNING avoids the copy.
  • Check the lowest release your code must run on.

Sources: Expressions in 7.40 SP02 · SP08 · ABAP SQL in 7.52