Skip to content

SonarQube New Code vs Old Code

The definition of "New Code" (what SonarQube definies as new content between analyses) can be defined in the following ways.

Previous Version

This defines new code as any code that has changed code since the project's most recent version. The current version of a project is determined in different ways depending on the build system being used:

  • If the analysis is done using the SonarScanner for Maven, then SonarQube reads the version from the pom.xml file.
  • If the analysis is done with the SonarScanner for Gradle then SonarQube reads the version from the gradle.build file.
  • In all other cases, the version must be explicitly specified by setting the analysis parameter sonar.projectVersion Failing to do so will produce inaccurate results.

This available at the global, project, and branch levels.

Number of days

You can specify the number of days for a floating new code period. For example, if you set the number of days to 30 this creates a new code period beginning 30 days before the current date.

This available at the global, project, and branch levels.

Specific analysis

You can choose a previous analysis as your new code definition. This denotes any changes made since that analysis as New Code. Issues in new code are those that have appeared since a specific, defined version of the project. The version of a project is determined in the same way as for the previous version option, above. SonarQube defines the start date of the new code period as the date when the project was incremented to the specified version.

This available at the branch.

How to Set New Code Definition

You can define new code at the global, project, or branch level. You will need project admin-level access.

  • Global level: Set a global new code definition at Administration> Configuration > General Settings > New Code. What you define as new code at the global level will be the default for your projects.
  • Project level: Set a new code definition for your project at Project Settings > New Code. What you define as new code at the project level will be the default for the project's branches.
  • Branch level: You can define new code for each branch from the Actions column of the branches table on the project's New Code settings page at the bottom.

How is the number of New Lines defined?

New Code and Overall code are calculating different things. In SonarQube, the "lines of code" metric can be calculated for both "new code" and "overall code." When you analyze a branch, the "new code" metric only considers the code changes that were made on that branch since the last analysis, while the "overall code" metric considers all the code in the main branch. This includes any code that was already analyzed in the main branch but, does not include extra line returns or comments.

In this example, there are 1 line of comments and 2 lines of code statements, and 9 lines of line returns for a total of 12 lines

As you can see the new lines of code refer to the 12 new lines that I have added, whereas the overall code refers to the two lines of code of actual code.

Your SonarQube portfolios only consider selected branches' number of lines (by default the main) of each individual project when aggregating the Lines of Code metric. Only branches excluded from purging are available for selection for comparison. Meaning branch that will not be automatically deleted when inactive.

When you analyze a branch, the "new code" metric only considers the code changes that were made on that branch since the last analysis, while the "overall code" metric considers all the code in the main branch, including any code that was already analyzed in the main branch.

How you can define the New Code for your project is documented above.

"New Code is defined as a changeset or period that you're keeping a close watch on for the introduction of new problems in the code. Ideally, this is since the previous_version, but if you don't use a Maven-like versioning scheme you may need to set a time period such as 21 days, since a specific analysis, or use a reference branch."

Your SonarQube project/portfolios only consider selected branches' total number of new lines added in the code based on the New Code definition for aggregating the New Code of each individual project when aggregating the Lines of Code metric including line returns and commented lines. However, commented lines are not counted towards lines of code.

When calculating Overall Code