Over the years, SAP software professionals have come up with best practices that are universally accepted and have not only proven to produce superior results but also define standard rules for ABAP (Advanced Business Application Programming) coding. It makes it easier for developers to adapt to each other’s code without much analysis.
SAP Best Practices is a defined way for success and minimizing chances of failure. SAP ABAP is the programming language for SAP and it has been around for decades now, and the best practices keep evolving with time and industry. We compiled this list of common best practices likely to be used by almost all SAP implementations and divided them into relevant topics.
Best Practices for ABAP Development
Learn ABAP Programming Best Practices from an expert developer in this training course.
Below are our top 10 tips for ABAP Best Practices:
- ABAP code should have explicit comments for the functionality implemented. It is best to have a detailed explanation for the ABAP logic.
- Each ABAP project implementation should have a class “ZCL_CONSTANTS” with attributes containing all constants required for the project. This avoids hard-coding values within ABAP programs.
- Utilize object-oriented modularization techniques to avoid redundancy of code. For example, all initialization logic can be pushed to the “CONSTRUCTOR” method and reusable logic to separate methods.
- Variable, Tables and Structures should be cleared within the program, and unused Variables, Tables, Structures should be deleted. These can be found easily via the SCI program.
- Before using “FOR ALL ENTRIES” it is mandatory to SORT and DELETE ADJACENT DUPLICATES of the said table. If FOR ALL ENTRIES is used on an empty table, the query will return all entries from the database and result in performance issues.
- A SELECT query should not be written inside a loop. Similarly, SORT should not be done inside a loop. Always select and sort the data before using it in the loop.
- “SELECT SINGLE” should only be used when you have all primary keys, otherwise use “SELECT UP TO 1 ROWS” instead. Interchanging this could create performance issues in the long run.
- If “BINARY SEARCH” is used, always check if the table is sorted. Without SORT, the BINARY SEARCH can produce invalid results.
- If there is a need to have a loop inside another loop, always use the “Parallel Cursor Method” to improve performance.
- Any change to an existing program should include change pointers as specified by the client, and a version should be generated for each change to ensure traceability.
Best Practices for ABAP on HANA Code Push Down
“Code Push Down” is an effective way to enhance the performance of ABAP code. However, the usage should be properly analyzed and planned based on the type of requirement.
SAP Applications are created in the ABAP Workbench or ABAP runtime environment and consist of programs, function modules, ABAP objects, data models and much more.
Learn more about SAP ABAP on HANA 7.4 in this training course.
A developer has the below options to use when accessing the SAP HANA database.
New Open SQL
- Should be used while querying database tables using simple joins along with new open SQL properties like COALESCE, SUBSTRING, SUM, AVG, MIN, MAX.
- Used with standard table buffering to improve performance.
- With new Open SQL, it is best to use subqueries with join (LEFT, INNER, OUTER) rather than multiple separate SQL queries.
- New Open SQL keywords like Inline declaration, REDUCE, LOOP with , LINES, VALUE #, etc. should be used to reduce the number of code lines.
Learn more: Reading Data in ABAP with Open SQL.
Core Data Services (CDS)
- Should be used when querying and joining large database tables. Also, this CDS query is reusable: Once the CDS is created, the same can be used in multiple other programs.
- Used when the security of data and access control is of significant concern. Annotations in CDS control the safety of the data fetched based on the roles defined.
- OData can be directly published using CDS annotation, “@OData.publish: true” and can be used to build an interactive UI5 application by adding explicit FIORI Smart Template annotation.
- It is always best to use “Entity Name” while consuming data from the CDS into an ABAP program.
Learn Access Control in ABAP Core Data Services.
ABAP Managed Database Procedure (AMDP)
- AMDP should always be used with BADI Framework as there is a way to provide a fallback implementation for future releases. And in the event of production issues, the AMDP can be easily deactivated.
- AMDP should not be used for simple queries, SELECTs, JOINs, or comparatively smaller logic and to be used when an extensive business logic can be written using SQL script, which executes fully on the database layer.
- AMDP output can be enhanced with CDS by using CDS Table Functions for complex cases. This would leverage the power of CDS and AMDP both.
- An AMDP feature that can be effectively utilized is that a developer can write select statements on local internal tables/variables as well. For instance, query 1 has results in LT_RESULT1, while subsequent query 2 can be based on LT_RESULT1.
Best Practices for ABAP Documentation
During the project implementation phase, documentation is usually the most ignored step. However, comprehensive documentation helps the team build a guide or repository for future reference and helps ensure successful handovers between groups. Below is the list of documents that should be created at each step of implementation,
- Functional Specification (FS) – Built by Functional Consultants to list business requirements after conducting workshops with the client team and understanding the business needs. This should have well-defined test cases to limit the scope of work and should use visual flowcharts of the business process. It is ideally approved by the project manager and client team.
- Coding Standard and Naming Conventions document – Built by Technical Leaders/Managers. This document is used by ABAP developers to follow a specific set of naming conventions for all programs written for the client and helps with a consistent approach for coding. This document must be defined before any development is started.
- Technical Specification (TS) – Built by the developer responsible for the object. This document is created before the development is initiated so that a correct approach of implementation is defined. Usually, reviewed by Technical Team Lead and development can begin only after signing off on this document.
The documents mentioned above are updated if there is any divergence. For example, if the requirement changes, the FS will have to be modified with the new version, and if the approach for development changes while performing some ABAP task, the TS will have to be modified. The standard practice is to strike out and not delete any of the existing content to ensure the traceability of changes.
All these details make for a useful guide for ABAP developers to know the best practices and how to use them. However, individual SAP systems implementations and development projects typically come up with their own, detailed lists based on the requirements and the SAP landscape they are working with.