This is a test message to test the length of the message box.
Login
ABAP in Practice Modern ABAP
Created by Software-Heroes

ABAP in Practice - Modern ABAP

1388

In this small task we look at existing classic ABAP source code and try to optimize it according to Modern ABAP.



This task is about optimizing a piece of source code using ABAP tools from Modern ABAP. At the end you will find some suggestions from our side.

 

Introduction

Modern ABAP enables you as an ABAP developer to write previous ABAP code much more briefly. This reduces the effort required to read source code, but also the effort required to create new code. For this purpose, many new functional statements have been introduced into the language, which make previous language constructs obsolete.

 

Preparation

In preparation, we create a new class in the system and transfer the source code into the system. In the TASK method you will find the source code that is to be optimized, in the SUGGESTION method you can implement your suggestion.

CLASS zcl_bs_demo_puzzle DEFINITION
  PUBLIC FINAL
  CREATE PUBLIC.

  PUBLIC SECTION.
    INTERFACES if_oo_adt_classrun.

    TYPES: BEGIN OF ts_data,
             field1 TYPE c LENGTH 1,
             field2 TYPE c LENGTH 1,
             field3 TYPE c LENGTH 1,
             field4 TYPE c LENGTH 1,
             field5 TYPE c LENGTH 1,
             field6 TYPE c LENGTH 1,
           END OF ts_data.
    TYPES tt_data TYPE STANDARD TABLE OF ts_data WITH EMPTY KEY.

  PRIVATE SECTION.
    METHODS task
      IMPORTING it_some_data TYPE tt_data.

    METHODS suggestion
      IMPORTING it_some_data TYPE tt_data.

    METHODS do_something
      IMPORTING is_data TYPE ts_data.
ENDCLASS.


CLASS zcl_bs_demo_puzzle IMPLEMENTATION.
  METHOD if_oo_adt_classrun~main.
    DATA(lt_some_data) = VALUE tt_data(
        ( field1 = '1' field2 = '2' field3 = '3' field4 = '4' field5 = '5' field6 = '6' )
        ( field1 = ' ' field2 = '7' field3 = '8' field4 = '9' field5 = 'A' field6 = 'B' ) ).

    task( lt_some_data ).
    suggestion( lt_some_data ).
  ENDMETHOD.


  METHOD task.
    DATA ls_mapped TYPE ts_data.

    LOOP AT it_some_data INTO DATA(ls_some_data).
      CLEAR ls_mapped.
      MOVE-CORRESPONDING ls_some_data TO ls_mapped.

      CLEAR:
        ls_mapped-field3,
        ls_mapped-field4,
        ls_mapped-field6.

      do_something( ls_mapped ).
    ENDLOOP.
  ENDMETHOD.


  METHOD suggestion.
    " Start here
  ENDMETHOD.


  METHOD do_something.
    " Method to demonstrate further handlings
  ENDMETHOD.
ENDCLASS.

 

Task

The task now consists in optimizing the following source code. We process the data in the method, carry out a mapping and pass the data to the next method for processing. We now want to map the process using modern ABAP tools.

DATA ls_mapped TYPE ts_data.

LOOP AT it_some_data INTO DATA(ls_some_data).
  CLEAR ls_mapped.
  MOVE-CORRESPONDING ls_some_data TO ls_mapped.

  CLEAR:
    ls_mapped-field3,
    ls_mapped-field4,
    ls_mapped-field6.

  do_something( ls_mapped ).
ENDLOOP.

 

Hint: In the next section we will go into the solution; if you want to do the task on your own, you should pause here.

 

Solution

As a solution, we have two suggestions on how you can easily and elegantly depict the mapping with a statement. By using inline declarations and a constructor statement, we save ourselves the trouble of creating and deleting the variable in the first step.

 

Suggestion 1

In the first suggestion, we use Value to do the mapping. By specifying the type, we can create the variable using an inline declaration. Using the Base addition, we use the fields from LS_SOME_DATA and finally set the fields to empty, which replaces the CLEAR.

