ABAP Obsolete - Assignment and calculation
How does it actually work with the assignments and the calculation in ABAP? Here you will find out the current language constructs and what you should avoid.
Table of contents
In our series of obsolete statements, we deal with the topic of assignment and calculation today and show you a few new constructs that are delivered with the current releases. The issue of obsolete statements is particularly important in the cloud, since some things work differently under steampunk.
Assignments
An assignment in which the source and target types are different was previously carried out with a MOVE. With this, the system knew that it should convert to the target type as long as it's possible. But this command was no longer needed, a simple assignment is enough. Therefore, the statement is now completely obsolete and is no longer needed.
DATA:
ld_number TYPE n LENGTH 10 VALUE 6513,
ld_integer TYPE i.
" Old style
MOVE ld_number TO ld_integer.
" New style
ld_integer = ld_number.
The COMPUTE command produces a similar result and can be omitted in the same way. This command is used less often than MOVE.
" Old style
COMPUTE ld_number = ld_integer.
" New style
ld_integer = ld_number.
For assignments, as in other programming languages, you can therefore simply make the assignment. It will automatically try to convert from the source to the target types if possible. Else you will get a shortdump.
Calculation
Even if the statements for the calculation are a bit dusty and actually still come from Cobol, they are still available in current ABAP coding. Furthermore, you can sometimes find them in older code. You should no longer use this form of calculation. Here you tend to use the generally accepted style of calculating with two variables.
" Old style
ADD 1 TO ld_number.
SUBTRACT 1 FROM ld_number.
MULTIPLY ld_number BY 2.
DIVIDE ld_number BY 4.
" New style
ld_number = ld_number + 1.
ld_number = ld_number - 1.
ld_number = ld_number * 2.
ld_number = ld_number / 4.
With the last major HANA release 1909 (last year), the typical constructs from other programming languages now work. This shortens typing, especially with longer variable names and provides more clarity. But be careful if you are working on backwards compatible code! Not that you run into problems with older versions there.
" New style Release > 7.54 or HANA 1909
ld_number += 1.
ld_number -= 1.
ld_number *= 2.
ld_number /= 4.
Conclusion
This means that ABAP is moving further and further away from its actual basis, Cobol, and is opening up more to general programming languages such as Java. For many younger developers, it is going in exactly the right direction and thus polishes up the dusty image somewhat.
Source:
SAP blog