June/July 2006 Volume IV Issue 3
SAPtipsJournal
Page 1
SAPtips4On ABAP/J2EE Improving the Quality of Your ABAP Code Using the Code InspectorA Guide for SAP® Developers and Consultants By Rehan Zaidi, Siemens Pakistan 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 optimizing 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 processing 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 Inspector 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 prepare for inspection.
Introduction
SAP has empowered consultants (and users) in all areas by providing tools for checking the quality of their work. The ABAP Workbench is no exception. One such tool available with the ABAP Workbench is the Code Inspector. It allows you to check the syntax and semantic consistency of ABAP programs, and also lets you improve their performance.
The Code Inspector lets you check ABAP repository objects including programs, classes, and function modules.
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 calling the Code Inspector? • What are the errors that the Code Inspector may identify? • What are some of the code improvement hints the Inspector may suggest?
detail, the steps required in checking repository objects such as ABAP programs, classes, and function groups. Finally, I will use an example drawn from an easy-for-all company scenario to illustrate my views. I am going to include tips learned from my personal 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 mentioned are relevant for later releases as well.
The Code Inspector: An Overview
The Code Inspector is a tool (available 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
I will begin with an overview of the Code Inspector and the benefits it provides to developers. I’ll discuss, in Figure 1: Objects That May Be Checked
SAPtips.com
SAPtips © 2006 Klee Associates, Inc.
June/July 2006 Volume IV Issue 3
tion SCI or via a menu path of the respective repository object maintenance 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 specifying the checks that are to be carried out, and
Figure 2: The Code Inspector Output The output (result) of the Inspector is in the form of a hierarchy showing 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.
SAPtipsJournal
Page 2
SAPtips4On ABAP/J2EE
Priority Color Codes
Meaning
Red Yellow Green
Error Warning Information
• 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-comments in your ABAP source code. There are two ways of calling the Code Inspector, namely via transac-
c) finally, create and execute an exception that is used to generate the check results. The Variants, 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 (transaction SE37) using the menu path shown in Figure 4.
Figure 3: Priority Code Colors and Their Meaning
The Code Inspector offers numerous 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.
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 inspection.
Calling the Code Inspector with Transaction SCI
As already mentioned, there are two ways of calling 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 Figure 5. Before creating an inspection, you need to define a suitable Object Set and Check Variant. Enter a suitable 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.
SAPtipsJournal
Page 3
SAPtips4On ABAP/J2EE
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 created the objects in question, or on the basis of the application 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 Responsible field.
Figure 6: Defining an Object Set
SAPtips.com
SAPtips © 2006 Klee Associates, Inc.
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 Classes, 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 programs that begin with the letter 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: 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.
SAPtipsJournal
Page 4
SAPtips4On ABAP/J2EE
Creating and Executing a Check Variant
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.
Finally, you have to create and execute an inspection for your specified objects. On the main screen of transaction SCI, enter an appropriate 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 created Check Variant, and the Object Set, in the fields provided. Then, click the Execute button to start the inspection Figure 8: Creating and Executing the Inspection
SAPtips.com
SAPtips © 2006 Klee Associates, Inc.
June/July 2006 Volume IV Issue 3
• Statements after which the return code (SY-SUBRC) must be checked. The SYSUBRC denotes the success (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 Native SQL. Figure 9: Code Inspector Output
Checks and Functions Included 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 Checks The first category deals with checks related to the ABAP syntax. In addition to the normal ABAP correctness, the Code Inspector also carries out an Extended Program Check, which includes the following:
SAPtipsJournal
Page 5
SAPtips4On ABAP/J2EE
• It checks whether the statements that call external programs (or program components) are used in the correct manner. Some of the statements that fall in this category are
CALL TRANSACTION SUBMIT CALL FUNCTION PERFORM IN (PROGRAM) CALL SCREEN SET PF-STATUS Figure 10: Statements that Call External Program Components
shown in Figure 10. For example, an error is generated if a transaction referred to in a CALL Transaction Statement does not exist. • The Inspection function searches for statements that could create 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 incorrect context. For example, a COMMIT WORK used within a SELECT loop would give undesirable results. b) Security Checks The checks pertinent to this category find statements (or constructs) that may hamper the stability and security of your SAP system. Typical examples of these are:
SAPtips.com
use
c) Performance Checks As the name implies, these checks identify statements (or constructs) that may lead to a decline in the program’s performance. 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 buffers residing on the application server.
The Code Inspector also carries out an Extended Program Check.
SAPtips © 2006 Klee Associates, Inc.
June/July 2006 Volume IV Issue 3
SAPtipsJournal
Page 6
SAPtips4On ABAP/J2EE • Select statements that do not have a suitable WHERE clause. In this case, the system may not be able to use an appropriate index and may carry out a sequential scan on the entire table, thus resulting in extremely long runtimes. d) Search Functions In addition to the checks mentioned, the Code Inspector allows you to search for words (tokens) or entire statements in your Object Set. The search option also allows you to base your search on patterns 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 Inspection is then created and executed 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. you to locate single words (having at For example, consider the statement least a length of three characters) in pattern: your program code. When using this search option, the characters + and * READ TABLE + WITH KEY + = + have special meanings. The symbol + represents a single character within a In this case, the system searches for word, whereas * denotes a sequence all READ statements that find a table of characters. For example, if you row, on the basis of a single key field. 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, you will write ITAB+ in the search followed by an “=”symbol and then field. a comparison value. All READ statements that use two or more field comOn the other hand, the Search parisons are omitted by this search. ABAP Statement Patterns lets you find sets of similar ABAP statements You may combine the rule menwithin your ABAP code. In this case, tioned in the token search with the the + and *may also be used to define statement search function in order to the search criteria. While search- define more powerful search criteria. ing for a statement, the wildcard + In this case, additional information is denotes one word or token, whereas provided for the token in consider-
SAPtips.com
ation. For example, our READ statement 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-specific phrases known as pseudo-comments, in the program code. ExamSAPtips © 2006 Klee Associates, Inc.
June/July 2006 Volume IV Issue 3
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 pseudocomment is appropriate for a given
check, click on the icon on the Variant 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.
Putting It All Together In this section, I will use the concepts presented in this article to demonstrate how the Code Inspector may be used to fulfill a simple requirement. Let us consider the following scenario: You are given the task of checking 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:
Figure 12: Documentation of a Code Inspector Check
SELECT SINGLE * WHERE * An Object Set Z_MY_OBJECTS is created, as shown in Figure 14.
SAPtipsJournal
Page 7
SAPtips4On ABAP/J2EE
Since we need a set of all programs having names starting with Z, the Program field in the Object Selection area is filled with Z*. Finally, an inspection is created and executed using the newly-created Variant and Object Set. The output of the inspection is shown in Figure 15.
Figure 13: Creating the Z_MY_VARIANT Variant
SAPtips.com
SAPtips © 2006 Klee Associates, Inc.
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 Inspector supports. Finally, I discussed a requirement that may be fulfilled via use of this tool. I hope that this article will be a precious resource for you, and will help you in checking the quality of your programs in the least possible time. Rehan Zaidi, Senior SAP Consultant, Siemens Pakistan. Rehan has been involved in both ABAP development and functional configuration for SAP HR implementations at multinational and local companies, and also has experience with SAP Workflow. 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 Managers, as well as a guide for ABAP/ Workflow Consultants titled “201 Interview Questions on Workflow”. Rehan is the founder 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
SAPtipsJournal
Page 8
SAPtips4On ABAP/J2EE
SAPtips.com
SAPtips © 2006 Klee Associates, Inc.
June/July 2006 Volume IV Issue 3
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
Page 9
SAPtipsJournal
SAPtips.com
SAPtips © 2006 Klee Associates, Inc.