Programming

How to Clean Your ABAP Code with One Keystroke Using ABAP Cleaner

As an ABAP developer, could you imagine a world without Pretty Printer or ABAP Formatter?

 

Where you are forced to switch caps lock on and off for every ABAP keyword, to indent all your code manually, and to waste precious time in every code review telling people what is supposed to be upper or lower case? Presumably, you do not want to imagine that!

 

In a very similar way, I think there is a good chance that after today, you won’t want to imagine an ABAP developer’s world without the ABAP cleaner tool anymore. This is because casing and indentation – as done by the Pretty Printer – are indeed just two concerns of well-formatted, readable, maintainable, and testable (in other words: clean) code. There are many more, and as it turns out, many of those can be automated!

 

ABAP Cleaner logo

 

ABAP Cleaner: Automate Whatever Can be Automated!

As of now, ABAP cleaner offers 65+ cleanup rules, all of which can be applied to your ABAP code with a single keystroke.

 

Cleanup example

 

Using ABAP cleaner is a lot like using Pretty Printer, but with a much wider scope of cleanup rules, and with the possibility to configure exactly what the cleaned code should look like. And since ABAP cleaner also covers the scope of Pretty Printer, you might indeed end up in a world without Pretty Printer after today!

 

How to Install ABAP Cleaner

ABAP cleaner is an open-source project that was published on GitHub in April 2023 and continues to evolve.

 

To install the ABAP cleaner plug-in for ABAP Development Tools (ADT), just start ADT, select menu 'Help/Install New Software...', copy the link https://sap.github.io/abap-cleaner/updatesite to the 'Work with' field, press Enter, and follow the installation steps, confirming to install ABAP cleaner (content is currently unsigned). For details and troubleshooting, please refer to Requirements and Installation.

 

ADT installation

 

Alternatively, if you cannot use ADT, there is a stand-alone version of ABAP cleaner for Windows, macOS, and Linux which can be downloaded here. This version, however, requires you to use the clipboard to get ABAP code in and out of ABAP cleaner. It is therefore recommended to use ADT and the ABAP cleaner plug-in.

 

How to Use the ABAP Cleaner Plug-in for ABAP Development Tools

After installing ABAP cleaner and opening an ABAP code editor in ADT, you will find three additional menu items in the Source Code menu:

 

ADT integration

Automated Cleanup

Using the automated cleanup is as simple as using Pretty Printer: Select any amount of code, from a single statement up to the whole code, in the current code editor, and press Ctrl+4 or select menu “Source Code/Clean Up With Automated ABAP Cleaner”:

 

ADT menu automated

 

This will execute all active cleanup rules on your code at once, either with ABAP cleaner defaults, or according to your configuration, as described below.

 

Hint 1: Your selection is always expanded to whole ABAP statements, so selecting just one line or even just one character of a statement is sufficient to make ABAP cleaner include the whole statement in the cleanup.

 

Hint 2: Instead of selecting the code to clean, you can also not select any code. In this case, Ctrl+4 will automatically clean the current method, subroutine, etc. in which the cursor is placed, or the current definition section, e.g. PUBLIC SECTION.

 

From our experience, you will soon use automated cleanup as regularly as you used Pretty Printer, and indeed, the shortcut Ctrl+4 was selected to be easily combined with Ctrl+F2 (for syntax check) and Ctrl+F3 (for activation). Ctrl+4 should not be confused with Ctrl+F4, as the latter would actually close the editor tab. When you start using ABAP cleaner, you will probably want to move a bit slow at first and see what happens!

Interactive Cleanup

To use interactive cleanup, select any code in the editor (or no code to clean the current method etc.) and press Ctrl+Shift+4 or select menu “Source Code/Clean Up With Interactive ABAP Cleaner…”:

 

ADT menu interactive

 

Diff View

This will open the main window of ABAP cleaner, which shows a diff view of the incoming code on the left-hand side, and the cleanup result on the right-hand side:

 

Main window

 

Yellow and orange background highlights changes, red and green background shows deleted and added lines. You can use the left and right arrow keys (and many more shortcuts) to navigate to the previous or next difference.

Rules Used in Current Selection

When you select one or several statements in the diff view, the list Rules Used in Current Selection will show you which cleanup rules were used to make the selected changes:

 

Main window rules used

 

You can deactivate the ticks to locally revert a change. Note that this does NOT deactivate the cleanup rule in general, but only the changes currently selected in the diff view.

 

Instead of locally deactivating a rule from the list, you may Ctrl+click on the rule to generally adjust its configuration options to your needs, or even completely deactivate the cleanup rule in the current profile (see “How to Configure ABAP Cleaner” below).

Display Options

If your display is all yellow due to many changes, you may want to deactivate the highlighting of some less significant types of changes – typically, indent, inner space, and upper/lower case changes. The diff view will then only highlight and navigate to ‘textual’ changes:

 

Main window display settings

 

More display settings can be found in the “View” menu, where you can zoom in and out, highlight declaration statements and variables in write positions.

Cleanup Settings

