ABAP Obsolete - Add
In today's article, we'll look at the obsolete language element Add, how the replacement looks like and what we like or dislike about it.
Table of contents
The language element of today's article is mainly found in many old programs in the system and should therefore not cause you too much headache. It comes in a very simple form to add a value to a variable ...
" old element
ADD 1 TO ld_num.
" new element
ld_num = ld_num 1.
... and in the more complex version that we want to discuss today.
Preparation
As a preparation, we take the example from the documentation of the object (more in the link at the end of the article) and create a structure that we fill. The example of the SAP documentation makes it easier to describe, but we first define the types, then create the variables and fill them.
" Types
TYPES:
tv_num TYPE p LENGTH 8 DECIMALS 0,
BEGIN OF ts_numbers,
one TYPE tv_num,
two TYPE tv_num,
three TYPE tv_num,
four TYPE tv_num,
five TYPE tv_num,
END OF ts_numbers.
" Variables
DATA:
ls_numbers TYPE ts_numbers,
ld_sum TYPE i.
FIELD-SYMBOLS:
< ld_num > TYPE tv_num.
" Initial values
ls_numbers = VALUE #( one = 10 two = 20 three = 30 four = 40 five = 50 ).
In the example from the official documentation, the fields that follow each other and have the same type are added together. In the OO context, however, this type of definition is no longer possible and also prohibited. The documentation also refers to a similar function with which we can achieve the same effect.
Execution
Unlike the original command, you have to do some more programming and coding to add. Although this contradicts the idea of the commands introduced so far, but it does make the function much clearer.
We loop the structure as often as defined in p_cnt. We start with the first element and add up all the values.
DO p_cnt TIMES.
DATA(ld_idx) = sy-index - 1.
ASSIGN ls_numbers-one INCREMENT ld_idx TO < ld_num > RANGE ls_numbers.
IF sy-subrc = 0.
ld_sum = ld_sum < ld_num >.
ENDIF.
ENDDO.
The Assign starts with the field "one" and index 0 from the range of the structure. We also want to include the first field in our addition and should not increase the index here.
Conclusion
Not every new language element is necessarily easier, to create the source code, but the new language elements make it easier for foreign developers to understand. In any case, with our little tip, you are well prepared if you should replace such a construct in an old program.
Source:
ABAP Documentation Add-Until