SonarQube Clean as You Code
Old projects often carry thousands of issues. Fixing them all before shipping anything new feels impossible, so teams give up. Clean as You Code offers a practical answer: keep every new or changed line clean, and let the old code improve over time.
The Kitchen Picture
A messy kitchen overwhelms anyone who starts cleaning everything at once. A simpler rule works better: wash each dish right after use. The pile never grows, and the old pile shrinks whenever someone has spare time. Clean as You Code applies this habit to software.
Old Code (untouched) New Code (changed lately) ┌──────────────────────┐ ┌──────────────────────┐ │ 1,200 old issues │ │ Rule: zero new │ │ Fix when you touch │ │ issues allowed │ │ the file │ │ Quality gate checks │ └──────────────────────┘ └──────────────────────┘
The Core Idea
The method splits code into two zones. Overall Code holds everything. New Code holds lines added or changed since a chosen starting point. The quality gate judges New Code only, so a legacy project can pass the gate on day one as long as fresh work stays clean.
Choosing the New Code Period
The new code period defines where New Code begins. SonarQube offers several choices.
| Option | Best For |
|---|---|
| Previous version | Teams that release numbered versions |
| Number of days | Continuous delivery teams, such as the last 30 days |
| Reference branch | Teams that compare feature work against main |
| Specific analysis | Teams that mark a fixed milestone |
Set It in the Interface
- Open your project and select Project Settings.
- Choose New Code.
- Pick an option and save.
The Default Quality Gate
The built-in quality gate, called Sonar way, applies Clean as You Code directly. It checks New Code for these conditions.
- No new issues appear.
- Every new security hotspot gets reviewed.
- New code coverage reaches at least 80%.
- New duplicated lines stay at 3% or lower.
A Day in the Life
- A developer edits a file that holds ten old issues.
- The developer adds a new function with one fresh smell.
- The scan flags the new smell, and the gate fails.
- The developer fixes the smell and pushes again.
- The gate passes, and the ten old issues wait for their turn.
The team never stops delivery, yet quality only moves upward.
Why the Method Works
Small Scope
Developers fix issues in code they just wrote, while the logic stays fresh in their minds. Fixes take minutes instead of hours.
Fair Judgment
Nobody carries blame for code written years ago. Reviews focus on today's work.
Steady Progress
Files that change often receive the most cleanup, which means effort lands where the risk lives.
Rules for Teams
- Agree on one new code period for the whole organization.
- Block merges when the quality gate fails.
- Pair the gate with pull request feedback in a commercial edition.
- Review overall debt once per quarter to confirm a downward trend.
Common Questions
What About Old Issues?
Old issues stay visible. Schedule time for the critical ones, especially vulnerabilities, without blocking regular releases.
What if Code Moves Between Files?
SonarQube tracks moved lines when it can, but large refactors may mark moved code as new. Review these cases manually.
Quick Recap
Clean as You Code keeps new work spotless and lets old code heal gradually. The approach turns an overwhelming backlog into a manageable daily habit.
