In simple words
- Code quality: how easy code is to read, change and trust, and how few bugs and security holes it has.
- Static analysis: checking code for problems by reading it, without running it.
- Quality gate: a pass/fail check, such as "no new bugs and at least 80% coverage on new code", that can stop a merge or release.
- SonarQube: a popular tool that runs static analysis and shows results and quality gates for your projects.
Big picture
Think of an airport security check. Every bag goes through the same scanner before it reaches the plane. Nobody argues with the scanner, and it never gets tired. A code quality gate is the same idea for code: every change is scanned automatically before it is allowed through.
SonarQube is the scanner. It reads your code and finds problems without running it.
The problem
Without an automatic check, quality depends on who reviews the code and how busy they are.
| Without a gate | With a gate |
|---|---|
| Reviewers miss things when tired or in a hurry | The same rules run on every change |
| Bugs are found in production | Bugs are found before merge |
| Quality slowly gets worse | Quality can only stay the same or get better |
| Opinions: "I think this is messy" | Facts: "3 new bugs, 40% coverage" |
What static analysis means
Static analysis reads the source code and looks for patterns that are known to be wrong. It does not start your program. That makes it fast and safe.
It can find:
- Bugs: code that will probably fail, like using a value that could be empty.
- Vulnerabilities: code that attackers could abuse, like building a database query from user input.
- Code smells: code that works but is hard to read or change, like a 600-line function.
- Duplicated code: the same block copied in many places.
- Missing tests: code that no test runs (this is called coverage).
It cannot find everything. It will not tell you that the feature is the wrong feature. It works best next to tests and human review, not instead of them.
What a quality gate is
A quality gate is a pass or fail rule made of simple conditions. For example:
- no new bugs
- no new vulnerabilities
- at least 80% of new code covered by tests
- less than 3% duplicated new code
If every condition is met, the gate is Passed. If any condition fails, the gate is Failed, and the pipeline can stop.
Where SonarQube fits
Commit -> Build -> Unit tests -> SonarQube analysis -> Quality gate -> Publish artifactSonarQube sits after the tests, because it also reads the test coverage reports. It sits before publishing, so bad code never becomes a release.
Why it works for teams
- Fast feedback. The developer sees the problem in minutes, while the code is fresh in their mind.
- Fair. The rules are written down and the same for everyone.
- Teaches. Each issue explains why it is a problem and shows how to fix it.
What each check is good at
No single check finds everything. Each one catches a different kind of problem.
| Check | Good at finding | Weak at finding |
|---|---|---|
| Static analysis (SonarQube) | Known bad patterns, security slips, untested code | Whether the feature is the right one |
| Unit tests | Wrong behaviour you thought to test | Problems nobody thought of |
| Code review | Design, naming, "does this make sense?" | Boring repeated mistakes when tired |
| Testing the running app | Problems that only appear when it runs | Code paths that tests never reach |
Use all four. SonarQube takes the boring, repeated checks off the humans, so reviews can focus on design.
A worked example
- Mia changes the login code and opens a merge request.
- The pipeline builds the code, runs the tests, then runs SonarQube.
- SonarQube finds one new bug (a file that is never closed) and sees that only 62% of the new code is covered by tests.
- The gate result appears on the merge request:
Quality gate: Failed New bugs 1 must be 0 FAILED Coverage on new code 62% must be at least 80% FAILED Duplicated lines on new code 0.8% must be under 3% OK- Mia closes the file and adds two tests. The next run passes, and the merge can go ahead.
The whole loop took minutes. Without the gate, the bug might have reached production before anyone saw it.
Common mistakes
- Treating the gate as a punishment. It is a safety net.
- Turning on every rule on day one for an old project. The team drowns in thousands of issues and stops looking. Lesson 08 shows the better way.
- Relying only on the tool and skipping code review.
Summary
- Static analysis finds bugs, vulnerabilities and smells without running the code.
- A quality gate turns the results into pass or fail.
- SonarQube runs in the pipeline after tests and before publishing.
CHECK YOUR UNDERSTANDING
3 quick questions
1. What does static analysis do?
2. What is a quality gate?
3. Where does the SonarQube step usually go in a pipeline?