Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 37 ยท Rules and Quality Gates

New Code and Clean as You Code

Learn the Clean as You Code approach and the new code definition options: previous version, number of days, reference branch and specific analysis, with calculators.

Imagine you adopt SonarQube on a ten-year-old project and the first scan shows 4,000 issues. If the quality gate demanded zero, nobody could ever merge. SonarQube's answer is Clean as You Code: judge only the code that is new or changed, keep that clean, and let the old code improve naturally as you touch it. The mechanism is the new code definition (formerly the new code period).

The idea in one picture

Clean as You Code

New code Must pass the gate. Always.
Old code Fix it when you touch it.
Result Quality rises without a big rewrite.

Stop adding debt first, then pay it down.

1 / 2

What counts as new code

You choose a new code definition per project (or a global default), and optionally per long-lived branch. The options are:

  • Previous version: everything changed since the last time sonar.projectVersion changed. Good for projects with versioned releases. Remember to set a new version number for each release.
  • Number of days: everything changed in the last N days (default 30). A rolling window that needs no release discipline; but old code that nobody touched ages out of the window.
  • Reference branch: everything that differs from a named branch (usually main or a release branch). The natural choice for feature branches and for pull requests; code is new if it is not on the reference branch.
  • Specific analysis (Developer Edition and above): a particular past analysis as the baseline. Handy to say "everything since our migration".

How does SonarQube know which lines changed? It uses SCM data (git blame information) that the scanner collects. If the scanner cannot read your git history, every line can look new or old by accident; in CI, make sure the clone includes history (for GitHub Actions use fetch-depth: 0), because a shallow clone loses blame data.

Calculating the new code window

This stand-in marks lines as new using the number of days and previous version strategies from a blame list, so you can see how the definitions differ.

Python Which lines are new? (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Setting it up

  • Global default: Administration > Configuration > General Settings > New Code (labels vary by version).
  • Per project: Project Settings > New Code.
  • Per branch: for long-lived branches in editions that support them, so a release branch can use reference branch while main uses previous version.

For pull requests there is no setting: the changes in the request against its base branch are always the new code.

A recommended starting point for most teams: Reference branch set to main for pull-request-centric workflows, or Previous version for release-based products where you bump sonar.projectVersion in your build file for every release.

Overall code still matters

Clean as You Code does not mean ignoring old code. SonarQube shows two tabs on the dashboard: New Code and Overall Code. The gate usually judges the first, while the second tells you the total debt. Use the overall view to plan cleanup work: for example, a quarterly goal like "reduce high-severity issues in overall code by half" with a regular budget of refactoring in each sprint.

New code and the gate: a worked example

Suppose your gate requires coverage on new code of at least 80%. You add 50 new lines and cover 38 of them: coverage on new code is 76%, which fails. The old 20,000 lines with 40% coverage are irrelevant to this gate. After you add tests for the rest, the 80% is reached and the gate passes, although the project overall is still below 50%.

Python Coverage on new code versus overall (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Pitfalls

  • Squashing or rebasing history can change blame dates and move lines in and out of new code. If numbers look odd, check how the scanner reads SCM data.
  • Bulk reformatting makes every line "changed" and thus new. Do reformatting in its own commit and consider excluding it, or accept the one-time gate spike.
  • Forgetting to bump the version with Previous version means new code never resets.
  • Days window too short lets problems age out unfixed. A reference branch is more reliable.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main idea of Clean as You Code?

    The gate judges new code; old code is cleaned when touched, so quality rises without a big-bang rewrite.
  2. 2. Which new code definition uses a branch as the baseline?

    With a reference branch, code that differs from that branch is new.
  3. 3. What must the CI checkout include so SonarQube can detect changed lines?

    Blame data comes from the history; a shallow clone makes line dates unreliable.
  4. 4. A project has 76% coverage on new code and the gate needs 80%. What happens?

    The condition applies to new code; 76% is below 80%, so it fails regardless of overall coverage.

Summary

  • Clean as You Code keeps new and changed code clean without demanding perfection from old code.
  • New code definitions: previous version, number of days, reference branch, specific analysis.
  • It depends on SCM data, so CI needs full git history.
  • Use the Overall Code tab for planning cleanup work.
  • Avoid pitfalls: bulk reformatting, history rewrites, forgotten version bumps.

Next: Complexity and Duplication