Install Free
VS Code | Core concepts | New code

Was this page helpful?

New code

On this page

New code in the context of Clean as You Code

Focusing on new code is an important step in getting the most out of SonarLint. When you run an analysis on your main branch (or other long-lived branches) in SonarQube or SonarCloud, SonarLint uses the New Code Definition (NCD) to determine which issues you should focus on fixing, and calls out that these issues are found in new code. Focusing on new code is at the core of implementing the Clean as You Code strategy, knowing that the other code will be incrementally fixed over time.

To achieve this, SonarLint offers the Focus on new code feature to highlight new code in the IDE. 

Your new code definition

When SonarLint is running in Connected Mode, the SonarLint uses the NCD defined in SonarQube or on SonarCloud. When SonarLint is running in standalone mode, a locally defined new code period highlights your new code.

Running SonarLint in Connected Mode with SonarQube or SonarCloud offers more opportunities to choose how you define new code.

New code definition options

When running SonarLint for Eclipse in Connected Mode, SonarLint uses the NCD found in the SonarQube or SonarCloud quality profile, which gives you more opportunities to choose how you define new code. Check out their respective pages for details.

Without Connected Mode, new code is defined by a new code period: any code added or changed in the last 30 days is considered new code. The 30-day timeframe is defined using Git. 

When not using Git, the new code period begins with the first SonarLint analysis.

There is no option to manually define a new code period in SonarLint. 

Setting your focus on new code

The Focus on new code feature works when SonarLint is running in either Connected Mode or standalone mode. New code is defined differently in each mode as mentioned above.

Setting your focus on new code has these prerequisites running in Connected Mode:

  • Your local project must be bound to a SonarQube or SonarCloud project.
  • The new code definition must be defined in SonarQube or SonarCloud using a Previous version, Number of days, or Specific analysis
  • The Reference branch new code definition is not supported. Please check the documentation in SonarQube or on SonarCloud to properly set your new code definition.

When not running in Connected Mode, the SonarLint focus feature can still be used to highlight only issues found in new code.

Setting the SonarLint focus on new code is easy. To activate or deactivate this mode, select SonarLint focus: in the VS Code Status Bar, then press Enter or choose Focus on new code or Focus on overall code from the Command Palette. Additionally, you can select or deselect the SonarLint focus mode from the VS Code > Settings… > Settings > Extensions > SonarLint > User settings menu.

By default, the SonarLint focus is set to overall code when you set up a new connection and establish the project binding. 

Running SonarLint in Connected Mode with SonarQube or SonarCloud offers more opportunities to choose how you define new code. Check out their respective pages for details.

How the new code definition affects the analysis results

The core idea of Clean as You Code is that you focus your attention and effort on new code (code that has been added or modified recently) and improve your code incrementally. SonarQube and SonarCloud use your NCD as a baseline for code comparison, to identify what is new code which reinforces the Clean as You Code strategy by only showing you issues found in new code.

  • When running SonarLint in Connected Mode and enabling the Focus on New Code feature, the NCD from SonarQube or SonarCloud is used to show you only issues found in new code.
  • When running SonarLint while not in Connected Mode and enabling the Focus on New Code feature, every SonarLint analysis will show you only issues found in new code

The Focus on new code feature gives you immediate feedback in the IDE, before you submit new code with new issues.

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

Creative Commons License