Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 37 ยท Rules and Quality Gates

Rules and Quality Profiles

Learn how SonarQube rules are keyed and described, how quality profiles choose which rules run per language, how inheritance and copies work, and how to tune a profile.

SonarQube does not run "all its rules" on every project. A quality profile is a list of rules that are active for one language, and every project uses exactly one profile per language. Choosing and tuning the profile is the most direct way to decide what counts as a problem for your team.

Anatomy of a rule

A rule has a key made of the language repository and a number, such as java:S106 or python:S1481. It also has:

  • a title and description with Why is this an issue?, How can I fix it? and Resources,
  • compliant and non-compliant code examples,
  • a type or quality and a default severity or impact,
  • optional parameters (for example the cognitive complexity threshold of rule S3776, default 15),
  • tags such as security, convention or cwe,
  • a remediation effort estimate that feeds technical debt.

Rules come from analyzer plugins bundled with SonarQube (one for each language family) or from third-party plugins. Browse them under Rules in the top bar; filter by language, tag, type and quality profile.

What a quality profile is

For every language, SonarQube ships a built-in profile called Sonar way. It is a curated set of rules the vendor considers valuable and low in noise. It is read-only; you cannot edit a built-in profile directly. Instead you:

  • Copy it to create an independent profile you fully control, or
  • Extend it (inheritance): the child gets every rule of the parent and can add or override some. When the parent changes (for example after an upgrade adds rules), the child inherits the change.

Inheritance works well for organizations: a company profile extends Sonar way, and each team profile extends the company profile. A rule's activation is shown in the profile with the severity and the parameters used; a child can raise a rule's severity or change a parameter.

flowchart TD
  A["Sonar way (built in, read-only)"] --> B["Company Java<br/>adds security rules"]
  B --> C["Payments team Java<br/>tunes thresholds"]
  B --> D["Mobile team Java"]

Child profiles inherit rules from their parent.

Choosing a profile for a project

The default profile for each language is set globally (Quality Profiles, then Set as default). A project can use a different one (Project Settings > Quality Profile). Changing the profile changes the rules for the next analysis: previously reported issues from deactivated rules are closed, and issues from newly activated rules appear. Do this with care before a release: the gate for new code is based on what the current profile finds.

Python Resolve a profile with inheritance (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.

Comparing profiles

The Compare action shows which rules exist in one profile but not another, and which differ in severity or parameters. This is also how you review the effect of an upgrade. Stand-in:

Python Compare two profiles (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.

Tuning guidelines

  • Start with Sonar way. It is deliberately tuned; copy it only when you must remove or add rules.
  • Disable rules sparingly. A rule that fires 500 times because your style differs is worth disabling or changing a parameter for. A rule you merely dislike is not.
  • Add security rules deliberately. Extra rules for sensitive code (cryptography, deserialization) are usually worth it.
  • Do not edit per project. Prefer a small number of shared profiles; a profile per project hides inconsistent standards.
  • Back up and restore. You can export a profile as XML and import it elsewhere, which is how you move settings between servers.
  • Review after upgrades. New versions may add rules to Sonar way. They appear as new issues; check the Activity log of the profile for changes.

Rules you write yourself

For custom needs you can write your own rules with a plugin API (Java), or with template rules: some analyzers offer rule templates (for example "forbidden method call") where you only fill in parameters, such as a regular expression, to create a new rule. This lesson only introduces the idea; use it when a recurring review comment could be a check.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How many quality profiles does a project use per language?

    Each project uses one profile for each language it contains.
  2. 2. Why can you not edit Sonar way directly?

    Built-in profiles are managed by the vendor; you copy them or create a child profile.
  3. 3. What happens when a parent profile gains a rule and you use inheritance?

    An extending profile inherits the parent's changes; a copy would not.
  4. 4. What does a rule key like python:S1481 consist of?

    The prefix names the analyzer repository and S followed by a number names the rule.

Summary

  • A quality profile is the set of active rules for one language; each project uses one per language.
  • Sonar way is built in and read-only; copy it or extend it for your own.
  • Inheritance lets a company profile feed team profiles.
  • Change profiles deliberately: it changes what counts as an issue on the next scan.
  • Compare, export and review profiles after upgrades.

Next: The Clean Code Taxonomy