Modern ABAP

Inline Declarations with DATA() and FIELD-SYMBOLS()

(C) Brandeis Consulting

The Problem with Declarations

In many places in ABAP code, we need a variable with an exact data type.
In the past, this variable had to be declared beforehand.
This had several disadvantages:

  • The declaration takes time, especially when the exact data type first had to be determined.
  • The declaration takes up space in the source code without adding logic. For scalar types, it’s one line; for structures, it can quickly become many lines.
  • There is a strong coupling between different code sections, sometimes even across classes.
    TYPES: BEGIN OF linetype,
             task_id  TYPE zbc_tasks-task_id,
             status   TYPE zbc_tasks-status,
             solution TYPE zbc_tasks-solution,
           END OF linetype.

    DATA tasks TYPE STANDARD TABLE OF linetype
      WITH DEFAULT KEY.

    SELECT task_id,
           status,
           solution
      FROM zbc_tasks
      INTO TABLE @tasks.

    out->write( tasks ).

  ENDMETHOD.
(C) Brandeis Consulting

The Solution: Inline Declaration

Concept

At the point where a variable is first used — i.e. when a value is assigned — it is also declared.
Prerequisite: The data type must be uniquely* determined.

*At least to some extent.

    SELECT task_id,
           status,
           solution
      FROM zbc_tasks
      INTO TABLE @DATA(tasks).

    out->write( tasks ).
(C) Brandeis Consulting

Direct Comparison

With Prior Declaration

    TYPES: BEGIN OF linetype,
             task_id  TYPE zbc_tasks-task_id,
             status   TYPE zbc_tasks-status,
             solution TYPE zbc_tasks-solution,
           END OF linetype.

    DATA tasks TYPE STANDARD TABLE OF linetype
      WITH DEFAULT KEY.

    SELECT task_id,
           status,
           solution
      FROM zbc_tasks
      INTO TABLE @tasks.

    out->write( tasks ).

  ENDMETHOD.

With Inline Declaration

    SELECT task_id,
           status,
           solution
      FROM zbc_tasks
      INTO TABLE @DATA(tasks).

    out->write( tasks ).
(C) Brandeis Consulting

Context Matters

For inline declarations to work, the data type must be clear enough at the position of DATA(<variable>) within the assignment.

SAP refers to this as Declaration Positions.

(C) Brandeis Consulting

Examples: Inline Declarations with Simple Assignments

The data type can be clearly derived from the expression on the right-hand side.

DATA(textvar) = 'Character string'.

DATA(typedescr) = cl_abap_typedescr=>describe_by_data( some_data ).

DATA(gross_amount) = net_amount * (taxrate/100+1).

The data type automatically adapts if something changes on the right-hand side — e.g., when a factory method returns a different type or a string becomes longer.

(C) Brandeis Consulting

Examples: Inline Declarations in Method Calls

The data type is determined from the method declaration.

Method Declaration

    methods exporting_many_params
       exporting chicken type chickentype
                 cow     type cowtype
                 dog     type dogtype
                 bird    type birdtype.

With Inline Declaration

my_method( IMPORTING chicken = data(chicken)
                     cow     = data(cow)
                     dog     = data(dog)
                     bird    = data(bird) ).

With Prior Declaration

DATA chicken TYPE chickentype  .
DATA cow     TYPE cowtype      .
DATA dog     TYPE dogtype      .
DATA bird    TYPE birdtype     .

my_method( IMPORTING chicken =  chicken
                     cow     =  cow
                     dog     =  dog
                     bird    =  bird ).
(C) Brandeis Consulting

Examples: In LOOPs

Advantage in loops: no second definition needed.

DATA lt_tasks TYPE STANDARD TABLE OF zbc_tasks.
"DATA ls_tasks LIKE LINE OF lt_tasks.  "Unnecessary

...
LOOP AT tasks INTO DATA(ls_task).

ENDLOOP.

...
READ TABLE lt_tasks INTO DATA(ls_task) WITH KEY task_id = 10.
READ TABLE lt_tasks ASSIGNING FIELD-SYMBOL(<ls_task>) WITH KEY task_id = 10.
(C) Brandeis Consulting

And 100 Other Positions

  • Error handling: CATCH cx_root INTO DATA(exception)
  • Statements for string processing:
    • FIND
    • REPLACE
    • CONCATENATEINTO position
    • SPLIT
  • Timestamp handling:
    • GET TIME STAMP FIELD DATA(mytimestamp).

...
Full list in the SAP documentation

(C) Brandeis Consulting

Risks and Side Effects

  • There must not be a variable named DATA, otherwise DATA(vname) is interpreted as an offset or substring access.
  • Only internal tables of type Standard are created automatically.
    If optimized read access is required, you must explicitly declare the table type, e.g., as HASHED TABLE or SORTED TABLE with proper key definition and possibly a secondary index.
  • Inline declarations do not work in obsolete constructs.
  • Inline declarations can make the refactoring assistant’s job harder.
    If data is used outside the extracted section, the variable should be declared in advance.

(C) Brandeis Consulting