SAP NetWeaver AS ABAP Release 751, ©Copyright 2017 SAP AG. All rights reserved.
ABAP - Keyword Documentation → ABAP Programming Guidelines → Robust ABAP → Assignments, Calculations, and Other Types of Data Access →Casting
Background
Casting refers to the process of handling a data object by assuming a certain data type. This definition is different to the meaning of the concept in other programming languages, such as Java. Here, casting means a different concept which is referred to as 'conversion' in ABAP. Casting in ABAP is either explicit or implicit:
Rule
Avoid implicit casting
Avoid implicit casting. If a cast to another data type is required, it can usually be implemented explicitly using ASSIGN ... CASTING.
Details
Implicit casting can potentially occur if structures are used as follows:
The use of implicit casting is prone to errors and produces source code that is difficult to understand. If the CASTING addition is used when handling field symbols, explicit casting can be implemented, which is easier to follow and understand. The explicit casting option is a very important reason to use field symbols.
Bad Example
The following source code shows the assignment of a text string to a structure with only character-like components. An implicit casting occurs here, which is regarded as unwanted according to the above rule. The entire structure is handled as a text field of type c and length 6.
Good Example
The following source code improves the example above, by assigning the structure to a field symbol of type c. Explicit casting occurs. Only the character-like field symbol is used to handle the structure as a character-like field.