LOOP AT it_some_data INTO DATA(ls_some_data).
  DATA(ls_mapped) = VALUE ts_data( BASE CORRESPONDING #( ls_some_data ) field3 = '' field4 = '' field6 = '' ).
  do_something( ls_mapped ).
ENDLOOP.

 

Suggestion 2

The second suggestion is somewhat simpler and shorter. Here we only use CORRESPONDING and can also create the variable using an inline declaration. When mapping, we can use the EXCEPT addition to ignore the fields and leave them empty.

LOOP AT it_some_data INTO DATA(ls_some_data).
  DATA(ls_mapped) = CORRESPONDING ts_data( ls_some_data EXCEPT field3 field4 field6 ).
  do_something( ls_mapped ).
ENDLOOP.

 

Complete example

Here you can find the complete class with both options. Since the variant with Corresponding is shorter, it is also active in the code.

CLASS zcl_bs_demo_puzzle DEFINITION
  PUBLIC FINAL
  CREATE PUBLIC.

  PUBLIC SECTION.
    INTERFACES if_oo_adt_classrun.

    TYPES: BEGIN OF ts_data,
             field1 TYPE c LENGTH 1,
             field2 TYPE c LENGTH 1,
             field3 TYPE c LENGTH 1,
             field4 TYPE c LENGTH 1,
             field5 TYPE c LENGTH 1,
             field6 TYPE c LENGTH 1,
           END OF ts_data.
    TYPES tt_data TYPE STANDARD TABLE OF ts_data WITH EMPTY KEY.

  PRIVATE SECTION.
    METHODS task
      IMPORTING it_some_data TYPE tt_data.

    METHODS suggestion
      IMPORTING it_some_data TYPE tt_data.

    METHODS do_something
      IMPORTING is_data TYPE ts_data.
ENDCLASS.


CLASS zcl_bs_demo_puzzle IMPLEMENTATION.
  METHOD if_oo_adt_classrun~main.
    DATA(lt_some_data) = VALUE tt_data(
        ( field1 = '1' field2 = '2' field3 = '3' field4 = '4' field5 = '5' field6 = '6' )
        ( field1 = ' ' field2 = '7' field3 = '8' field4 = '9' field5 = 'A' field6 = 'B' ) ).

    task( lt_some_data ).
    suggestion( lt_some_data ).
  ENDMETHOD.


  METHOD task.
    DATA ls_mapped TYPE ts_data.

    LOOP AT it_some_data INTO DATA(ls_some_data).
      CLEAR ls_mapped.
      MOVE-CORRESPONDING ls_some_data TO ls_mapped.

      CLEAR:
        ls_mapped-field3,
        ls_mapped-field4,
        ls_mapped-field6.

      do_something( ls_mapped ).
    ENDLOOP.
  ENDMETHOD.


  METHOD suggestion.
    LOOP AT it_some_data INTO DATA(ls_some_data).
      DATA(ls_mapped) = CORRESPONDING ts_data( ls_some_data EXCEPT field3 field4 field6 ).
      do_something( ls_mapped ).
    ENDLOOP.

*    LOOP AT it_some_data INTO DATA(ls_some_data).
*      DATA(ls_mapped) = VALUE ts_data( BASE CORRESPONDING #( ls_some_data ) field3 = '' field4 = '' field6 = '' ).
*      do_something( ls_mapped ).
*    ENDLOOP.
  ENDMETHOD.


  METHOD do_something.
    " Method to demonstrate further handlings
  ENDMETHOD.
ENDCLASS.

 

Conclusion

Modern ABAP makes many development tasks easier and reduces the amount of source code that needs to be written without increasing its complexity. If you have another solution, please post it in the comments.


Included topics:
QuickABAP in PracticeModern ABAP
Comments (0)



And further ...

Are you satisfied with the content of the article? We post new content in the ABAP area every Friday and irregularly in all other areas. Take a look at our tools and apps, we provide them free of charge.


ABAP in Practice - String Processing

Category - ABAP

In this practical example we look at the string processing to determine the CDS names in CamelCase and how you can implement this with ABAP.

10/15/2024

ABAP in Practice - Test Driven Development

Category - ABAP

How does TDD actually work in practice and are there simple examples for learning in ABAP? In this exercise we will look at the practical part.

09/24/2024

ABAP in Practice - Merge data sets

Category - ABAP

How do we merge two different data sets in ABAP, especially with regard to Modern ABAP? A practical task for this topic.

09/17/2024

ABAP Quick - Performance Data Filtering

Category - ABAP

Which statement do you use in ABAP to filter internal tables and is it performant? Read more in this article.

08/13/2024

ABAP in Practice - Type Conversion

Category - ABAP

How would you perform this type conversion in ABAP? A practical example and a suggested solution.

07/16/2024