Chapter 12Lesson 01~105 minutes

New Code, Clean-as-You-Code, Baselines, and Legacy Modernization: Core Concepts and Mental Model

Learn how SonarQube separates overall code from governed new code, how each baseline mode changes that population, and why Clean-as-You-Code is a modernization strategy rather than a way to forget legacy risk.

New CodeClean as You CodeBaselineLegacyQuality Gate

Learning objectives

  • Separate overall-code state from the new-code population used by quality standards and gates.
  • Explain Previous version, Number of days, Specific analysis, and Reference branch without treating them as interchangeable labels.
  • Trace how project/global new-code configuration and SCM/revision evidence influence what SonarQube considers new.
  • Explain why Clean-as-You-Code improves legacy systems incrementally without declaring old risk harmless.
  • Inspect a project’s current new-code definition and analysis history before changing policy.
  • Recognize baseline changes as governed production policy rather than a routine troubleshooting shortcut.

1. Current baseline and chapter boundary

Chapter 11 established that a quality gate evaluates a particular code population after Compute Engine finishes an analysis. Chapter 12 asks the next question: which lines and findings belong to the “new code” population? That answer is a policy decision expressed by the project’s New Code definition.

SonarQube Community Build separates overall code—the complete analyzed codebase—from new code, the recently added or modified portion selected by the configured definition. A healthy modernization program uses the strictest practical quality standard on new code while retaining visibility and separate ownership for inherited legacy risk.

Governance boundary: changing a baseline can change measures, issue populations, and gate status without changing one byte of source. Never move the baseline simply because an analysis failed. Treat the definition, reference/value, approver, reason, and rollback as production policy evidence.

2. Why legacy systems need a stable modernization boundary

A large inherited application may have years of low coverage, complex code, or unresolved issues. A big-bang policy that requires the entire codebase to meet a new standard before the next deployment can stop delivery without creating a feasible remediation path. The opposite extreme—ignoring all legacy debt—allows serious risk to persist indefinitely.

Clean-as-You-Code supplies a third operating model: prevent new debt and repair old code when you touch it, while governing the remaining legacy backlog separately. This makes improvement incremental, measurable, and compatible with normal product work. It does not mean old vulnerabilities, regulatory defects, or high-impact reliability risks can be forgotten.

3. Mental model: overall state plus a governed new-code lens

New Code population and modernization feedback
flowchart TD
O[Overall analyzed codebase] --> D[New Code definition]
R[Revision and SCM history] --> D
V[Version / analysis / time / reference] --> D
D --> N[New or modified code population]
P[Quality profile and rules] --> A[Analysis findings and measures]
N --> A
O --> A
A --> G[New-code quality gate]
G --> F[Modernization feedback]
L[Legacy-risk backlog] --> F
F --> C[Code fixes and governed future changes]

The overall codebase remains visible. The New Code definition applies a lens using version, time, a specific analysis, or a reference branch. Scanner/SCM evidence determines which lines are added or modified. The same rules analyze the code, but SonarQube reports separate new and overall measures so the gate can focus on recent changes while legacy work continues under its own risk plan.

4. Current New Code definition modes

Definition Meaning Good fit Governance risk
Previous version Code changed since the most recent project-version increment. Regular releases with deliberate versioning. Incrementing the version after a failure can move the baseline and hide the failed change from “new.”
Number of days Code changed within the last X days; current docs allow up to 90 days. Continuous delivery with a stable cadence. A floating time window changes automatically; comparison dates must be recorded.
Specific analysis Changes since a selected historical analysis. Explicit cycle baseline or controlled migration checkpoint. Manual baseline movement can become result-driven unless change control is strict; current Community Build makes this API-only.
Reference branch Differences between analyzed code and the current reference branch, using SCM evidence. Branch/PR workflows and short-lived comparisons where supported. Bad/missing Git history or the wrong reference makes the population wrong; edition/branch support must be verified.

Community Build’s current project documentation exposes configuration at global and project levels. A project-specific definition takes precedence over the inherited global baseline. The default global baseline is Previous version.

5. Previous version is a release concept, not “previous commit”

