Start Free
Latest | Core concepts | Clean as You Code | About new code

About new code

On this page

New code is code that you've recently added or modified. To ensure you focus your efforts on the right lines of code, Sonar will highlight technical debt (issues, code coverage, etc) in the new code.

In pull requests, all code changes are considered new code. For branch and IDE, the new code definition setting tells Sonar which part of the code is considered new. 

Your new code definition

Each project in the Sonar solution has a new code definition. It’s a setting that determines what code in your projects is considered new code. 

Depending on the type of project you're working on, the best option to use will vary.  Here are general use cases for various types of projects: 

Recommended new code definition option for different types of projects.

New code definition options

Previous version

Defines new code as any code that has changed since the most recent version increment of the project. 

Number of days

Defines new code as any code that has changed in the last X days.

Specific analysis

Defines new code as any changes made since that specific analysis. 

This option gives you more control over your new code than the "Number of days" option. For example, for project that follows a continuous delivery model, it allows you to mark the start of a new cycle, where a number of days would not be accurate enough.

Reference branch

Any differences between your branch and a selected reference branch are considered new code.

New code period vs. branch comparison

  • For the previous version, number of days, and specific analysis options, SonarQube calculates a new code period with a start and end date. All the code that falls between the date of your last analysis and the start date is considered new code. Here is how the start date is calculated:
      • Previous version: Date when the project was first incremented to the version in question.
      • Number of days: Current date minus the specified number of days.
      • Specific analysis: Date of the past analysis.
  • For the reference branch option, the analyzed branch is compared to the current state of the reference branch based on SCM data obtained during analysis. If the SCM is not available, the two branches are compared based on their current state in SonarQube.

All lines of code in all files under analysis that are not in the reference branch or have changed since the start date of the new code period are marked. In SonarQube, these lines are displayed in yellow.

See also Issue identification mechanisms to learn how new issues are calculated.

Focus on new code in the UI

In the Sonar solution, all views that display found issues or other code metrics always prioritize the status of new code. Data on overall code is available but is not displayed by default. This encourages you to maintain focus on the new code.

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.
  • In SonarQube and Sonarcloud: 
    • There are separate tabs for new code data vs. overall code data. 
    • Selecting the Issues in new code filter found on your project > Issues allows you to quickly switch between issues in new code or issues in overall code.
    • The activity graphs separate activity in the new code from activity in the overall code.

Next steps

In SonarQube, the new code definition can be set at different levels (global, project, and, starting in Developer Edition, at the branch level). 

To learn how to configure these options, see "Setting a new code definition" in the Setting up Clean as You Code section.


Was this page helpful?

© 2008-2024 SonarSource SA. All rights reserved. SONAR, SONARSOURCE, SONARQUBE, and CLEAN AS YOU CODE are trademarks of SonarSource SA.

Creative Commons License