ABAP Tools for Clean ABAP
This blog provides you an overview of ABAP tools which have been developed in the context of Clean ABAP projects led by SAP in the open-source community and gives you guidance for their usage scenarios.
Let’s start with a picture which illustrates the interplay of the Clean ABAP initiative, style guide and tools.
The large outer circle Clean ABAP is all you can do to make your ABAP code clean.
The second circle is about how to describe Clean ABAP, and that is the well-known Clean ABAP style guide, which provides a common set of the rules which can be applied to improve maintainability, readability, and testability of your ABAP code.
The check circle means that for a subset of Clean ABAP rules, there are tools available (code pal for ABAP) which you can use to check your code for Clean ABAP adherence. The check circle is smaller than the describe circle, because it is generally possible to define rules which cannot be checked easily (for example, using semantically descriptive names as variable names).
Finally, the automate circle represents the automation tools which can be used to apply Clean ABAP rules to your code in an automated way like code pal quick fixes and ABAP cleaner. The automate circle is smaller than the check circle, because it is technically not feasible to automate code cleanup for all the issues reported by the Clean ABAP checks (for example, reducing the nesting depth of code must be reworked manually).
Now after a basic understanding is gained, let’s examine all circles in a detailed way.
Why Clean ABAP?
|Nowadays, almost everybody in the software development area knows the book by Robert C. Martin “Clean Code: A Handbook of Agile Software Craftmanship” about the general clean code rules which should be considered to write readable, testable and maintainable code. These general clean code rules have guided SAP to apply the clean code principle to ABAP.|
As every ABAP developer knows, ABAP is not only about writing source code, but also using Data Dictionary, CDS modelling etc., which go far beyond just ABAP language constructs. The Clean ABAP initiative revolves very much around optimization of code for readers, since in the ABAP area, people spend more time reading the code than writing it, for example to understand the business application or during maintenance tasks. Beyond this, in many cases you need not only to read your own code, but also SAP code, partner code or any other kind of custom code. If a uniform representation for these different kinds of code was possible, it would save you a lot of reading and understanding effort. Another aspect which should be considered is that ABAP has evolved over a long time with backward compatibility in mind. There is a lot of legacy code in the traditional ERP landscapes, therefore it would not be realistic to apply Clean ABAP principles to the whole legacy code, but surely, new code, developed with modern ABAP for example in SAP S/4HANA landscapes, can be handled with Clean ABAP.
The introduction to the Clean ABAP is provided in the blog post Why Clean ABAP – A foundation for improvements.
Describe: Clean ABAP Style Guide
The Clean ABAP Style Guide is an open-source project on GitHub. The project is driven by SAP and fully relies on contributions from the open-source community. The project is an active joint work of SAP and contributors with a lot of discussions and reported issues. The Clean ABAP style guide is the central place for looking up all agreed rules which can be applied to make your ABAP code clean. There is also a sophisticated process of aligning and reviewing these rules before they become a part of the Clean ABAP style guide.
As an ABAP developer, you are very welcome to contribute to the Clean ABAP Style Guide, see Contributing to SAP Style Guides and governance information Clean Code Community – Contribution, Discussion, Review and Decision Process on GitHub for more details.
Check: code pal for Clean ABAP
The code pal tool contains a set of checks which can be used in Code Inspector and ABAP test cockpit (ATC) to control the usage of Clean ABAP. It is also integrated via ATC into ABAP development tools for Eclipse. The code pal is an open-source project, provided on GitHub and led by the SAP ATC development team.
There is also a new cloud-enabled version of the code pal checks which can be executed in SAP BTP, ABAP environment and will be available on premise in the upcoming SAP S/4HANA 2023 release. The checks are remote-enabled and therefore can be executed in the remote ATC scenarios. For automated adaptation of code for many Clean ABAP rules, quick fixes are offered in ABAP development tools for Eclipse. Details are provided in the blog post Clean code checks for ABAP – Cloud Edition and on the GitHub under Code Pal for ABAP – Cloud Edition.
As an ABAP developer, you are very welcome to contribute to this project.
Automate: ABAP cleaner and code pal quick fixes
The objective of the ABAP cleaner tool is the same as of code pal for ABAP: helping ABAP developers to follow the rules of Clean ABAP. While code pal quick fixes are most suitable to clean existing code according to Clean ABAP rules, ABAP cleaner can be used immediately during code writing (a.k.a Pretty Printer approach) to clean the code in an automated way.
With ABAP cleaner, you can apply 60+ cleanup rules to your code and clean any amount of code from a single statement to an entire source code document using just one keystroke (Ctrl + 4). You can also configure cleanup rules as needed by your team and restrict code changes to the syntax of a specific ABAP release (for example if your code must be downported to older releases).
You can use ABAP cleaner as a plug-in (recommended) for ABAP development tools in Eclipse (update site: https://sap.github.io/abap-cleaner/updatesite), where you can execute it automatically with a single keystroke (Ctrl +4) or interactively by opening the ABAP cleaner UI, which allows you to preview and adjust changes before applying them (under Source Code menu in ABAP development tools for Eclipse):
It is also possible to use ABAP cleaner standalone with SAP GUI. To start with ABAP cleaner, we recommend selecting the code of a larger ABAP method or class and opening the interactive ABAP cleaner UI. You will then see in a diff view what changes ABAP cleaner would do on your code, and which cleanup rules produce these changes:
Click “Configure…” to see a list of all 60 available cleanup rules. These include
- standardizing empty lines and spaces, such as closing brackets at line end, or removing needless spaces.
- reworking declaration sections, e. g. for unchaining, rearranging local declarations into “order of appearance”, introducing FINAL to immutable variables, or adding missing parameters ABAP Doc.
- syntax improvements on comment signs (* versus “), obsolete pseudo-comments, pragma position, IS NOT versus NOT IS, <= versus LE, optional EXPORTING etc.; one rule even corrects frequent typos like “wether”, “occurr” or “sucessfull” in comments.
- command changes that replace CHECK with IF, CALL METHOD with functional calls, CREATE OBJECT with NEW, obsolete ADD … TO, MOVE … TO, TRANSLATE etc.
- Pretty Printer functionality – with ABAP cleaner you actually don’t need Shift+F1 anymore.
- alignment for ABAP Doc, METHODS declarations, variable and types declarations, assignments, parameters, components, logical and conditional expressions etc.
You can create multiple configuration profiles, in which any cleanup rule you don’t want can be deactivated with one click. Many rules have additional options for fine-tuning, allowing you to get exactly the formatting that you and your team want. Default settings are of course in alignment with the Clean ABAP style guide.
ABAP cleaner is also an SAP-led open-source project that depends on your contributions as ABAP developers. The open-source repository ABAP cleaner on GitHub contains installation instructions, documentation, release notes, a list of available cleanup rules, allows to create issues and so on.
For details about ABAP cleaner, the Devtoberfest 2023 session on YouTube How to Clean your ABAP code in Seconds with ABAP Cleaner and the SAP Press blog post How to Clean Your ABAP Code with One Keystroke Using ABAP Cleaner by the author of the ABAP cleaner tool, Jörg-Michael Grassau, are highly recommended.
If using code pal for ABAP, Cloud Edition, you can also adapt your code for Clean ABAP using single quick fixes (Ctrl + 1) in ABAP development tools for Eclipse:
We recommend to automate your code adaptation to Clean ABAP by using the mass-enabled quick fixes:
Just try out ABAP tools for Clean ABAP!