Sie sind auf Seite 1von 9

SAPtips4On ABAP/J2EE

Improving the Quality of Your ABAP Code


Page 1

June/July 2006 Volume IV Issue 3

Using the Code Inspector-


A Guide for SAP® Developers and Consultants
By Rehan Zaidi, Siemens Pakistan
Editor’s Note: We can always detail, the steps required in checking
count on ABAP Editor Rehan Zaidi repository objects such as ABAP pro-
to bring something substantial to grams, classes, and function groups.
the SAPtips table, and this article Finally, I will use an example drawn
is no exception. If you are an ABAP The Code Inspector from an easy-for-all company scenar-
developer you know the value of opti- io to illustrate my views. I am going
mizing your existing code as well as lets you check ABAP to include tips learned from my per-
learning tips and tricks to generate sonal experience and observation.
more efficient code in the future. In repository objects
this issue Rehan shows you how to This article is intended primarily
utilize SAP’s ABAP Code Inspector to including programs, for SAP developers. I will assume
check repository objects (in terms of that the reader is familiar with basic
performance), security, syntax, and classes, and ABAP concepts and syntax. For
naming conventions. Conducting an more information, refer to the SAP
inspection of this type will help you function modules. documentation on http://help.sap.com/.
identify potential coding flaws. It will Though the screenshots have been
also help you identify unnecessary or taken from Release 4.7, the tips men-
redundant tasks that would add pro- tioned are relevant for later releases
cessing time to your routines. Rehan as well.
presents an overview of the Code Due to the ease and control with
Inspector and demonstrates how which you can test a large number of The Code Inspector:
to call it using either a transaction programs, learning the basics of this An Overview
code or a menu path. He describes tool is necessary for developers and The Code Inspector is a tool (avail-
the types of errors the Code Inspec- consultants. able in the ABAP Workbench) that
tor may identify, and explains the lets you check ABAP repository
hints it may suggest for improving The aim of this article is to provide objects including ABAP programs,
the ABAP code. Having explained a description of the Code Inspector classes, and function modules. These
the basics, Rehan then takes you and the way it may be used to check checks highlight problems related to
through an example of how he has ABAP programs. These are some of the syntax, security, and performance
SAPtipsJournal

used this tool, complete with screen the questions that this article will of the ABAP code. In addition, you
shots and “lessons learned”. What’s address: may also search through the code for
next? Assemble the troops and pre- single words and/or patterns of ABAP
pare for inspection. • What are the two methods for call- statements. The objects that may be
ing the Code Inspector? checked using the Code Inspector are
Introduction shown in Figure 1.
SAP has empowered consultants • What are the errors that the Code
(and users) in all areas by provid- Inspector may identify?
ing tools for checking the quality of Function Groups
their work. The ABAP Workbench • What are some of the code improve- Programs
is no exception. One such tool avail- ment hints the Inspector may sug- Classes
able with the ABAP Workbench is gest? Interfaces
the Code Inspector. It allows you to
Type Groups
check the syntax and semantic con- I will begin with an overview of
sistency of ABAP programs, and also the Code Inspector and the benefits it
lets you improve their performance. provides to developers. I’ll discuss, in Figure 1: Objects That May Be Checked

SAPtips.com SAPtips © 2006 Klee Associates, Inc.


SAPtips4On ABAP/J2EE
Page 2

June/July 2006 Volume IV Issue 3

tion SCI or via a menu path of the


respective repository object mainte-
nance screen. With transaction SCI,
you:

a) first create an Object Set that


specifies the programs or classes
to be checked,

b) then define a Variant for specify-


ing the checks that are to be car-
ried out, and

c) finally, create and execute an


exception that is used to gener-
ate the check results. The Vari-
ants, Object Sets, and Inspection
may be defined as local (pertinent
Figure 2: The Code Inspector Output to one user) or global (accessible
by all users) depending on the
scenario.
The output (result) of the Inspector • It gives you total control over the
is in the form of a hierarchy show- tests or functions to be executed. On the other hand, you may also
ing the various check categories, as call the Code Inspector from the
shown in Figure 2. • For exceptional situations, you may ABAP Editor (transaction SE38), the
suppress the checks for a given Class Builder (transaction SE24),
The output contains three columns, statement by adding pseudo-com- and the Function Builder (transac-
each represented by a different color ments in your ABAP source code. tion SE37) using the menu path
(see Figure 3), showing the number shown in Figure 4.
of errors or warnings found by each There are two ways of calling the
check. Code Inspector, namely via transac-

