9
SAPtips Page 1 SAPtips Journal June/July 2006 Volume IV Issue 3 SAPtips.com SAPtips © 2006 Klee Associates, Inc. Editor’s Note: We can always count on ABAP Editor Rehan Zaidi to bring something substantial to the SAPtips table, and this article is no exception. If you are an ABAP developer you know the value of opti- mizing your existing code as well as learning tips and tricks to generate more efficient code in the future. In this issue Rehan shows you how to utilize SAP’s ABAP Code Inspector to check repository objects (in terms of performance), security, syntax, and naming conventions. Conducting an inspection of this type will help you identify potential coding flaws. It will also help you identify unnecessary or redundant tasks that would add pro- cessing time to your routines. Rehan presents an overview of the Code Inspector and demonstrates how to call it using either a transaction code or a menu path. He describes the types of errors the Code Inspec- tor may identify, and explains the hints it may suggest for improving the ABAP code. Having explained the basics, Rehan then takes you through an example of how he has used this tool, complete with screen shots and “lessons learned”. What’s next? Assemble the troops and pre- pare for inspection. Introduction SAP has empowered consultants (and users) in all areas by provid- ing tools for checking the quality of their work. The ABAP Workbench is no exception. One such tool avail- able with the ABAP Workbench is the Code Inspector. It allows you to check the syntax and semantic con- sistency of ABAP programs, and also lets you improve their performance. 4On ABAP/J2EE Improving the Quality of Your ABAP Code Using the Code Inspector- A Guide for SAP ® Developers and Consultants By Rehan Zaidi, Siemens Pakistan Due to the ease and control with which you can test a large number of programs, learning the basics of this tool is necessary for developers and consultants. The aim of this article is to provide a description of the Code Inspector and the way it may be used to check ABAP programs. These are some of the questions that this article will address: What are the two methods for call- ing the Code Inspector? What are the errors that the Code Inspector may identify? What are some of the code improve- ment hints the Inspector may sug- gest? I will begin with an overview of the Code Inspector and the benefits it provides to developers. I’ll discuss, in detail, the steps required in checking repository objects such as ABAP pro- grams, classes, and function groups. Finally, I will use an example drawn from an easy-for-all company scenar- io to illustrate my views. I am going to include tips learned from my per- sonal experience and observation. This article is intended primarily for SAP developers. I will assume that the reader is familiar with basic ABAP concepts and syntax. For more information, refer to the SAP documentation on http://help.sap.com/. Though the screenshots have been taken from Release 4.7, the tips men- tioned are relevant for later releases as well. The Code Inspector: An Overview The Code Inspector is a tool (avail- able in the ABAP Workbench) that lets you check ABAP repository objects including ABAP programs, classes, and function modules. These checks highlight problems related to the syntax, security, and performance of the ABAP code. In addition, you may also search through the code for single words and/or patterns of ABAP statements. The objects that may be checked using the Code Inspector are shown in Figure 1. Function Groups Programs Classes Interfaces Type Groups Figure 1: Objects That May Be Checked The Code Inspector lets you check ABAP repository objects including programs, classes, and function modules.

Code Inspector

Embed Size (px)

Citation preview

Page 1: Code Inspector

SAPtips

Page

1

SA

Pti

ps J

ourn

alJu

ne/Ju

ly 20

06 V

olum

e IV

Issue

3

SAPtips.com SAPtips © 2006 Klee Associates, Inc.

Editor’s Note: We can always count on ABAP Editor Rehan Zaidi to bring something substantial to the SAPtips table, and this article is no exception. If you are an ABAP developer you know the value of opti-mizing your existing code as well as learning tips and tricks to generate more efficient code in the future. In this issue Rehan shows you how to utilize SAP’s ABAP Code Inspector to check repository objects (in terms of performance), security, syntax, and naming conventions. Conducting an inspection of this type will help you identify potential coding flaws. It will also help you identify unnecessary or redundant tasks that would add pro-cessing time to your routines. Rehan presents an overview of the Code Inspector and demonstrates how to call it using either a transaction code or a menu path. He describes the types of errors the Code Inspec-tor may identify, and explains the hints it may suggest for improving the ABAP code. Having explained the basics, Rehan then takes you through an example of how he has used this tool, complete with screen shots and “lessons learned”. What’s next? Assemble the troops and pre-pare for inspection.

IntroductionSAP has empowered consultants

(and users) in all areas by provid-ing tools for checking the quality of their work. The ABAP Workbench is no exception. One such tool avail-able with the ABAP Workbench is the Code Inspector. It allows you to check the syntax and semantic con-sistency of ABAP programs, and also lets you improve their performance.

4On ABAP/J2EE

Improving the Quality of Your ABAP Code Using the Code Inspector-