For Maven and Gradle, SonarQube can read the version from the build. With standalone Scanner CLI, set sonar.projectVersion explicitly. The Previous version definition uses the version boundary, not the immediately preceding Git commit. That distinction prevents a dangerous anti-pattern where every push would redefine new code and instantly make yesterday’s issue “old.”

Evidence chain
Git revision → scanner projectVersion → first analysis of that version → New Code start → new/overall measures → gate result

6. SCM history is part of the measurement system

SonarQube uses SCM data for blame, issue dating/backdating, branch comparisons, and New Code detection. A shallow clone removes evidence needed to determine when lines changed. Current scanner guidance recommends full-depth checkout and warns that blame retrieval is skipped for shallow clones and analysis may fail.

Therefore a baseline dossier must record the commit SHA and checkout depth/SCM status—not only the UI setting. “Previous version, 30 days, or reference branch” is incomplete evidence when the scanner cannot see trustworthy history.

7. Read-only inspection before touching the baseline

export SONAR_HOST_URL="http://localhost:9000"
export PROJECT_KEY="academy-sq-ch12"

# Server identity
curl -fsS "$SONAR_HOST_URL/api/server/version"

# Current project-level/inherited New Code definition
curl -fsS -H "Authorization: Bearer $SONAR_ADMIN_TOKEN" --get \
  --data-urlencode "project=$PROJECT_KEY" \
  "$SONAR_HOST_URL/api/new_code_periods/show"

# Analysis history: revision, version, build string, analysis key
curl -fsS -H "Authorization: Bearer $SONAR_ADMIN_TOKEN" --get \
  --data-urlencode "project=$PROJECT_KEY" \
  --data-urlencode "ps=20" \
  "$SONAR_HOST_URL/api/project_analyses/search"

# New versus overall coverage evidence after analysis
curl -fsS -H "Authorization: Bearer $SONAR_ADMIN_TOKEN" --get \
  --data-urlencode "component=$PROJECT_KEY" \
  --data-urlencode "metricKeys=coverage,new_coverage,ncloc" \
  "$SONAR_HOST_URL/api/measures/component"

Use a browse-capable or project-admin lab credential only where required. Never print the token itself. The instance’s built-in Web API documentation is authoritative because endpoint surfaces are evolving.

8. DevOps control: baseline identity belongs beside revision identity

A reproducible quality signal must identify revision + effective analysis inputs + New Code definition/reference + Compute Engine task + gate definition/result. If a gate becomes green after only the baseline changed, that is a policy event—not a code fix.

For legacy modernization, report both new and overall measures. New-code success demonstrates that the team is not adding fresh debt. Overall trends and a separate risk backlog demonstrate whether inherited debt is actually shrinking.

Knowledge check

Does “new code” mean only newly created files?

Why is a version bump after a failed Previous-version gate suspicious?

Which definition changes automatically as time passes even if code does not change?

What does a green new-code gate prove about severe legacy risk?

Why record full Git history/SCM state in baseline evidence?

Next lesson

Prove the population with a controlled legacy fixture

Lesson 2 creates a low-coverage legacy baseline, adds fully tested new code, pins a Specific analysis baseline, changes the definition once without changing source, and then restores the governed baseline.

Official references and version notes

Version and compatibility note

Rechecked 2026-09-07. Mandatory executable examples target SonarQube Community Build 26.9.0.129388 and SonarScanner CLI 8.1.0.6389. Current Community Build documentation lists Previous version, Number of days, Specific analysis, and Reference branch as New Code definition modes; the project-level definition overrides the global baseline, whose default is Previous version. Specific analysis is configured through the Web API. Commercial branch analysis and broader enterprise capabilities are not required by this chapter. Re-check the linked primary documentation and your instance’s built-in Web API before automating against another release.

Keep the academy open

Support free, practical DevOps education.

Every lesson is designed to remain readable in a browser, downloadable from GitHub, and usable without a paid learning platform. Contributions help expand and maintain the curriculum.

Ethereum / ERC-20
0x716c4Ab160C4B66F31a28AE2448BfF68fc3a2ef0Send only Ethereum/ERC-20 compatible assets to this address.