Priority Color Codes Meaning


Red Error
Yellow Warning
SAPtipsJournal

Green Information

Figure 3: Priority Code Colors and Their Meaning

The Code Inspector offers numer-


ous advantages to consultants and
developers:

• Instead of carrying out manual


checks for programs individually,
you may check multiple objects
by defining Object Sets. The check
may be carried out either online
or in the background (for quick
performance). Figure 4: Accessing the Code Inspector from Transaction SE38

SAPtips.com SAPtips © 2006 Klee Associates, Inc.


SAPtips4On ABAP/J2EE
Page 3

June/July 2006 Volume IV Issue 3

In this case, if there is no


DEFAULT Variant defined
for your user, the system uses
the global Variant DEFAULT
while carrying out the inspec-
tion.

Calling the Code In-


spector with Transac-
tion SCI
As already mentioned,
there are two ways of call-
ing the Code Inspector for
checking your ABAP code.
The more powerful option
is using the transaction code
SCI. The main screen of this
transaction is shown in Fig-
ure 5.

Before creating an inspec-


tion, you need to define
a suitable Object Set and
Check Variant. Enter a suit-
able name for your Object Set
and then click on the Create
Figure 5: Code Inspector -- Main Screen of Transaction SCI
icon. This takes you to the
screen shown in Figure 6.

Enter a description for


your Object Set. You also
need to enter suitable values
in the Object Assignment and
Object Selection areas. The
Object Assignment area lets
you define your selection by
specifying the user who cre-
SAPtipsJournal

ated the objects in question,


or on the basis of the applica-
tion ID, software component,
and package. For example, if
you’d like to specify a set of
objects created by the user S_
JONES, you should enter the
same in the Person Respon-
sible field.

Figure 6: Defining an Object Set

SAPtips.com SAPtips © 2006 Klee Associates, Inc.


SAPtips4On ABAP/J2EE
Page 4

June/July 2006 Volume IV Issue 3

The Object Selection area is


used to specify the programs,
classes, and function groups
that you want to include in
your selection. On the Class-
es, Func Groups tab, enter
the criteria for selecting the
object that you would like to
check. For example, if you
would like to select all pro-
grams that begin with the let-
ter Z, you should enter Z* in
the Program field. When you
have specified your selection,
save your entries by clicking
the Save button.

Next, you need to create or


choose a suitable Variant for
your inspection. On the main
screen of transaction SCI
(Figure 5), enter a name in
the Check Variant field, and
click the Create button. This
takes you to a screen similar
to the one shown in Figure
7.

Specify a suitable name


and description for your Figure 7: Creating and Executing a Check Variant
Variant. This screen displays
the entire range of checks
and functions available in the
Code Inspector. Use the checkboxes immediately. When the inspection is played. The output of the inspection
to select the checks that you want to complete, a message “The Inspection is then displayed, as shown in Figure
include in your program Inspection. was carried out successfully” is dis- 9.
SAPtipsJournal

Finally, you have to create


and execute an inspection for
your specified objects. On
the main screen of transac-
tion SCI, enter an appropri-
ate name for your inspection,
and click the Create button.
The screen appears, as shown
in Figure 8.

On this screen, enter the


name of your newly cre-
ated Check Variant, and the
Object Set, in the fields pro-
vided. Then, click the Execute
button to start the inspection Figure 8: Creating and Executing the Inspection

SAPtips.com SAPtips © 2006 Klee Associates, Inc.


SAPtips4On ABAP/J2EE
Page 5

June/July 2006 Volume IV Issue 3

• Statements after which the


return code (SY-SUBRC)
must be checked. The SY-
SUBRC denotes the suc-
cess (or outcome) of an
ABAP statement. In some
cases, use of the return
code value is absolutely
necessary.

• Constructs in which
authorization checks are
essential, but are not
used.

• Statements that use


Native SQL.

