Book Image

Sonar Code Quality Testing Essentials

By : Charalampos S Arapidis
Book Image

Sonar Code Quality Testing Essentials

By: Charalampos S Arapidis

Overview of this book

Sonar is an open source platform used by development teams to manage source code quality. Sonar has been developed with this main objective in mind: make code quality management accessible to everyone with minimal effort. As such, Sonar provides code analyzers, reporting tools, manual reviews, defect-hunting modules, and TimeMachine as core functionalities. It also comes with a plugin mechanism enabling the community to extend the functionality, making Sonar the one-stop-shop for source code quality by addressing not only the developer's requirements, but also the manager's needs.The "Sonar Code Quality Testing Essentials" book will help you understand the different factors that define code quality and how to improve your own or your team's code using Sonar. You will learn to use Sonar effectively and explore the quality of your source code in the following axes: Coding Standards Documentation and Comments Potential Bugs and Defects Unit Testing Coverage Design and Complexity Through practical examples, you will customize Sonar components and widgets to identify areas where your source code is lacking. The book goes down to proposing good practices and common solutions that you can put to use to improve such code.You will start with installing and setting up a Sonar server and performing your first project analysis. Then you will go through the process of creating a custom and balanced quality profile exploring all Sonar components through practical examples. After reading the book, you will be able to analyze any project using Sonar and know how to read and evaluate quality metrics.Hunting potential bugs and eliminating complexity are the hottest topics regarding code quality. The book will guide you through the process of finding such problematic areas, leveraging and customizing the most appropriate components. Knowing the best tool for each task is essential. While you improve code and design through the book, you will notice that metrics go high and alerts turn green. You will use the Time Machine and the Timeline to examine how your changes affected the quality."Sonar Code Quality Testing Essentials" will enable you to perform custom quality analysis on any Java project and quickly gain insight on even large code bases, as well as provide possible solutions to code defects and complexity matters.
Table of Contents (18 chapters)
Sonar Code Quality Testing Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Sonar Metrics Index

Appendix A. Sonar Metrics Index

This appendix lists all Sonar metrics in separate categories along with a brief description for each metric and information on where to locate it within the Sonar user interface.

Sonar metrics

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

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: if, for, while, case, catch, throw, return, &&, ||, and ?.

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

Design metrics

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 metrics

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

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.

General metrics

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

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)

Rules Compliance metrics

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.

Size metrics

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:

expression, if, else, while, do, for, switch, break, continue, return, throw, synchronized, catch, and finally.

Management metrics

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.