SAP CODE INSPECTOR - Guide

Get Example source ABAP code based on a different SAP table
  


GUIDELINE 3.8

Code Inspector

ABAP_BACKGROUND
The Code Inspector tool performs a static check of Repository objects regarding performance, security, syntax, and adherence to naming conventions. You can call transaction SCI to use the full range of functions of the Code Inspector to perform complex static checks and regular mass tests for large numbers of development objects.
You can also call the Code Inspector from the ABAP Workbench to perform a standard set of checks for your current object, for example by choosing Program -> Check -> Code Inspector in the ABAP Editor. The standard check variant used here contains most of the checks from the extended program check, as well as a few additional security and performance checks. The Code Inspector can also be integrated into the release of transports.
As in the extended program check, the results of the Code Inspector are divided into three categories (errors, warnings, and simple messages), which you can hide using special pseudo comments.

ABAP_RULE
Using the Standard Check Variant of the Code Inspector
Perform the standard check variant of the Code Inspector before you release a program, and correct all error messages.

ABAP_DETAILS
If you are using the extended program check, the standard check variant of the Code Inspector only reports messages and checks that are not covered by the extended program check. These are mainly messages relating to potential performance or security risks in programs. Examples are messages about unfavorable WHERE conditions in a SELECT, a pass by value instead of a pass by reference for parameters, or unsecure program calls.
Compared with the messages of the extended program check, it is not always so easy to correct these problems at their source, perhaps because there is no other option for a selection, or because the transparency or robustness of a construct is seen as more important than a small potential loss of performance.
In such cases, the messages can be suppressed using the appropriate pseudo comments. A pseudo comment is a clear indication to the reader of a program that the program author has performed the relevant checks and has explicitly suppressed the message for a good reason. If necessary, pseudo comments can be substantiated by additional normal comments.

Bad example
A standard Code Inspector run for the example class below issues warnings because an internal table is returned by pass by value and an inner join for database tables with activated SAP buffering is used in the SELECT statement. CLASS class DEFINITION FINAL.
PUBLIC SECTION.
TYPES: BEGIN OF docu_wa,
object TYPE dokil-object,
dokldate TYPE dokhl-dokldate,
dokltime TYPE dokhl-dokltime,
END OF docu_wa,
docu_tab TYPE SORTED TABLE OF docu_wa
WITH NON-UNIQUE KEY object.
METHODS get_docu
IMPORTING VALUE(langu) TYPE sy-langu
EXPORTING VALUE(documents) TYPE docu_tab.
ENDCLASS.

CLASS class IMPLEMENTATION.
METHOD get_docu.
SELECT d~object h~dokldate h~dokltime
FROM dokil AS d
INNER JOIN dokhl AS h
ON h~id = d~id AND
h~object = d~object AND
h~typ = d~typ AND
h~langu = d~langu AND
h~dokversion = d~version
INTO CORRESPONDING FIELDS OF TABLE documents
WHERE d~id = 'SD' AND
d~object LIKE 'AB%' AND
d~typ = 'E' AND
d~langu = langu.
ENDMETHOD.
ENDCLASS.

Good example
The source code below shows the corrected version of the above class, for which the Code Inspector no longer issues any messages.
The pass by value of the internal table is replaced by a pass by reference. For the transfer of the elementary parameter langu, the pass by value is left unchanged to ensure robustness. In the standard check used, it would not have triggered a warning in any case. If the Code Inspector displays a warning in a case such as this, it can be hidden by using the pseudo comment '#EC CI_VALPAR.
The inner join of the SELECT statement bypasses the SAP buffering; this would lead to performance problems if the method was called frequently. However let us assume (for purpose of this example) that the method is part of a larger application, where Shared Objects ensure that the selected data is buffered. In this case, you should use the inner join instead of other low-performance constructs, such as a nested SELECT loop. As a result, the warning of the Code Inspector is hidden using the pseudo comment #EC CI_BUFFJOIN. The reasons for this are described in a regular comment. CLASS class DEFINITION FINAL.
PUBLIC SECTION.
TYPES: BEGIN OF docu_wa,
object TYPE dokil-object,
dokldate TYPE dokhl-dokldate,
dokltime TYPE dokhl-dokltime,
END OF docu_wa,
docu_tab TYPE SORTED TABLE OF docu_wa
WITH NON-UNIQUE KEY object.
METHODS get_docu
IMPORTING VALUE(langu) TYPE sy-langu '#EC CI_VALPAR
EXPORTING REFERENCE(documents) TYPE docu_tab.
ENDCLASS.

CLASS class IMPLEMENTATION.
METHOD get_docu.
SELECT d~object h~dokldate h~dokltime '#EC CI_BUFFJOIN
FROM dokil AS d
INNER JOIN dokhl AS h 'Buffering is done
ON h~id = d~id AND 'by application
h~object = d~object AND 'with Shared Objects
h~typ = d~typ AND
h~langu = d~langu AND
h~dokversion = d~version
INTO CORRESPONDING FIELDS OF TABLE documents
WHERE d~id = 'SD' AND
d~object LIKE 'AB%' AND
d~typ = 'E' AND
d~langu = langu.
ENDMETHOD.
ENDCLASS.