In the top right corner, you will find three important settings for your cleanup:

 

Main window cleanup settings

 

The first of these settings allows you to select a cleanup profile. Each profile stores the activation (or deactivation) of all available cleanup rules and detailed configuration for those rules. We will see below how to create and configure your own profiles. Note that the profile that was last selected in the interactive cleanup window will then be used for automated cleanup with Ctrl+4.

 

Setting the Default cleanup range allows you to determine which amount of code shall be cleaned if no code is selected while you press Ctrl+Shift+4: the current statement, the current method, the current class, or the entire code document.

 

Finally, you can Restrict rules to the syntax of a certain ABAP release. This will prevent newer syntax such as calculation assignments (+=, *= etc.) from being introduced to your code if the specified release does not yet support them. In the ADT plug-in, this restriction is automatically selected for the ABAP release of your code; however, you may want to further restrict the release if the same code shall later be downported to an earlier ABAP release.

Apply Changes

If you agree to the changes suggested by ABAP cleaner, you can close the window by pressing Apply and Close. This will transfer the changes to your ADT editor, where you can then activate them. Alternatively, press Cancel to discard all the suggested changes.

Read-Only Preview Without Locking or Changing Code

A third menu item in the Source Code menu offers to Show a Read-Only Preview With ABAP Cleaner:

 

ADT menu read-only preview

 

This opens the ABAP cleaner UI with the diff view exactly like the interactive cleanup, however, in case of the read-only preview, the ABAP code is not locked and will not be changed when returning to the ADT editor. You can use this menu if a code object is already locked by someone else, or if you do not have a transport or even no development authorization in a system, but nevertheless want to see the possible cleanup result or change ABAP cleaner settings and configuration.

 

This menu might even be helpful to simply read legacy code that cannot currently be changed: Instead of looking at the old (and potentially hard-to-read) code, you can look at the cleaned version in the right-hand display of the diff view (possibly deactivating the Display: Highlight options).

Further Information

How to Configure ABAP Cleaner

ABAP cleaner is installed with two cleanup profiles:

  • In the default profile, almost all cleanup rules are activated with what we believe are good defaults for everyone to use.
  • In the essential profile, only those cleanup rules are activated which are explicitly demanded by the Clean ABAP style guide. Currently, this is true for approximately 40% of the rules. This shows that a lot of automation is possible even beyond the specific demands of the style guide – and we think that is beneficial, simply because consistent formatting helps readability. Indeed, be consistent is a style guide rule!

In any case, you may want to explore in more detail what cleanup rules are available in ABAP cleaner, and how they can be configured to get exactly the code formatting that you and your team prefer. This is done by pressing the Configure… button at the top right corner of the interactive ABAP cleaner window, which will then show the following window for the configuration of profiles and rules:

 

Configuration

Exploring the Cleanup Rules

Currently, ABAP cleaner offers 69 cleanup rules, which are grouped into rules on empty lines, spaces, declarations, syntax, commands, Pretty Printer, and alignment:

 

Available cleanup rules

 

If you click on one of the cleanup rules in the list Rules in Current Profile, the right-hand side of the window will show detailed information on this rule. This is the best way to explore what ABAP cleaner offers. You will find:

Configuration references

  • Options with which you can configure the cleanup rule to get exactly the formatting and cleanup result that you and your team want:

Configuration options

  • Examples that demonstrate in a diff view what the rule changes, and how the options influence the result. You can paste your own code into this diff view to test the current cleanup rule and the effect of its settings on your code.

Configuration examples

 

Checking or unchecking a rule in the list will activate or deactivate it in the current profile.

Activating and Configuring New Cleanup Rules from Updates

ABAP cleaner is not a concluded project but continues to evolve with new cleanup rules and new options for existing rules. ADT will automatically inform you whenever a new update for ABAP cleaner is available and can be installed. Updates with new features will then enhance the list of available cleanup rules and options in each profile, including the existing profiles.

 

You can decide for each profile whether you want to automatically activate new features after updates:

 

Configuration profile settings

 

If you prefer a more cautious approach, you can deactivate this option. Newly added rules will then be inactive at first, and new configuration options will be set to a neutral setting that creates the same result as before the option was added. With this approach, you could occasionally open the ABAP cleaner configuration to check which new cleanup rules were added in the meantime.

 

To help you with that, ABAP cleaner offers the Highlight: Features added after <version> (<date>) setting. With that, the list of rules highlights recently added rules (green) or rules that have new configuration options (yellow), allowing you to activate and configure these new features as needed.

Changing and Creating Profiles

Each profile contains all active or inactive rules and their detailed configuration options. In the section Profiles, you can select one of the preconfigured profiles to view or change it. You can also create your own profiles, or copy, delete, rename, import, and export existing ones:

 

Configuration profiles

 

Technically, a profile is a small, JSON-like text file with the file extension .cfj which is stored in the application data on your local machine.

Synchronizing Team Profiles

If you are part of a team that works on the same ABAP code, you will probably want to align on ABAP cleaner settings to use. To support that, ABAP cleaner offers a mechanism to synchronize profiles that can then be used consistently across your team.

 

