Skip to content

ABAP Keyword Documentation →  ABAP Programming Guidelines →  Robust ABAP →  Assignments, Calculations, and Other Types of Data Access 

Assignments Between Different Types

Other versions: 7.31 | 7.40 | 7.54

Background

ABAP allows a direct value assignment between data objects with different data types. There must be a suitable conversion rule and the content of the source field must be a meaningful value for the data type of the target field. If a suitable conversion rule is not found or the content of the source field is not suitable, an exception occurs.

Such conversions take place not only in direct assignments, but also in many operand positions and in particular in arithmetic calculations, if the specified operand does not have the data type expected at the position.

Rule

Avoiding Conversions

Where possible, perform value assignments between compatible data objects with the same data type.

Details

Type conversions incur additional runtime and may not always have the result intended by the developer. Therefore, you should only perform conversions between data objects with different data types if it is unavoidable. In particular, you should avoid conversions where the conversion rules lead to unexpected results.

Bad example

The following source text shows an arithmetic calculation involving two unnecessary conversions. First the text field literal '1' has to be converted to the calculation type i, then the result of the calculation has to be converted from type i to data type n. Such conversions lead to significant increases in runtime.

DATA index TYPE n LENGTH 4.
...
DO ... TIMES.
  index = sy-index - '1'.
  ...
ENDDO.

Good example

The following source text shows how code can be improved compared to the previous example, so that no conversions are necessary.

DATA index TYPE i.
...
DO ... TIMES.
  index = sy-index - 1.
  ...
ENDDO.