-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Sonar Code Quality Testing Essentials
By :

Sonar metrics are categorized into the following categories:
Complexity
Design and Architecture
Documentation
Duplication
General
Unit Tests
Rules Compliance and Violations
Project Size
Management
Complexity metrics are available in the Complexity and Quality Index widgets in the dashboard. The Quality Index widget is available as a plugin.
Metric name |
Definition |
Sonar component |
---|---|---|
Complexity |
The total Cyclomatic Complexity number was introduced by McCabe. For each of the following java statements the number increments by one: |
This is available via the Complexity widget. Navigate to Dashboard | Complexity Widget | Total. |
Complexity / class |
Average Cyclomatic Complexity per class. |
This is available via the Complexity widget. Navigate to Dashboard | Complexity widget | Per Class metric. |
Complexity / file |
Average Cyclomatic Complexity per file. |
This is available via the Complexity widget. Navigate to Dashboard | Complexity widget | Per File metric. |
Complexity / method |
Average Cyclomatic Complexity per method. |
This is available via the Complexity widget. Navigate to Dashboard | Complexity widget | Per Method metric. |
Complexity Factor |
Density of complexity in methods in percentage. |
This is available via the Quality Index widget. Navigate to Dashboard | Quality Index widget | Complexity factor percentage. |
Complexity Factor Methods |
Methods with high complexity density. |
These are available via the Quality Index widget. Navigate to Dashboard | Quality Index widget | Complexity Factor Methods total. |
QI Complexity |
Complexity rating in respect to total LOC. |
This is available via the Quality Index widget. Navigate to Dashboard | Quality Index widget | QI Complexity Bar |
The formula for the QI Complexity metric is:
(Complexity > 30 *10 + Complexity > 20 * 5 + Complexity > 10 * 3 + Complexity > 1) / effective lines of code
Abstractness and Instability design metrics as specified by Robert C. Martin have not been implemented yet; however, there is an open ticket by the Sonar development team at http://jira.codehaus.org/browse/SONAR-94.
Metric name |
Definition |
Sonar component |
---|---|---|
Abstractness |
The ratio of the number of abstract classes (and interfaces) to the total number of classes in the analyzed package. |
To be implemented—open ticket SONAR-94. |
Afferent couplings |
Number of other classes that use this class. |
These are available via the Sonar Sourcecode viewer Drill down to Class level | Open in Sourcecode viewer | Dependencies tab. |
Depth in tree (DIT) |
Number of parent classes. |
To be implemented—open ticket SONAR-94 |
Efferent couplings |
Number of classes that are used by this class. |
This is available via the Sonar source code viewer. Drill down to Class level | Open in Sourcecode viewer | Dependencies tab |
File dependencies to cut |
Total number of dependencies between files. |
This is available via the Package Design widget. Navigate to Dashboard | Package Design widget | Total dependencies between files. |
Instability |
The number of classes inside a package that depend on classes outside the package. |
This is available via the Sonar source code viewer. Drill down to Class level | Open in Sourcecode viewer | Dependencies tab |
Lack of cohesion of methods (LCOM4) |
Correlation between the methods and the local instance variables of a class. Methods not related to local fields increase the class LCOM number by one. |
These are available via the Sonar source code viewer. Drill down to Class level | Open in Sourcecode viewer | LCOM4 tab Alternatively, navigate to Dashboard | Chidamber and Kemerer widget | Files having LCOM4 greater than 1. |
Number of Children (NOC) |
Number of descendants of the class. |
This is available via the Sonar source code viewer Drill down to Class level | Open in Sourcecode viewer | Source tab (on the left-hand side of the header) |
Package cycles |
The minimum number of package cycles detected while traversing a package to identify dependencies. |
These are available via the Package Design widget. Navigate to Dashboard | Package Design widget | Total cycles. |
Package dependencies to cut |
Total number of dependencies between packages. |
This is available via the Package Design widget. Navigate to Dashboard | Package Design widget | Total dependencies between packages. |
Package tangle index |
Level of tangle of the packages; the best is 0 percent. |
This is available via the Package Design widget. Navigate to Dashboard | Package Design widget | Package tangle index in percentage. |
Response For Class (RFC) |
Total number of methods that can be potentially executed by an object of this class counting distinct calls made by the methods in the class. |
This is available via the Chidamber and Kemerer widget. Navigate to Dashboard | Chidamber and Kemerer widget | Response for Class | value/class. |
Documentation-related metrics are available via the Comments and Duplications widget.
Metric name |
Definition |
Sonar component |
---|---|---|
Blank comments |
Empty comment lines. |
These are available via the Treemap. Navigate to Dashboard | Components | Treemap on the right | Set size to Blank Comments metric. |
Comment lines |
Number of Javadoc, multi-comment, and single-comment lines. Empty comment lines, header file comments, and commented-out lines of code are not included. |
These are available via the Comments and Duplications widget. Navigate to Dashboard | Comments and Duplications widget | total lines. |
Commented-out LOC |
Commented out lines of code. The Javadoc blocks are excluded. |
This is available via the Comments and Duplications widget. Navigate to Dashboard | Comments and Duplications widget | total commented LOCs. |
Density of Comments (%) |
Number of comment lines / (lines of code + number of comments lines) * 100. |
This is available via the Comments and Duplications widget. Navigate to Dashboard | Comments and Duplications widget | comments percentage value. |
Density of Public documented API (%) |
(Number of public API - Number of undocumented public API) / Number of public API * 100. |
This is available via the Comments and Duplications widget. Navigate to Dashboard | Comments and Duplications widget | documented API percentage value. |
Public undocumented API |
Number of public API without Javadoc. |
This is available via the Comments and Duplications widget. Navigate to Dashboard | Comments and Duplications widget | undocumented API total value. |
Duplication metrics are available via the Comments and Duplications widget and the Useless Code Tracker is available as a plugin.
Metric name |
Definition |
Sonar component |
---|---|---|
Duplicated blocks |
Number of comment lines / (lines of code + number of comments lines) * 100. |
This is available via the Comments and Duplications widget. Navigate to Dashboard | Comments and Duplications widget | number of blocks. |
Duplicated files |
Number of files containing duplicated code. |
This is available via the Comments and Duplications widget. Navigate to Dashboard | Comments and Duplications widget | number of files. |
Duplicated lines |
Number of physical lines touched by duplication. |
This is available via the Comments and Duplications widget. Navigate to Dashboard | Comments and Duplications widget | number of lines. |
Density of duplicated lines (%) |
Duplicated lines / Physical lines * 100. |
This is available via the Comments and Duplications widget. Navigate to Dashboard | Comments and Duplications widget | percentage value. |
Useless code |
Total number of lines that can potentially be reduced via refactoring. |
This is available via the Useless Code Tracker widget. Navigate to Dashboard | Useless Code Tracker widget | total number of useless LOCs. |
The Quality Index metric is available through the Quality Index plugin. Install it from http://docs.codehaus.org/display/SONAR/Quality+Index+Plugin or from Sonar Update Center | Plugin Library.
Metric name |
Definition |
Sonar component |
---|---|---|
Profile version |
Version of the Sonar analysis profile. |
This is available via the Description widget. Navigate to Dashboard | Description widget | Profile Version value. |
Quality Index |
A value on scale of 0 to 10 based on the following four weighted axes of quality: Coding Violations, Complexity, Coverage, and Checkstyle Standards |
This is available via the Quality Index widget. Navigate to Dashboard | Quality Index Widget | Total Quality value. |
Code Coverage and Unit Test metrics are displayed with the Coverage widget from the project dashboard. In differential mode, the widget reports only on new/updated code, as you can see in the following screenshot:
![]() |
In the following table, the Sonar Component column is omitted because all metrics are available from the same widget—the Code Coverage widget.
Metric name |
Definition |
---|---|
Branch Coverage |
Percentage value of covered branches in program flow structures (Boolean expressions). |
Coverage |
Percentage value of total coverage combining line and branch coverage. |
Line Coverage |
Percentage value of number of lines executed/covered in unit tests. |
Lines to cover |
Total number of uncovered LOCs in unit tests. |
New branch coverage |
As Branch Coverage but only for new/updated code. |
New branches to cover |
Total number of uncovered branches only in new/updated code. |
New coverage |
As Coverage but only for new/updated code. |
New line coverage |
As Line Coverage but only for new/updated code. |
New lines to cover |
As Lines to cover but only for new/updated code. |
Uncovered branches |
Total number of branches not covered by unit tests. |
Uncovered lines |
Total number of lines of code that are not covered by unit tests. |
New uncovered branches |
As Uncovered branches to cover but only for new/updated code. |
New uncovered lines |
As Uncovered lines to cover but only for new/updated code. |
Skipped unit tests |
Number of skipped unit tests. |
Unit tests |
Total number of unit tests. |
Unit test errors |
Number of unit test errors—assertion errors. |
Unit test failures |
Number of unit tests that failed with an unhandled exception. |
Unit test success (%) |
Percentage value of successful unit tests—excluding errors and failures. |
Unit tests duration |
Total duration of unit tests' execution time. |
The formula for the Coverage metric as implemented by the Sonar development team is:
Coverage = (CT + CF + LC) / (2*B + EL)
CT: Branches that evaluated to true
at least once
CF: Branches that evaluated to false
at least once
LC: Lines covered (lines_to_cover - uncovered_lines
)
B: Total number of branches (2*B = conditions_to_cover
)
EL: Total number of executable lines (lines_to_cover
)
You can review Rules and Violations metrics from the project dashboard by using the default Rules Compliance widget and Useless Code Tracker with Quality Index widgets available as plugins. Notice that the widgets report on new violations and metric values when in differential mode for new/updated code.
Metric name |
Definition |
Sonar component |
---|---|---|
Rules Compliance |
Weighted violations percentage value. |
This is available via the Rules Compliance widget. Navigate to Dashboard | Rules Compliance widget | percentage value |
Violations |
Total number of code violations. |
This is available via the Rules Compliance widget. Navigate to Dashboard | Rules Compliance widget | Total number |
Weighted Violations |
Total sum of weighted violations (number of violations * weight value) |
This is available via the Rules Compliance widget. Navigate to Dashboard | Rules Compliance widget. |
Blocker Violations |
Total value of Blocker level code violations. |
This is available via the Rules Compliance widget. Navigate to Dashboard | Rules Compliance widget | Blocker. |
Critical Violations |
Total value of Critical-level code violations. |
This is available via the Rules Compliance widget. Navigate to Dashboard | Rules Compliance widget | Critical |
Major Violations |
Total value of Major-level code violations. |
This is available via the Rules Compliance widget. Navigate to Dashboard | Rules Compliance widget | Major. |
Minor Violations |
Total value of Minor-level code violations. |
This is available via the Rules Compliance widget. Navigate to Dashboard | Rules Compliance widget | Minor. |
Info Violations |
Total value of Info-level code violations. |
This is available via the Rules Compliance widget. Navigate to Dashboard | Rules Compliance widget | Info. |
New Blocker Violations |
Same as Blocker Violations but for new/updated code only. |
This is available via the Rules Compliance widget. Navigate to Dashboard | Rules Compliance widget | Blocker in differential mode. |
New Critical Violations |
Same as Critical violations but for new/updated code only. |
This is available via the Rules Compliance widget. Navigate to Dashboard | Rules Compliance widget | Critical in differential mode. |
New Major Violations |
Same as Info violations but for new/updated code only. |
This is available via the Rules Compliance widget. Navigate to Dashboard | Rules Compliance widget | Major in differential mode. |
New Minor Violations |
Same as Major violations but for new/updated code only. |
This is available via the Rules Compliance widget. Navigate to Dashboard | Rules Compliance widget | Minor in differential mode. |
New Info Violations |
Same as Minor violations but for new/updated code only. |
This is available via the Rules Compliance widget. Navigate to Dashboard | Rules Compliance widget | Info in differential mode. |
New Violations |
Total number of violations in new code only. |
This is available via the Rules Compliance widget. Navigate to Dashboard | Rules Compliance widget | Added in differential mode. |
Dead Code |
Total lines of code in unused private methods. |
This is available via the Useless Code Tracker widget. Navigate to Dashboard | Useless Code Tracker widget | total number of LOCs. |
Potential Dead Code |
Total lines of code in unused protected methods. |
This is available via the Useless Code Tracker widget. Navigate to Dashboard | Useless Code Tracker widget | total number of LOCs. |
Quality Index Coding Weighted Violations |
Quality Index on Coding violations calculated is by the formula: (Blocker * 10 + Critical * 5 + Major * 3 + Minor + Info) / LOCs |
This is available via the Quality Index widget. Navigate to Dashboard | Quality Index widget | Coding Bar. |
Quality Index Style Weighted Violations |
Quality Index on Checkstyle violations is calculated by the formula: QI Style = (Errors*10 + Warnings) / LOCs * 10 |
This is available via the Quality Index widget. Navigate to Dashboard | Quality Index widget | Style Bar. |
The following metrics are displayed in the Size widget from the project dashboard. On the left-hand side of the widget there is information on line levels and statements, while on the right-hand side of the widget there is information on packages and classes.
Metric name |
Definition |
---|---|
Accessors |
Number of getter and setter methods. |
Classes |
Number of classes including nested classes, interfaces, enums, and annotations. |
Directories |
Number of analyzed directories. |
Files |
Number of analyzed files. |
Lines |
Number of carriage returns. |
Lines of code |
Number of physical lines of code excluding blanks, comments, and commented-out code. |
Methods |
Total number of methods excluding accessors. |
Packages |
Total number of packages. |
Statements |
Total number of statements. The statements counter gets incremented by one each time one of the following is encountered:
|
The next three metrics are business oriented and you can add them to the Custom Measures widget. You can also add it to the dashboard.
Metric name |
Definition |
---|---|
Burned Budget |
The budget already used in the project. |
Business Value |
An indication of the value of the project to the business. |
Team size |
The size of the project team. |
Change the font size
Change margin width
Change background colour