What I wasn’t aware before is, that the Code Inspector (transaction SCI) already offers quite software metrics out of the box. In the remainder of this block I show how to configure the Code Inspector to calculate the delivered software metrics.
In order to calculate software metrics using the Code Inspector the first step is to create a check variant. Be sure to create a global check variant. Only global check variants can later be used from within ADT. For this blog I created a check variant named Z_SOFTWARE_METRICS.
In this check variant I only activated the checks in the “Metrics and Statistics” group.
Furthermore, I set the reporting to “Show metrics”. There reason is twofold. First, I wanted to see that the software metrics are calculated and what the actual results are. Second, the default thresholds for any warning are so high, that you’d have to write really really complex (i.e. bad) code in order to trigger the warnings. Of cause it would have also been possible to lower the thresholds for the warnings.
However, this didn’t have the expected result. The calculated metrics where not displayed. Therefore, I change the reporting to “Show warnings” and the threshold to 1 for all metrics.
After the check variant is created it is now possible to trigger the calculation of the software metrics in the ADT. To do this right click on the object (program, class, package) for which the metrics should be calculated. In the context menu select “Run As” → “ABAP Test Cockpit with…”
This opens a popup windows in which the check variant can be selected. Note that this window support search and completion using the <ctrl>-<space> shortcut. So simply type Z and trigger the search to select the required check variant.
For this blog I recreate one of the famous counter examples of the cyclomatic complexity metric, a case statement mapping the numbers 1 to 7 to weekdays. The following screenshot shows the code of this example as well as the result of calculating the software metrics. Markers were added to some of the lines (e.g. 1, 15 and 17).
Detailed results of the calculated metrics are shown in the ATC Problems view. For the example the calculated cyclomatic complexity is the expected 8. Furthermore, also the Halstead metrics were calculated.