Technical Articles
ABAP Cleaner: Streamline Your Code for Enhanced Quality and Productivity
This blog is to guide all Abapers on how to achieve clean code using ABAP Cleaner and not only to understand the advantages of clean ABAP and also how to use ABAP Cleaner.
ABAP Cleaner
The ABAP Cleaner tool is used to clean up ABAP code by applying various clean-up rules.
Here are some reasons why you might want to use the ABAP Cleaner tool:
- Automation: The ABAP Cleaner tool automates the process of cleaning up ABAP code, saving you time and effort.
- Uniformity: By using the ABAP Cleaner tool, you can ensure that your code follows a consistent style and structure. This improves code readability and makes it easier for other developers to understand and maintain the code.
- Error reduction: Following clean ABAP principles, which the ABAP Cleaner tool helps enforce, can reduce the number of errors and misunderstandings in your code. Clear and well-structured code is less prone to bugs and easier to debug.
- Modernization: The ABAP Cleaner tool encourages the use of modern language features and coding techniques. This is important if your organization plans to migrate to newer technologies like S/4HANA or the Cloud.
- Collective code ownership: Adopting clean ABAP practices fosters a culture of collective code ownership and feedback. It encourages collaboration and allows multiple developers to work on the same codebase more effectively.
- Maintainability: Clean ABAP code is easier to maintain and modify. It follows best code organization and readability practices, making it easier to add new features or make changes without introducing bugs.
What is ABAP Cleaner?
ABAP cleaner is a configurable tool that aims to automate whatever can be automated concerning ABAP code style.
What the ABAP Cleaner Tool Offers?
ABAP cleaner offers 59 cleanup rules with a total of 186 configuration options. Whereas, previously it offered 57 cleanup rules with a total of 162 configuration options.
Empty Lines
- Standardize empty lines within methods
- Separate methods and classes with empty lines
- Standardize empty lines in class definitions
Spaces
- Standardize spaces next to parentheses
- Close brackets at line end
- Remove space before commas and period
- Put spaces around “ comment sign
- Remove needless spaces
Declarations
- Unchain into multiple statements
- Rearrange local declarations
- Delete unused variables
- Simplify a chain with one element
- Make implicit type explicit
- Use FINAL for immutable variables
- Standardize escaping of !parameters
- Remove empty class definition SECTIONs
- Add missing parameters to ABAP Doc
- Remove lang=”en” from ABAP Doc
Syntax
- Comment with “, not with * (for text)
- Replace obsolete pseudo comments with pragmas
- Move pragmas to correct position
- Correct frequent typos
- Resolve equals sign chain into several commands
- Prefer calculation assignment operators (+=, -= etc.)
- Prefer =, <>, <= etc. to EQ, NE, LE etc.
- Prefer IS NOT to NOT IS
- Move AND/OR etc. from line end to next line start
- Remove empty commands
- Shorten VALUE statements
- Remove the self-reference me->
- Omit RECEIVING
- Omit optional EXPORTING
Commands
- Convert CHECK outside loop to IF NOT … RETURN
- Convert CHECK in loop to IF NOT … CONTINUE
- Replace long IF blocks at loop end
- Replace long IF blocks at method end
- Replace CALL METHOD with functional call
- Replace CREATE OBJECT with NEW constructor
- Replace RAISE … TYPE with RAISE … NEW
- Replace obsolete ADD … TO etc. with += etc.
- Replace obsolete MOVE … TO with =
- Replace TRANSLATE with string functions
- Use assert_true and assert_false
- Use assert_subrc instead of assert_equals
- Use assert class instead of ASSERT
Pretty Printer
Alignment
- Align ABAP Doc
- Align METHODS declarations
- Align METHODS … FOR TESTING
- Align METHODS … REDEFINITION
- Align ALIASES … FOR …
- Align declarations
- Align assignments to the same object
- Align keywords with second word of first line
- Align CLEAR:, FREE: and SORT
- Align parameters and components
- Align logical expressions
- Align conditional expressions
Requirements and Installation
To install and use the ABAP cleaner plug-in for ABAP Development Tools (ADT) on Windows or macOS,
- Install ABAP Development Tools as described in the Install ADT Tutorial (Step 1), using an Eclipse installation that is compatible with ADT.
- Start ADT, select the menu ‘Help / Install New Software…’, copy the link https://sap.github.io/abap-cleaner/updatesite to the ‘Work with’ field, and follow the installation steps, confirming to install ABAP cleaner. (Known issues)
After restarting ADT, open an ABAP code document in an editor, and use the menu ‘Source Code / Clean Up With Interactive ABAP Cleaner…’ (shortcuts Ctrl + 4 or Ctrl + Shift + 4),
If you select the first option “Clean Up with Automated ABAP Cleaner” it will automatically clean up the code based on the profile we have set up.
If you select the second option “Clean Up with Interactive ABAP Cleaner” here we can select the rules that we want to reflect in our code and here we have many options.
If we already created the profile, we can directly click on the “Save and Apply” button to apply the desired rules to be applied.
If we want to create a profile we need to click the configure button on the right corner.
On the top left in the profiles section we can Create, Copy, Delete, Import, and Export profiles.
On the bottom left in the rules section, we can check the checkbox that should clean up our code.
If the selection of rules is completed then click on the “save profiles and exit” button on the bottom right.
So, now we are set to give it a try.
In summary, the ABAP Cleaner tool is a valuable tool for automating the clean-up of ABAP code, ensuring uniformity, reducing errors, promoting modernization, fostering collective code ownership, and improving maintainability.
Have you used the ABAP Cleaner tool before? What was your experience like? Do you have any questions about Clean ABAP or the ABAP Cleaner tool?
Please feel free to share your thoughts in the comments section below.
If you enjoyed reading this blog post about Clean ABAP and would like to receive similar content in the future, please consider following my profile. By following my profile Gaddam Arun Kumar Reddy , you will receive updates on new blog posts and other content related to ABAP development best practices.
Follow the SAP ABAP environment Topic page: SAP ABAP
This page is a great resource for staying up-to-date on the latest news and developments in the SAP HANA environment. You can follow the page to receive updates, post and answer questions, and read other posts on the topic
If you have any queries related to SAP and ABAP Developments you can post here https://answers.sap.com/tags/73554900100700000996, you can also find many topics related to your query.
Thanks for sharing!
For reference, this is the earlier blog post from Klaus Haeuptle talking more about where ABAP Cleaner comes from. It looks like number of rules increased since then, which is encouraging.
Just a suggestion, I'm not sure posting a bunch of random (?) links adds value to this post (or any blogs, in general). If the goal was to highlight some rules, then I'd offer an explanation, e.g. "these rules are most helpful, in my opinion". These don't look like the most impactful rules though, e.g. not sure how many people care about empty lines. Just by looking at the list of links, someone could get a wrong impression, I suspect.
It is great to see posts on SAP Community talking about open source ABAP initiatives.
Thank you!
Hi Jelena Perfiljeva,
I appreciate your suggestion. I have provided links to resources that I believe will be helpful for beginners in ABAP.
Thank you for your support.
Hi Gaddam,
thank you for the information about this Eclipse tool for ABAP. When installing it, there is a warning that the content is unsigned and of unknown origin - do you know if this could be changed in the future?
At my company, we normally don't trust unsigned content - and it would be a pity if we can't use it because of that.
Thank you and best regards,
Lisa