SAP NetWeaver AS ABAP Release 740, ©Copyright 2014 SAP AG. All rights reserved.
ABAP Keyword Documentation → ABAP Programming Guidelines → Robust ABAP → Modularization units →Typing of Formal Parameters
Background
The typing of formal parameters can be complete or generic. Formal parameters of methods must be, and formal parameters of function modules and subroutines should be, explicitly typed using the TYPE or LIKE addition. When you connect actual parameters to formal parameters, the system checks whether the data type of the actual parameter corresponds to the typing of the formal parameter.
For generic typing, a set of predefined generic types is available in ABAP, which are only intended for the typing of formal parameters and field symbols. Using them in any other way can lead either to errors or to missing properties being completed with default values. The generic types are: any, any table, c, clike, csequence, data, decfloat, hashed table, index table, n, numeric, object, simple, sorted table, standard table, table, x, and xsequence. Self-defined table types without completely specified table key are also generic.
Rule
Be as specific as possible when typing formal parameters
Be only as generic as necessary when typing formal parameters. Completely generic types (any) should be the exception rather than the rule.
Details
Absolute type security within a procedure can only be achieved with complete typing. It should always be used when providing a generic service is not a defined goal. It is much easier to carry out tests for non-generic services than for generic services.
A generically typed procedure interface usually involves more implementation effort within the procedure (method) to avoid runtime errors. Therefore, use the following principle when providing generic interfaces: as little generic typing as possible and as much generic typing as necessary. You should use specific generic types, such as numeric or csequence, instead of any or data, for example, if services are involved that are supposed to process numeric values or character strings.
Generic typing can be a pitfall if you are not aware that you have used generic typing instead of complete typing, because only the technical type properties are checked when an actual parameter is connected, but no component names, for example. This can lead to different behavior than expected.
Note
These rules for typing also apply to field symbols.
Bad Example
The example in the following source code shows the trap you can fall into, particularly when working with table types, if the table key is not completely specified in their declaration (in a program or in the ABAP Dictionary). Due to the missing key specification, the table type that is used to type the formal parameter of sort_itab is generic. While the first static sorting is successful, the second SORT statement fails and triggers a runtime error. For the dynamic component specification, the properties of the actual parameter apply to the formal parameter, and the actual parameter does not have the col2 component (this can also be tracked in the ABAP Debugger).
Good Example
The following source code shows a very simple correction of the typing in the above example. Because the primary table key is completely specified, the used type is no longer generic, and the dynamic sorting functions like the static sorting.