...
There are a number of guidelines we feel improve efficiency, productivity and/or the ability to read, understand and maintain ABAP code. These are:
Guideline | Justification |
---|---|
Internal tables | |
Where possible, use FIELD-SYMBOLS (LOOP AT - ASSIGNING) instead of work areas (LOOP AT - INTO) when looping over, and READ - ASSIGNING instead of READ - INTO when reading the contents of a table. | There are no disadvantages in using a field-symbol instead of a work area. The benefits are:
Note: in very specific instances where you want to loop through a table and NOT change the contents of the table, but you do want to modify some of the table fields temporarily, you may want to consider using LOOP - INTO instead of FIELD-SYMBOLS as the latter option will change the contents of the table. |
Use BINARY SEARCH where possible when searching internal tables in ABAP. Remember: sort your table before the search. | Efficiency - search is vastly more efficient, with exponential improvements the larger the table gets. |
When using FOR ALL ENTRIES to compare against an internal table in a SELECT statement, ensure that you have checked that the internal table is not empty before executing the SELECT. Checking SY-SUBRC = 0 (for a prior SELECT that fills the comparison table) or checking the contents (# of rows) of the internal table should suffice. Furthermore, please ensure that you delete duplicate entries in the 'comparison' table as this might result in an inefficient search. Remember: You may have to code two versions of the SELECT, inside an IF/ELSE construct - one where the internal table used for comparison is empty (without the FOR ALL ENTRIES addition) and another where the internal table does contain records (with the FOR ALL ENTRIES addition). Note: Consider using a Range (TYPE RANGES OF) with an IN clause in your SELECT, instead of using FOR ALL ENTRIES. This reduces the need to check for an empty table, but there are size limitations to consider for the Range object. This may work initially (especially as DEV/QAS only has limited data in most instances) and only later become apparent (potentially only in PRD) as the datasets grow over time. | Efficiency - if the internal table is empty then the entire database table will be searched - the FOR ALL ENTRIES addition will not provide any benefit / filtering at all. From the SAP help website: "If the internal table itab is empty, the entire WHERE condition is ignored. This means that none of the rows in the database table are skipped...." |
When working with nested tables, use of the parallel cursor technique is recommended. See example / explanation at: https://wiki.scn.sap.com/wiki/display/Snippets/ABAP+Code+for+Parallel+Cursor+-+Loop+Processing Remember: sort both the tables in the same (at least primary) sort order before the looping / search commences. | Efficiency - significant reduction in reading times of the 2nd (nested) table. Especially important if the nested table has a large number of rows. |
ABAP syntax / compatability - as a general rule, when developing FAB / OneList Adapters / CAPEX or other packaged solutions, assume that not all clients will have the latest ABAP syntax available. Please do not use the latest ABAP functionality/keywords as it will cause syntax errors during deployment, leading to immediate loss of confidence in the product by the client Basis / IT team(s). When developing directly on the client system (Z) this restriction does not apply. | To avoid syntax errors during deployment of FAB / OneList Adapters / CAPEX or other packaged solutions |
Authorisations | |
Apply authorisations before extracting data where (possibly) large data sets may be extracted for reporting, monitoring apps, search helps, etc. For example, in a monitoring app, let's say that we want to only present instances to the user pertaining to the Company Codes that a user is allowed to see. Suggested steps:
| When working with reports, monitoring apps and any situation where a large data set needs to be extracted, we want to prevent the scenario where we retrieve all data (ignoring authorisations) and then applying the authorisations after the fact. This approach will be inefficient, as data will be extracted that may never be presented to the users, since they may not be authorised for large sections of that data set. The same concept may be applied where we only want to present the relevant (allowed) values to a user in a Search Help. |