The following best ABAP coding practices will help you make your ABAP program more performance and robust:
- Make sure that the code has explicit comments about the functionality of the implemented code logic.
- Unused variables or constants must be removed from the program.
- Limit hard coding elements.
- Declare elements (variables, internal tables, etc…) locally when used only in sub routines and pass globally declared elements to sub routines via parameters.
- Reduce code redundancy (avoid duplication of codes) by modularizing code whenever possible.
- Make sure that variables, work areas or internal tables have been cleared properly when required (especially inside loop).
- Implement appropriate checks in loop to prevent infinite loop.
- Before the FOR ALL ENTRIES statement, check if the table is not empty first, SORT the table and DELETE ADJACENT DUPLICATES from the table using the same set of keys that will be used on the FOR ALL ENTRIES statement. Please note: Empty tables in FOR ALL ENTRIES will retrieved all entries from database tables and can cause performance issue.
- Make sure all primary keys have been selected in FOR ALL ENTRIES select statement else it will retrieve only none duplicates lines of the selected fields.
- Make sure \’FOR ALL ENTRIES\’ is not done on cluster tables.
- No database statement (like SELECT, INSERT, UPDATE, DELETE, etc..) inside loop.
- Avoid multiple select statements on the same database table in the same program.
- The \’SORT INT_TABL BY <FIELDS X Y X>\’ statement must be used instead of just SORT INT_TABL (this will sort all fields in the internal table and this affect the performance). Also avoid sorting inside loop.
- Make sure BINARY SEARCH statement is used in READ statement and make sure table is sorted correctly before using BINARY search.
- Use \’SELECT UP TO 1 ROWS\’ when you don\’t have all primary keys and \’SELECT SINGLE\’ when you have all primary keys.
Happy coding.