About new code
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 a couple of use cases:
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 version
Defines new code as any code that has changed since a specific, defined version of the project. For more compliance with the Clean as You Code methodology, this option cannot be set in the UI, as it would require frequent user action to be kept up to date. This can only be set using the API.
Specific date
Defines new code as any code that has changed since a specific, defined date. This can only be set using the API.
New code period
For all the above options, SonarQube Cloud 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 version: Date at which the project was upgraded to the version in question.
- Specific date: The specified date.
All lines of code in all files under analysis that have changed since the start date of the new code period are marked. In SonarQube Cloud, these lines are displayed in yellow.
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 Server and SonarQube Cloud:
- 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 Server, the new code definition can be set at the organization or project level.
To learn how to configure these options, see "Setting a new code definition" in the Setting up Clean as You Code section.
Related pages
Was this page helpful?