Skip to content
$ lynuxpro
← SonarQube & Code Quality

LESSON 2 OF 16 · BEGINNER · 10 MIN

Core concepts

Free previewLesson 2 of the 2 free lessons in this Pro course.Unlock all 16

HOW IT WORKSScanner, server and projects

Your codeScannerthe cameraServerthe photo albumQuality profilerules for a languageIssuesbug, vulnerability, smellQuality gatejudges the numbers
1/5

The scanner reads your project, which is identified by a project key.

sonar.projectKey=acme_shop-api

In simple words

  • Issue: one problem SonarQube found in the code.
  • Bug / vulnerability / code smell: something that will break, something attackers could use, and something that makes code hard to maintain.
  • Rating: a grade from A to E for reliability, security and maintainability.
  • New code: code changed recently, such as since the last release. Gates focus on it.
  • Project key: the unique ID of a project in SonarQube.

Big picture

SonarQube has two parts that work together, like a camera and a photo album.

  • The scanner looks at your code and takes a "photo" (it sends a report).
  • The server stores every photo, shows them on a web page, and decides pass or fail.
Your code -> Scanner (in your laptop or CI) -> report -> SonarQube server -> web page + gate result
OUTPUTterminalutf-8 · 1L

The words you will see

WordPlain meaning
ProjectOne code base that is analysed, such as shop-api
Project keyA short unique id for the project, like acme_shop-api
AnalysisOne scan. Each analysis is saved with a date
IssueOne problem found in one place in the code
RuleThe check that produced an issue, such as "do not leave empty catch blocks"
Quality profileThe list of rules switched on for a language
Quality gateThe pass or fail conditions for a project
MetricA number such as lines of code, coverage or duplication
TokenA secret key the scanner uses to log in

Types of issue

TypeQuestion it answersExample
BugWill this break?A null value is used without a check
VulnerabilityCan this be attacked?A password is written in the code
Security hotspotIs this sensitive code safe? A person must decideUse of a weak hash
Code smellWill this be hard to maintain?A function with too many branches

Newer versions also group issues by what they affect: Security, Reliability and Maintainability. The idea is the same.

Severity and ratings

Each issue has a severity such as Blocker, High, Medium or Low, so you fix the dangerous ones first.

The server also gives each area a grade from A (best) to E (worst): Reliability, Security and Maintainability. They are easy to read in a meeting.

New code and overall code

This is the most important idea in the course.

  • Overall code: everything in the project, old and new.
  • New code: only what changed recently (since a date, a version or the last release).

SonarQube lets you judge new code strictly while living with old problems for now. This approach is called Clean as You Code. Lesson 08 explains it fully.

Editions

EditionGood for
Community BuildFree. Single-branch analysis, many languages
DeveloperAdds branch and merge request analysis, more security checks
EnterpriseAdds portfolios, reports and extra languages
Data CenterHigh availability for very large setups

Features move between editions over time, so check the current comparison on the SonarQube website before you plan.

The grades in plain words

GradeReliability and SecurityMaintainability
ANo bugs (or vulnerabilities)Debt is 5% or less of the time it took to write the code
BAt least one low-impact problemDebt 6% to 10%
CAt least one medium-impact problemDebt 11% to 20%
DAt least one high-impact problemDebt 21% to 50%
EAt least one blockerDebt over 50%

The exact wording of severities changes between versions. The idea stays the same: one serious problem is enough to lower the grade.

One analysis, start to end

Follow one scan to see how the words fit together:

  1. You run the scanner and give it the project key and a token.
  2. The scanner reads the code and sends a report to the server.
  3. The server runs the rules from the project's quality profile.
  4. Each problem found becomes an issue, saved with this analysis and its date.
  5. The server works out the metrics (coverage, duplication, grades).
  6. The quality gate looks at the new-code metrics and says Passed or Failed.

Good and bad project keys

KeyVerdict
acme_shop-apiGood: company, then service
shop-apiRisky: another team may use the same name
My Project!Bad: spaces and symbols are not allowed

Common mistakes

  • Mixing up a bug and a code smell. A bug is wrong behaviour; a smell is hard-to-read code.
  • Looking only at overall numbers on an old project and feeling hopeless. Look at new code.
  • Using the same project key for two projects. They overwrite each other.

Summary

  • A scanner sends a report to the server after each analysis.
  • Issues come from rules; rules come from a quality profile; the gate decides pass or fail.
  • Always separate new code from overall code.

CHECK YOUR UNDERSTANDING

3 quick questions

  1. 1. What does the scanner send to the server?

  2. 2. What is the difference between a bug and a code smell?

  3. 3. What is Clean as You Code based on?

You've finished the free preview of SonarQube & Code Quality

Upgrade to Pro to unlock the remaining 14 lessons, every practice lab and a certificate when you finish.

See plans