SAP GENERIC PROGR - Guide
Get Example source ABAP code based on a different SAP table
GUIDELINE 6.52
Generic Programming
ABAP_BACKGROUND
Generic programming (dynamic generation of source code) makes programs as dynamic as possible. The following methods can be used to achieve this:
Dynamic token specification involves specifying individual operands or whole parts of statements (clauses) in the form of character-like data objects. These are usually enclosed in round brackets and must contain source code with correct syntax at runtime. Important examples:
Dynamic token specifications are often used in combination with
Program generation involves preparing complete programs as content for internal tables and then creating the programs. A distinction is made between the following cases:
ABAP_RULE
Program generation should only be used as a last resort for generic programming. Other dynamic methods (especially in application programs) should be tried first, such as dynamic token specification,
ABAP_DETAILS
Program generation has a lot of conceptual problems such as checking, testing and maintaining generated programs. In addition, programs generated hastily can be a security risk because they cannot be statically checked. Generating programs is usually very intensive in terms of runtime and resources. Due to the above reasons, program generation should be avoided wherever possible and other dynamic methods used instead:
These methods, combined with
Exception
Program generation should only be used as a last resort if the other methods are not sufficient to achieve dynamic program control. Another reason is the processing speed. When program generation is used, the costs incurred due to checking and generation occur less frequently than with the other dynamic methods. However, program generation is usually associated with worse system performance than dynamic token specification. The conceptual problems explained at the start are still applicable here and therefore careful consideration is needed.
Unlike application programs, system programs usually frequently rely on program generation and the associated language constructs. Examples include the generation of proxy classes for Web Dynpro or
Latest notes:
Even generated programs should always adhere to the predefined guidelines. For example, the program logic should be implemented/gene rated in a generated subroutine pool in the form of
To minimize the risks involved, it can be useful to save syntactically correct templates that adhere to the guidelines in the repository.
Subroutines in generated subroutine pools are an exception to the
Bad example
The following source text demonstrates unnecessary program generation. The only reason for generating the program is dynamic read access to a database table. The name of the database table and the row type of the internal table (into which data is read) are replaced by a parameter value in the source code of the program to be generated. The value operator
FIELD-SYMBOLS <(><<)>table> TYPE STANDARD TABLE.
program TYPE string,
mess TYPE string.
( `program.` )
( `class main definition.` )
( ` public section.` )
( ` class-data` )
( ` dyn_table type standard table of dyn_name.` )
( ` class-methods meth` )
( ` exporting table type ref to data.` )
( `endclass.` )
( `class main implementation.` )
( ` method meth.` )
( ` select *` )
( ` from dyn_name` )
( ` into table dyn_table.` )
( ` get reference of dyn_table into table.` )
( ` endmethod.` )
( `endclass.` )
( `form subr changing table type ref to data.` )
( ` main=>meth(` )
( ` importing table = table ).` )
( `endform.` ) ).
IN TABLE source WITH dbtab.
GENERATE SUBROUTINE POOL source NAME program MESSAGE mess.
IF sy-subrc = 0.
PERFORM subr IN PROGRAM (program) CHANGING table.
IF table IS BOUND.
ASSIGN table->* TO <(><<)>table>.
ENDIF.
ELSE.
...
Good example
The following source code produces the same result as the example above, when executed successfully. In other words, the field symbol
DATA table TYPE REF TO data.
FIELD-SYMBOLS <(><<)>table> TYPE STANDARD TABLE.
CREATE DATA table TYPE TABLE OF (dbtab).
ASSIGN table->* TO <(><<)>table>.
SELECT *
FROM (dbtab)
INTO TABLE <(><<)>table>.
CATCH cx_sy_create_data_error cx_sy_dynamic_osql_error.
...
ENDTRY.