Figure 9: Code Inspector Output c) Performance Checks


As the name implies, these
checks identify statements
Checks and Functions In- shown in Figure 10. For example, (or constructs) that may lead
cluded in the Code Inspector an error is generated if a transac- to a decline in the program’s per-
The range of checks and functions tion referred to in a CALL Trans- formance. Some of these statements
available in the Code Inspector may action Statement does not exist. include:
be divided into four categories. Let
us discuss them in detail. • The Inspection function searches • CHECK statements based on
for statements that could cre- a table field within a SELECT
a) Syntactical Checks ate problems if the program is loop. This reads more data than
The first category deals with checks used for different languages. For is required, and may cause the
related to the ABAP syntax. In addi- example, statements that use program to get slow.
tion to the normal ABAP correctness, hard-coded text literals instead
the Code Inspector also carries out of text IDs. • Statements that bypass the buf-
an Extended Program Check, which fers residing on the application
includes the following: • It highlights any variables or server.
form routines that are declared
• It checks whether the state- in a given program but are never
SAPtipsJournal

ments that call external pro- used.


grams (or program com-
ponents) are used in the correct • The Inspector function also looks
manner. Some of the statements for statements used in an incor-
that fall in this category are rect context. For example, a
COMMIT WORK used within a
The Code Inspector
SELECT loop would give unde-
sirable results.
also carries out an
CALL TRANSACTION
SUBMIT Extended Program
CALL FUNCTION b) Security Checks
PERFORM IN (PROGRAM) The checks pertinent to this cat-
egory find statements (or constructs)
Check.
CALL SCREEN
SET PF-STATUS that may hamper the stability and
security of your SAP system. Typical
examples of these are:
Figure 10: Statements that Call External Pro-
gram Components

SAPtips.com SAPtips © 2006 Klee Associates, Inc.


SAPtips4On ABAP/J2EE
Page 6

June/July 2006 Volume IV Issue 3

• Select statements that


do not have a suitable
WHERE clause. In this
case, the system may not
be able to use an appro-
priate index and may
carry out a sequential
scan on the entire table,
thus resulting in extreme-
ly long runtimes.

d) Search Functions
In addition to the checks
mentioned, the Code Inspec-
tor allows you to search for
words (tokens) or entire state-
ments in your Object Set. The
search option also allows you
to base your search on pat-
terns using wildcards such
as “+”and “*”. For searching
a word or statement in your
Object Set, create a Variant
that is comprised of one (or
both) of the search functions
(see Figure 11). The Inspec-
tion is then created and exe-
cuted as already mentioned.

There are two search func- Figure 11: Specifying a Search Function
tions available, namely token
search and statement search.
The token search function allows * represents any number of words. ation. For example, our READ state-
you to locate single words (having at For example, consider the statement ment pattern may look like:
least a length of three characters) in pattern:
your program code. When using this READ TABLE + WITH KEY +ABC+ =+
search option, the characters + and * READ TABLE + WITH KEY + = +
SAPtipsJournal

have special meanings. The symbol + For the key field, the system
represents a single character within a In this case, the system searches for searches only for those fields that are
word, whereas * denotes a sequence all READ statements that find a table five characters long and have “ABC”
of characters. For example, if you row, on the basis of a single key field. in the center.
want to search for internal tables In other words, after the KEY clause,
having names ITAB1, ITAB2, etc., you may expect a table field name, Suppressing Checks for
you will write ITAB+ in the search followed by an “=”symbol and then Selected Code Statements
field. a comparison value. All READ state- When an inspection is executed, all
ments that use two or more field com- the checks selected for the specified
On the other hand, the Search parisons are omitted by this search. Variant are carried out. However, in
ABAP Statement Patterns lets you some exceptional circumstances, you
find sets of similar ABAP statements You may combine the rule men- may need to exclude a given program
within your ABAP code. In this case, tioned in the token search with the from the code inspection. If you want
the + and *may also be used to define statement search function in order to to skip a check for a given statement,
the search criteria. While search- define more powerful search criteria. you need to insert special check-spe-
ing for a statement, the wildcard + In this case, additional information is cific phrases known as pseudo-com-
denotes one word or token, whereas provided for the token in consider- ments, in the program code. Exam-