A Guide for SAP® Developers and ConsultantsBy Rehan Zaidi, Siemens Pakistan

Due to the ease and control with which you can test a large number of programs, learning the basics of this tool is necessary for developers and consultants.

The aim of this article is to provide a description of the Code Inspector and the way it may be used to check ABAP programs. These are some of the questions that this article will address:

• What are the two methods for call-ing the Code Inspector?

• What are the errors that the Code Inspector may identify?

• What are some of the code improve-

ment hints the Inspector may sug-gest?

I will begin with an overview of the Code Inspector and the benefits it provides to developers. I’ll discuss, in

detail, the steps required in checking repository objects such as ABAP pro-grams, classes, and function groups. Finally, I will use an example drawn from an easy-for-all company scenar-io to illustrate my views. I am going to include tips learned from my per-sonal experience and observation.

This article is intended primarily for SAP developers. I will assume that the reader is familiar with basic ABAP concepts and syntax. For more information, refer to the SAP documentation on http://help.sap.com/. Though the screenshots have been taken from Release 4.7, the tips men-tioned are relevant for later releases as well.

The Code Inspector:An Overview

The Code Inspector is a tool (avail-able in the ABAP Workbench) that lets you check ABAP repository objects including ABAP programs, classes, and function modules. These checks highlight problems related to the syntax, security, and performance of the ABAP code. In addition, you may also search through the code for single words and/or patterns of ABAP statements. The objects that may be checked using the Code Inspector are shown in Figure 1.

Function Groups ProgramsClassesInterfacesType Groups

Figure 1: Objects That May Be Checked

The Code Inspector

lets you check ABAP

repository objects

including programs,

classes, and

function modules.

Page 2: Code Inspector

SAPtips

Page

2

SA

Pti

ps J

ourn

alJu

ne/Ju

ly 20

06 V

olum

e IV

Issue

3

SAPtips.com SAPtips © 2006 Klee Associates, Inc.

The output (result) of the Inspector is in the form of a hierarchy show-ing the various check categories, as shown in Figure 2.

The output contains three columns,

each represented by a different color (see Figure 3), showing the number of errors or warnings found by each check.

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).

• It gives you total control over the tests or functions to be executed.

• For exceptional situations, you may suppress the checks for a given statement by adding pseudo-com-ments in your ABAP source code.

There are two ways of calling the Code Inspector, namely via transac-

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 to one user) or global (accessible by all users) depending on the scenario.

On the other hand, you may also call the Code Inspector from the ABAP Editor (transaction SE38), the Class Builder (transaction SE24), and the Function Builder (transac-tion SE37) using the menu path shown in Figure 4.

4On ABAP/J2EE

Figure 2: The Code Inspector Output

Priority Color Codes Meaning

Red ErrorYellow Warning Green Information

Figure 3: Priority Code Colors and Their Meaning

Figure 4: Accessing the Code Inspector from Transaction SE38

Page 3: Code Inspector

SAPtips

Page

3

SA

Pti

ps J

ourn

alJu

ne/Ju

ly 20

06 V

olum

e IV

Issue

3

SAPtips.com SAPtips © 2006 Klee Associates, Inc.

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 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-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.

4On ABAP/J2EE

Figure 5: Code Inspector -- Main Screen of Transaction SCI

Figure 6: Defining an Object Set

Page 4: Code Inspector

SAPtips

Page

4

SA

Pti

ps J

ourn

alJu

ne/Ju

ly 20

06 V

olum

e IV

Issue

3

SAPtips.com SAPtips © 2006 Klee Associates, Inc.

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 Variant. This screen displays the entire range of checks and functions available in the Code Inspector. Use the checkboxes to select the checks that you want to include in your program Inspection.

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

immediately. When the inspection is complete, a message “The Inspection was carried out successfully” is dis-

played. The output of the inspection is then displayed, as shown in Figure 9.

4On ABAP/J2EE

Figure 7: Creating and Executing a Check Variant

Figure 8: Creating and Executing the Inspection

Page 5: Code Inspector

SAPtips

Page

5

SA

Pti

ps J

ourn

alJu

ne/Ju

ly 20

06 V

olum

e IV

Issue

3

SAPtips.com SAPtips © 2006 Klee Associates, Inc.

Checks and Functions In-cluded in the Code Inspector

The range of checks and functions available in the Code Inspector may be divided into four categories. Let us discuss them in detail.

a) Syntactical ChecksThe first category deals with checks

related to the ABAP syntax. In addi-tion to the normal ABAP correctness, the Code Inspector also carries out an Extended Program Check, which includes the following:

• It checks whether the state-ments that call external pro-grams (or program com-ponents) are used in the correct manner. Some of the statements that fall in this category are

shown in Figure 10. For example, an error is generated if a transac-tion referred to in a CALL Trans-action Statement does not exist.

