There are many static code analysis tools. Long list can be found on Wikipedia. At some point in time a manager or developer might come to you with one of these tools like “hey, at my previous customer we used SonarABAP for code measurements”.
This blog will explain the ABAP code metric tool built into netweaver. Together with the ATC tool the code metric tool covers most of the functions that such tools deliver.
Questions that will be answered in this blog are:
- How can I count the amount of lines of Z code in my SAP system?
- How to run ABAP code metric tool?
- What output will the ABAP code metric tool deliver?
- Use cases of the ABAP code metric tool?
The test the code metric tool write a small ABAP program like this:
This program has nested IF statements (3 levels deep), one ELSE statement and a nested DO.
Code metric tool
Start the custom code analysis tools with transaction /SDF/CD_CCA:
The code metric tool is the tool on the bottom of the screen.
On the start screen select the package and extra checks (selecting more packages and checks will increase the runtime of the tool):
Result for our test program:
The output is giving:
- LoC: lines of code
- NoS: number of statements
- NoC: number of comments
- COM: complexity of conditions
- TOTAL DD: complexity weighted by decision depth
- IF: 3 (this was in our test program)
- ELSEIF: 1
- DO: 2
- etc like amount of loops, selects, updates, deletes, modifies, case statements.
Use cases of Code Metric Tool
Use case 1: complex programs
Complex programs are normally source of most productive issues. You can use the code tool to check if the program is not too overly complex (like many nested if and case statements).
Use case 2: the procedural versus OO discussion
Sometimes you have managers wanting you to count the amount of Z objects. If you have switched from procedural coding to OO coding you will find the amount of objects to have increased. The code metric tool can help you here by counting the amount of statements and complexity. The amount of objects in OO is typically higher, but due to re-use and better setup, the total amount of statements and the total complexity decision depth should be smaller.
Use case 3: count the amount of Z code lines in your SAP system
Every now and then there is discussion on Z code. Sometimes the question will popup: “How many lines of Z code do we have in our SAP system?”. This question you can answer as well with the code metric tool explained above.
For reference: SAP standard has 238 million lines of code (source is this SAP blog).