For that, click the Folders button in the Profiles section of the configuration window. This will allow you to specify up to three additional folders for team profiles:

 

Configuration profile folders

 

By specifying folders that are synchronized across your organization e.g. via SharePoint or OneDrive, profiles can easily be shared with whoever you want to align code formatting with. Team profiles are added to the list of profiles with the supplied short name (e.g. “team A: default”) and are read-only for everyone, so in ABAP cleaner, they can be used and displayed, but not changed.

 

Your team architect or lead developer can then maintain a source profile locally in their own profiles and use the Export… button to copy this profile to the synchronized folder whenever they changed configuration that should be distributed to the team.

Further Information

  • You can find a more detailed description of the profiles window and all its features on the ABAP cleaner repository.
  • The list of available cleanup rules shows the descriptions, options, references, and examples for each rule on the ABAP cleaner repository and links to the related code (rule implementation and tests). However, it is much more fun to explore the rules in the configuration window and interactively try the effect of their configuration options!
  • The release notes give details on new cleanup rules, new options, new UI features and bugfixes added in each release. ABAP cleaner uses semantic versioning, which means that an increase in the third version number is used for bug fixes, while the second number is increased for any added (but backward compatible) functionality.

Benefits of Using ABAP Cleaner

I hope that ABAP cleaner will soon prove beneficial to you by:

  • saving you a lot of time and concentration that you had to spend on tedious code formatting, which is now automated.
  • leaving you more time for complex refactoring and code improvements that cannot be automated.
  • helping you and your team to create ABAP code in a consistent, well-formatted style that is easier to read, understand, and maintain for everyone in the team.
  • making onboarding of new developers easier, because any clean code rule that can be automated does not have to be specified in guidelines and learned.
  • relieving your code reviews of fruitless discussions on style that can now be decided once and for all by configuring a common cleanup profile.
  • reducing the number of ATC/code pal findings, because with ABAP cleaner, some of them – including some very frequent ones – are fixed before they even come up.
  • again, leaving you more time for the more complex ATC/code pal findings. From experience, fewer findings are more likely to be taken seriously.
  • reducing the number of errors in the code, because in well-formatted code, errors can be spotted more easily, or can even be avoided in the first place.
  • supporting you in your efforts to improve legacy code, e.g. by automatically replacing some obsolete statements with newer syntax.
  • making the perfectionists in your team happy with clean and tidy code, and making the non-perfectionists happy, too, because they get a lot of this by simply pressing Ctrl+4!

ABAP Cleaner in the Context of Other Clean ABAP Projects

To be sure, ABAP cleaner is not the only open-source project concerned with clean ABAP. You can roughly categorize these projects into three sets, or circles, shown here with the respective SAP-initiated open-source projects (there are, of course, non-SAP-initiated projects as well!)

 

SAP-initiated projects for clean ABAP

 

None of these projects is completed, which means that each of the circles continues to expand over time, trying to describe even better what clean ABAP should look like, to provide more checks for reporting violations of clean code rules, and to automate further. Nevertheless, it is obvious that some aspects of clean ABAP can be described, but not checked, such as “Use descriptive variable names.” Similarly, some issues can be checked, while fixing them cannot be automated – e.g. when a maximum recommended number of attributes or methods in a class is exceeded.

 

ABAP cleaner therefore belongs to the smallest circle, the automate circle, but any progress in this circle will leave you more time to improve your code in the more complex matters checked by Code pal or described in the style guide.

Further Information

How to Contribute

You are very welcome to contribute to the development and improvement of ABAP cleaner by reporting any suspected bugs or issues, telling us about your ideas on what else could be automated, or contributing to the discussions of how exactly code should look after cleanup and what configuration you would need for that.

 

All communication is done by opening or commenting on issues on the GitHub repository. You will quickly see that the community certainly hasn’t run out of ideas!

 

For contributing, please refer to the sections Engaging in our Project and Limitations and Known Issues on the ABAP cleaner repository.

 

For the story behind ABAP cleaner and its journey to open source, you can watch the webinar hosted by the SAP Open Source Program Office (slides).

Recommendation

Clean ABAP: A Style Guide for Developers
Clean ABAP: A Style Guide for Developers

ABAP developers, are you looking to clean up your code? Then pick up this official companion to the Clean ABAP GitHub repository! This book is brimming with best practices, straight from the experts, to help you write effective ABAP code. Start by learning when to apply each clean ABAP practice. Then, dive into detailed code examples and explanations for using classes, methods, names, variables, internal tables, and more. From writing code to troubleshooting and testing, this is your complete style guide!

Learn More
Jörg-Michael Grassau
by Jörg-Michael Grassau

Jörg-Michael Grassau is a senior developer for SAP S/4HANA Cloud at SAP. After joining SAP in 2019, he started to develop ABAP cleaner in December 2020 based on his own needs in ABAP development. The project was then released as InnerSource at SAP in December 2021 and published as Open Source in April 2023.

Comments