ABAP Keyword Documentation → ABAP Programming Guidelines → Robust ABAP → Data Types and Data Objects
Reference to Data Types or Data Objects
Other versions: 7.31 | 7.40 | 7.54
Background
As well as using data types
for declarations and typings with the addition TYPE
, you can use the alternative
addition LIKE
of the corresponding statements to directly reference the data
type of one of the data objects visible at this position. This includes references to data objects of
the same program, interface parameters of the current procedure, attributes of global classes and interfaces, and constants in type groups.
Rule
Declare dependent data objects with reference to other data objects
If a data object directly depends on another data object, refer to it directly using LIKE
for the declaration. In all other cases, use TYPE
to refer to a standalone data type.
Details
For example, if a helper variable of the type of an input parameter is required within a procedure
(method), you
should not declare it with reference to the type of the parameter using TYPE
but with reference to the parameter itself using LIKE
. You can also declare
work areas using LIKE LINE OF
if the parameter is an internal table. In the
case of typing with LIKE
, you can retroactively change the type of the parameter without always having to adapt the procedure implementation.
However, if no close reference to another data object exists, it is usually more useful to declare data objects with reference to a
standalone data type by using TYPE
.
Note
You should never implement an obsolete reference to flat structures or database tables or views of the ABAP Dictionary with LIKE
.
Bad example
The following source code shows the declaration of a helper variable in a method that is supposed to
be of the same data type as an interface parameter. The TYPE
reference to the data type requires a manual implementation of any type changes.
PUBLIC SECTION.
METHODS some_method
CHANGING some_parameter TYPE some_type.
...
ENDCLASS.
CLASS some_class IMPLEMENTATION.
METHOD some_method.
DATA save_parameter TYPE some_type.
save_parameter = some_parameter.
...
ENDMETHOD.
...
ENDCLASS.
Good example
The following source code shows the improved declaration of the helper variable that now directly refers
to the interface parameter with LIKE
, so that possible type changes are automatically accepted.
METHOD some_method.
DATA save_parameter LIKE some_parameter.
save_parameter = some_parameter.
...
ENDMETHOD.
...