New Code Definition

Setting up the right new code definition for your project is an important step in getting the most out of SonarCloud. When you do an analysis on your main branch (or other long-lived branches) SonarCloud uses the new code definition to determine which issues you should focus on fixing and highlights these as issues in new code. This helps you to use the Clean As You Code methodology to improve your code quality and code security in a manageable and efficient way.

Setting your new code definition

You can define new code at the organization level or project level:

  • Organization level: Set a global new code definition for new projects at My Organization > Administration > New Code. What you define as new code at the organization level will be the default setting for your new projects and only applies to new projects when they are imported into the SonarCloud organization.
  • Project level: Set a new code definition for your existing project at My Organization > My Project > Administration - New Code. What you define as new code at the project level will supersede what was defined at the Organization level.

For example, if you select My Organization > Administration > New Code > Previous version to set your new code period for a new project, you can later redefine what constitutes new code by navigating to My Organization > My Project > Administration - New Code to change the definition at the project level.

New code options

You can define new code using one of these four definitions:

  • Previous version: Issues in new code are those that have appeared since the most recent version increment of the project. 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 SonarCloud reads the version from the pom.xml file.
    • If the analysis is done with the SonarScanner for Gradle then SonarCloud 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. Do not use your build number as sonar.projectVersion.
  • Specific version: Issues in new code are those that have appeared since a specific, defined version of the project. The Specific version of a project is determined in the same way as for the Previous version option, above. SonarCloud defines the start date of the new code period as the date when the project was incremented to the specified version.
  • Number of days: Specify the number of days for a floating new code period. For example, setting the Number of Days to 30 creates a new code period beginning 30 days before the current date.
  • Specific date: Issues in new code are those that have appeared since a specific, defined date.

How the new code definition affects your analysis results

During analysis of the main branch or other long-lived branches what counts as a new code issue is determined as follows:

  1. SonarCloud calculates the start date of the new code period based on the new code option chosen.
    • In the case of the previous version and specific version options, this is the date when the project was first incremented to the version in question.
    • In the case of the number of days option, it is the current date minus the specified number of days.
    • In the case of the specific date option, it is simply the specified date.
  2. All lines of code in all files under analysis that have changed since the start date of the new code period are marked (and displayed in yellow in the SonarCloud interface).
  3. All issues with one or more of the marked lines as primary or secondary locations are categorized as new code issues.

The set of new code issues, in turn, affects many aspects of your results:

  • The default quality gate applies conditions only to new code issues.
  • New code metrics are separated from overall code metrics in the main branch overview and the overviews of the other branches.
  • The Measures panel separates new code vs overall code data.
  • The Issues page has a creation date facet where you can select to see only issues in new code or issues in overall code.
  • The activity graphs separate activity in new code from activity in overall code.

© 2008-2022, SonarCloud by SonarSource SA. All rights reserved.