SAP ABAP Best Practices | Michael Management
11/2/2020 by Ronak Hingorani Posted in SAP

SAP ABAP Best PracticesOver 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:

  1. ABAP code should have explicit comments for the functionality implemented. It is best to have a detailed explanation for the ABAP logic.
  2. 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. 
  3. 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. 
  4. 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.
  5. 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. 
  6. 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.
  7. “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.
  8. If “BINARY SEARCH” is used, always check if the table is sorted. Without SORT, the BINARY SEARCH can produce invalid results. 
  9. If there is a need to have a loop inside another loop, always use the “Parallel Cursor Method” to improve performance.
  10. 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

  1. Should be used while querying database tables using simple joins along with new open SQL properties like COALESCE, SUBSTRING, SUM, AVG, MIN, MAX.
  2. Used with standard table buffering to improve performance.
  3. With new Open SQL, it is best to use subqueries with join (LEFT, INNER, OUTER) rather than multiple separate SQL queries.
  4. 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) 

  1. 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.
  2. 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.
  3. 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.
  4. 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)  

  1. 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.
  2. 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.
  3. 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.
  4. 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,

 

  1. 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. 
     
  2. 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. 
     
  3. 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.

 


Author Photo

Ronak is an ABAP/UI5/FIORI Consultant with more than 8 years of experience, with a demonstrated history of working in the SAP consulting industry. He has worked across various SAP modules like MM, SD, FICO, P2P and has helped client by developing a scalable technical ABAP solution. Over the past 8 years he has built expertise in server-side ABAP technology and advanced ABAP skills like CDS, AMDP, OData and in client-side UX technology like UI5 Framework (HTML/CSS/ JavaScript) and FIORI. He also has experience in SAP Upgrade from ECC to HANA (1709/1909).
More Blogs by Ronak Hingorani:
full-stack webide
SAP WebIDE Full-Stack Edition Setup and Configuration

Introduction SAP WebIDE is a powerful, extensible, web-based integrated development tool that simplifies end-to-end full-stack application development. SAP WebIDE supports the entire application lifecycle, ...

sap webide
SAP WebIDE Personal Edition Setup and Configuration

Introduction SAP WebIDE is a web-based development tool to build SAPUI5 and FIORI applications. WebIDE is an open source code editor development environment for a single user. There are two kinds of ...

fiori elements
Application Development with FIORI Elements

Introduction SAP Fiori Elements, formerly known as smart templates, has been around since 2015 (when SAP S/4HANA come out) and provides a framework for the most common application patterns.  If ...