ABAP Programming Best Practices
Contents
Introduction
Top 10 Rules
Performance Rules
1. Introduction
This document is intended to cover the basics of good ABAP programming for ABAP developers and ABAP learners. In a normal SAP environment we use ABAP to write a code for doing a look-up in transformation or to code function modules to extract data from source systems. This process would generally involve declarations, selection of data, read the selected data to manipulate it or to perform calculations. I have taken into consideration only these basic steps for this document.
2. Top 10 Rules
Rule 1: Use the Code Inspector (CI), the Extended Syntax Check (if code inspector is not available) or ABAP Test Cockpit (ATC) and fix all errors and warnings – respectively P1 and P2 for ATC.
Rule 2: Use the TRY / ENDTRY instead of CATCH / ENDCATCH statement to trap runtime errors and handle them.
Rule 3: Always handle all exceptions of a function module or method call. If the function module/method actually issues messages, handle the exceptions with the default message statement. Otherwise handle them with your own error messages.
Rule 4: Always check for the deletion flag on key master data tables. LOEVM, LOEKZ, LVORM, etc are some of the Deletion flags.
Rule 5: Check the return code after every statement that updates it. Check with SY-SUBRC only.
Rule 6: Internal tables must be defined with the appropriate table type based on usage (HASHED, SORTED or STANDARD).
Rule 7: When using the FOR ALL ENTRIES option in an SQL WHERE clause, you must ensure that the internal table used in the FOR ALL ENTRIES option is not empty, immediately before the SQL statement.
Rule 8: Alway apply lock to a tables before issuing update commands.
Rule 9: Use Join conditions instead of SELECT....INTO itab with subsequent SELECT.....FOR ALL ENTRIES IN itab, except for buffered tables.
Rule 10: Don't use INSERT, UPDATE, etc. within loops.
3. Performance Rules
Rule 1: Pass by value in Sub-Routine/Method should only be used when passed data need to be duplicated for specific processing.
Rule 2: Avoid CPU intensive operations during LOOP … ENDLOOP.
Rule 3: ALWAYS process internal tables with field-symbols either using LOOP AT ... ASSIGNING <...> or using READ ... ASSIGNING.
Rule 4: RFC Calls: Optimize network traffic by architecting the solution with minimized network traffic.
Note: I will keep updating this document based on further corrections/suggestions.