You are on page 1of 4

On my last post i have written about Infosets and User Group Creation and Parallel Cursor Logic in SAP

ABAP, this week i will be listing out quick tips and tricks for sap ABAP code optimization,basically these are the best code practices that must be followed in order to write fine optimized code that will enhance the performance

Modularization: The code should be broken into logical segments for modularization into subroutines/functions/packages whenever possible. It will also enhance the readability of the code. Try to make the user interface such that the program gradually unfolds more information to the user, rather than giving a huge list of information all at once to the user. Also make habit to use USING, CHANGING, TABLES while writing a subroutine. Do not change the global values or tables inside a subroutine. SY-SUBRC Check: Always use sy-subrc to check when dealing with tables (for example: READ TABLE, SELECT, INSERT). Avoid CHECK / EXIT Commands: These commands should be avoided, especially within USER-EXITs. SELECT SINGLE Command: Use the SELECT SINGLE command whenever possible. If you are interested in exactly one row of a database table or view, use the SELECT SINGLE statement instead of a SELECT * statement. SELECT SINGLE requires one communication with the database system whereas SELECT * requires two. SELECT INTO preferred to SELECT APPEND ENDSELECT When an internal table needs to be created directly from one database table, the SELECT INTO is used to fill the internal table. It is faster to use the INTO TABLE version of a SELECT statement than to use APPEND statements. Necessary Column Selection: Use SELECT statements to fetch only the necessary columns from a table. SELECT * is only used when more than one-third of the columns are being used. Avoid SELECT *, especially in tables that have a lot of fields. Use SELECT A B C INTO instead, so that fields are only read if they are used. This can make a very big difference. Use BEGDA, ENDDA and Language for selection criteria wherever applicable. The ordering of the WHERE statements matches the arrangement and the types with the keys in the table records. When using the AND or OR operator the most likely elimination criteria is specified first. (Expressions are evaluated left to right and the evaluation ends when the final result has been established)

FOR ALL ENTRIES: When using this command, make sure that the table is not empty first. Ensure the selection of unique records by specifying the key fields in WHERE clause or in the selection list. We should always check the internal table is not initial before using for all entries in. Check the internal table is not empty before using for all entries. CHECK that the internal table used in FOR ALL ENTRIES is NOT empty as this will retrieve all entries from the table Delete adjacent duplicate entries from internal table before selection from database table using FOR ALL ENTRIES. DB Statement: Always avoid DB statement in the loop. None of the DB statement (SELECT, INSERT, UPDATE, )should be in the scope of a loop. ORDER BY: Never use ORDER BY clause with SELECT. The Internal Table can be sorted after the values are selected. Lock & Unlock: Always Use ENQUEUE and DEQUEUE while updating tables/Infotypes. Also perform the DB updation in bulk instead of within LOOPs. When using HCM Use CALL FUNCTION BAPI_EMPLOYEE_ENQUEUE to lock a pernr and CALL FUNCTION BAPI_EMPLOYEE_DEQUEUE to unlock a pernr. COMMIT and ROLL BACK: Use COMMIT WORK after DB updates. In case of errors, use ROLL BACK. Use CALL FUNCTION HR_PSBUFFER_INITIALIZE when using HCM. READ statement: When using READ statement first perform BINARY search, the internal table must be first sorted by the key. Always try to read internal table by passing key and read by Binary search. And before doing this always sort your internal table (This is the primary condition for binary search). TRANSPORTING clause: To MODIFY fewer fields in an internal table record, TRANSPORTING clause should be used. CLEAR/REFRESH: is used on variables/tables before using them and not only at the beginning of the program. Work areas & Global Variables are freed when no longer needed (e.g. using the FREE / REFRESH command), especially when the tables are large or the program is a batch program. Use Local Variables: Use Local variables as much as possible. Also clear the variables if its value is not required later on in the program. Naming of the Constants: Name the constants with a functional sense. Avoid Hard-coding: Hard-coding should be avoided at any cost. Instead use text elements or message class.

Calculation of Currency: Currency calculations should be performed between currency fields only. Declarations of variables: Use TYPE instead LIKE. Use Field Symbols: Use Field-symbols in place of work areas for looping through large internal tables (more than 1000 entries). When you read table entries using READ or in a LOOP, you can assign them to a field symbol using the addition ASSIGNING . The field symbol points directly to the assigned line in memory. Unlike work areas, in which the contents of the line are only available indirectly using the INTO addition, field symbols allow you to read and change table entries directly. MOVE CORRESPONDING: Its only used for small tables or tables where most, but not all, fields need to be moved. When all fields need to be moved and the attributes for every field and position are identical, the table is MOVEd as a group. MOVE-CORRESPONDING it_header TO it_final. Then this statement will match all the fields from it_header to it_final till it finds the same column and then transfer the values from one to other table. So, this is considered to be time taking and a lot of resources can be used in this process. CLEAR Statement: CLEAR field is used to initialize rather than explicit move. Table headers and work areas are CLEARed at the end of LOOP process. After the APPEND statement inside a loop, the work area that has been appended is cleared Internal tables. Avoid dead-code: Remove unnecessary code and redundant processing. Documentation: Spend time to do proper documentation of the developed or changed objects. Avoid nested loops: Use parallel cursor logic as explained by me on this post. Code Inspector & EPC checks: Always perform code inspector & EPC check (SLIN). Handle Exceptions: All the exceptions such as divide-by-zero and function module exceptions should be handled. Change History Header: Maintain the program history & have all information regarding the TR, Developer name, Date, Technical Design ID, Functional Design ID, etc. A chronological history of modifications should be maintained. Program Documentation: Program documentation should be maintained giving the overview of the program & its use. Also it should have information about the inputs & outputs of the program. It provides functional leads and end users with necessary information to determine report functionality, file inputs and output, etc. Code Comments: Always supplement the code with sufficient internal code comments to ease program maintenance and reliability of the code. In case of subroutines provide the information about the actual &

formal parameters. Make it a habit to write sufficient comment even for single variables.

You might also like