SAPtips.com SAPtips © 2006 Klee Associates, Inc.


SAPtips4On ABAP/J2EE
Page 7

June/July 2006 Volume IV Issue 3

ples are “#EC CI_NOWHERE and check, click on the icon on the Vari- Putting It All Together
“#EC CI_NOFIRST. This is inserted ant creation screen (see Figure 7). In this section, I will use the con-
at the end of the statement that is to This displays the documentation of cepts presented in this article to dem-
be skipped from the check. the given check that mentions the onstrate how the Code Inspector may
relevant pseudo-comment name, as be used to fulfill a simple require-
In order to find out which pseudo- shown in Figure 12. ment. Let us consider the following
comment is appropriate for a given scenario:

You are given the task of check-


ing the performance of all customer
programs (having names beginning
with the letter Z). In addition, you
also want to figure out how many
programs incorporate the SELECT
SINGLE statement.

In order to fulfill the requirement, a


Variant Z_MY_VARIANT is defined,
as shown in Figure 13.

The Performance Checks indicator


is selected, as the primary emphasis
of the test is on the performance of
the programs concerned. The Search
Function (Search Functs.) indicator
Figure 12: Documentation of a Code Inspector Check is also checked, and the pattern to be
searched is entered as:

SELECT SINGLE * WHERE *

An Object Set Z_MY_OBJECTS is


created, as shown in Figure 14.

Since we need a set of all programs


having names starting with Z, the
Program field in the Object Selec-
tion area is filled with Z*. Finally,
SAPtipsJournal

an inspection is created and executed


using the newly-created Variant and
Object Set. The output of the inspec-
tion is shown in Figure 15.

Figure 13: Creating the Z_MY_VARIANT Variant

SAPtips.com SAPtips © 2006 Klee Associates, Inc.


SAPtips4On ABAP/J2EE
Page 8

June/July 2006 Volume IV Issue 3

Conclusion
In this article, I discussed the
basics of the Code Inspector and
the steps required in order to test
the quality of ABAP programs.
Then, I listed the set of checks
and functions that the Inspec-
tor supports. Finally, I discussed
a requirement that may be ful-
filled via use of this tool. I hope
that this article will be a pre-
cious resource for you, and will
help you in checking the quality
of your programs in the least
possible time.

Rehan Zaidi, Senior SAP Con-


sultant, Siemens Pakistan.
Rehan has been involved in both
ABAP development and func-
tional configuration for SAP HR
implementations at multination-
al and local companies, and also
Figure 14: Creating the Object Set Z_MY_OBJECTS has experience with SAP Work-
flow. He has contributed articles
to the SAP Professional Journal,
the HR Expert newsletter, and
to the TechRepublic Website.
He is currently working on his
first book, specifically designed
for SAP HR Users and Manag-
ers, as well as a guide for ABAP/
Workflow Consultants titled
Figure 15: Output of Inspection “201 Interview Questions on
Workflow”. Rehan is the found-
er of the Web site www.siteofSAP.
SAPtipsJournal

com. Rehan’s email address is


Rehan.Zaidi@SAPtips.com. ≈

SAPtips.com SAPtips © 2006 Klee Associates, Inc.


SAPtipsJournal
The information in our publications and on our Website is the copyrighted work of Klee Associates, Inc. and is owned by Klee Associates, Inc. NO WARRANTY:
Page 9

June/July 2006 Volume IV Issue 3

This documentation is delivered as is, and Klee Associates, Inc. makes no warranty as to its accuracy or use. Any use of this documentation is at the risk of the user.
Although we make every good faith effort to ensure accuracy, this document may include technical or other inaccuracies or typographical errors. Klee Associates,
Inc. reserves the right to make changes without prior notice. NO AFFILIATION: Klee Associates, Inc. and this publication are not affiliated with or endorsed by SAP
AG, SAP AG software referenced on this site is furnished under license agreements between SAP AG and its customers and can be used only within the terms of such
agreements. SAP AG and mySAP are registered trademarks of SAP AG. All other product names used herein are trademarks or registered trademarks of their
respective owners.
SAPtipsJournal

SAPtips.com SAPtips © 2006 Klee Associates, Inc.