• The Inspection function searches

for statements that could cre-ate problems if the program is used for different languages. For example, statements that use hard-coded text literals instead of text IDs.

• It highlights any variables or form routines that are declared in a given program but are never used.

• The Inspector function also looks for statements used in an incor-rect context. For example, a COMMIT WORK used within a SELECT loop would give unde-sirable results.

b) Security ChecksThe checks pertinent to this cat-

egory find statements (or constructs) that may hamper the stability and security of your SAP system. Typical examples of these are:

4On ABAP/J2EE

Figure 9: Code Inspector Output

• 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.

c) Performance ChecksAs the name implies, these

checks identify statements (or constructs) that may lead

to a decline in the program’s per-formance. Some of these statements include:

• CHECK statements based on a table field within a SELECT loop. This reads more data than is required, and may cause the program to get slow.

• Statements that bypass the buf-

fers residing on the application server.

CALL TRANSACTION SUBMITCALL FUNCTIONPERFORM IN (PROGRAM)CALL SCREENSET PF-STATUS

Figure 10: Statements that Call External Pro-gram Components

The Code Inspector

also carries out an

Extended Program

Check.

Page 6: Code Inspector

SAPtips

Page

6

SA

Pti

ps J

ourn

alJu

ne/Ju

ly 20

06 V

olum

e IV

Issue

3

SAPtips.com SAPtips © 2006 Klee Associates, Inc.

• 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 FunctionsIn 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-tions available, namely token search and statement search. The token search function allows you to locate single words (having at least a length of three characters) in your program code. When using this search option, the characters + and * have special meanings. The symbol + represents a single character within a word, whereas * denotes a sequence of characters. For example, if you want to search for internal tables having names ITAB1, ITAB2, etc., you will write ITAB+ in the search field.

On the other hand, the Search ABAP Statement Patterns lets you find sets of similar ABAP statements within your ABAP code. In this case, the + and *may also be used to define the search criteria. While search-ing for a statement, the wildcard + denotes one word or token, whereas

* represents any number of words. For example, consider the statement pattern:

READ TABLE + WITH KEY + = +

In this case, the system searches for all READ statements that find a table row, on the basis of a single key field. In other words, after the KEY clause, you may expect a table field name, followed by an “=”symbol and then a comparison value. All READ state-ments that use two or more field com-parisons are omitted by this search.

You may combine the rule men-

tioned in the token search with the statement search function in order to define more powerful search criteria. In this case, additional information is provided for the token in consider-

ation. For example, our READ state-ment pattern may look like:

READ TABLE + WITH KEY +ABC+ = +

For the key field, the system searches only for those fields that are five characters long and have “ABC” in the center.

Suppressing Checks for Selected Code Statements

When an inspection is executed, all the checks selected for the specified Variant are carried out. However, in some exceptional circumstances, you may need to exclude a given program from the code inspection. If you want to skip a check for a given statement, you need to insert special check-spe-cific phrases known as pseudo-com-ments, in the program code. Exam-

Figure 11: Specifying a Search Function

4On ABAP/J2EE

Page 7: Code Inspector

SAPtips

Page

7

SA

Pti

ps J

ourn

alJu

ne/Ju

ly 20

06 V

olum

e IV

Issue

3

SAPtips.com SAPtips © 2006 Klee Associates, Inc.

ples are “#EC CI_NOWHERE and “#EC CI_NOFIRST. This is inserted at the end of the statement that is to be skipped from the check.

In order to find out which pseudo-comment is appropriate for a given

check, click on the icon on the Vari-ant creation screen (see Figure 7). This displays the documentation of the given check that mentions the relevant pseudo-comment name, as shown in Figure 12.

Figure 12: Documentation of a Code Inspector Check

Putting It All TogetherIn this section, I will use the con-

cepts presented in this article to dem-onstrate how the Code Inspector may be used to fulfill a simple require-ment. Let us consider the following 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 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, 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

4On ABAP/J2EE

Page 8: Code Inspector

SAPtips

Page

8

SA

Pti

ps J

ourn

alJu

ne/Ju

ly 20

06 V

olum

e IV

Issue

3

SAPtips.com SAPtips © 2006 Klee Associates, Inc.

ConclusionIn 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 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 “201 Interview Questions on Workflow”. Rehan is the found-er of the Web site www.siteofSAP.com. Rehan’s email address is [email protected]. ≈

Figure 14: Creating the Object Set Z_MY_OBJECTS

Figure 15: Output of Inspection

4On ABAP/J2EE

Page 9: Code Inspector

SAPtips

Page

9

SA

Pti

ps J

ourn

alJu

ne/Ju

ly 20

06 V

olum

e IV

Issue

3

SAPtips.com SAPtips © 2006 Klee Associates, Inc.

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: 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