Start Free
Latest | User guide | Clean as You Code

Was this page helpful?

Clean as You Code

On this page

What is Clean as You Code?

SonarQube is designed to ensure high code quality and security by helping you, the developer, make sure that every code change that you commit to your project is free of issues. By always committing clean code, you incrementally improve the overall quality of your project. We call this methodology Clean as You Code.

Focus on new code

The core idea of Clean as You Code is that you focus your attention and effort on new code. As you work on features and improvements, SonarQube analyzes your code on each new commit and alerts you to any code quality problems and security issues. You can then address these issues right away and ensure that all new code added to the project is always clean.

The traditional approach to code quality usually emphasizes a separate stage of review at the end of each development cycle where quality issues are found and addressed. Having a separate stage in the process, often conducted by a different team, can lead to problems because the amount of technical debt accumulated can become unmanageable and the splitting of responsibility means quality issues can fall through the cracks.

Clean as You Code avoids these pitfalls. It clarifies that code quality and code security are the responsibility of you as the developer. It also keeps the maintaining of code quality manageable by making it part of the daily cadence of your work.

What about old code?

If you adopt Clean as You Code from the start of a project, you should never face the problem of having to remediate problems in old code; SonarQube makes sure that the code is clean with each commit.

If you have an existing project and start using SonarQube with it, you may find that initially, SonarQube finds a lot of quality issues in your existing old code. You don't need to start remediating this technical debt immediately. 

In any software project, a certain percentage of old code is touched with each new feature and improvement added. When this happens in a project analyzed by SonarQube, issues in that old code are highlighted and fixed as part of the normal evolution of the code. Eventually, the entire codebase gets cleaned up without having to dig through the old code. This approach is more sustainable because it is part of your everyday work and does not require a separate, special effort of remediation.

Developer ownership

The traditional approach to code quality suffers from an ownership problem. Responsibility for code quality is often split. A separate QA team identifies the issues late in the process and then attempts to get the original developers to return their attention to the problematic code and address the issues. 

In contrast, the Clean as You Code methodology places the ownership of code quality squarely with the individual developer who wrote the code in the first place. We believe that only you and your fellow developers can have a sustainable impact on code quality and security.

How SonarQube promotes developer ownership

SonarQube identifies issues as soon as possible in the development cycle: 

  • sometimes before the code is even committed (if you are using SonarLint)
  • often at the pull requests stage
  • at the latest in recently merged new code

SonarQube also uses repository information to tag each issue with the name of the developer who last touched that line of code.

This makes it easy to identify who is responsible for which issues. For more information on issues and how they are assigned, check out the Issues page.

Further, SonarQube provides detailed guidance on the details of each issue and how to remedy it. Developers have reported that as they use SonarQube, the educational aspect of the tool becomes more and more evident. This leads to a virtuous cycle where code quality increases as a developer's skills and knowledge increase.

How SonarQube helps

SonarQube helps you focus on new code by building the concept right into the product.

SonarLint catches issues early

We recommend that you use the SonarLint IDE extension together with SonarQube. It works like a spell-checker to provide in-IDE code analysis, alerting you to problems before you even commit any code. This provides the first level of defense to keep the code that you ultimately merge into your main branch clean.

Pull requests are always new code

Code changes made as part of a pull request are always considered new code. A major emphasis is placed on identifying such issues and surfacing them to the developer, not just through the SonarQube interface itself but also through the pull request decoration in the interface of your code repository platform (GitHub, etc.), where you are most likely to notice them. In this way, SonarQube encourages the fixing of code quality and security issues even before the code is merged into the main branch of the project.

For more information on setting up pull request analysis and pull request decoration, see the documentation on Pull request analysis.

New code definition

Apart from pull requests where all code changes are considered new code, SonarQube also encourages you to specify a new code definition for other contexts within their project. The details of this definition depend on your project, but the aim is to encompass code changes that are part of the current phase in development. For example:

  • For projects that have explicit versioning of releases, the new code definition can be set as "everything that has changed since the last version bump". 
  • For projects that work on the principle of continuous delivery (i.e., they have no versions) other criteria are available to define new code.

The new code definition can be set at different levels (global, project, and, starting in Developer Edition, at the branch level). Depending on the level at which your new code definition is set, you can change the starting point to fit your situation.

For more information on setting your new code definition, check out Defining new code.

Focus on new code in the UI

All views that display found issues or other code metrics always prioritize the status of new code. Data on overall code is available but is not displayed by default. This encourages you to maintain focus on the new code.

Quality gates

Your quality gate is a set of conditions that tells you whether or not your project is ready for release. With the Clean as You Code approach, your Quality gate should:

  • Focus on new code metrics – When your quality gate is set to focus on new code metrics (like the built-in Sonar way quality gate), new features will be delivered cleanly. As long as your quality gate is green, your releases will continue to improve.
  • Set and enforce high standards – When standards are set and enforced on new code, you aren't worried about having to meet those standards in old code and having to clean up someone else's code. You can take pride in meeting high standards in your code. If a project doesn't meet these high standards, it won't pass the quality gate, and is therefore not ready to be released.
  • Be a reliable measure of code quality -  When you consistently have a passing quality gate, you have a clear indication that developers can maintain high standards on all new code.

For more information on quality gates and to make sure your quality gate is enforcing your standards, check out the Quality gates page.

Quality gate conditions

We recommend configuring all your quality gates to make them Clean as You Code ready. You can do this by configuring your quality gate conditions for new code. This helps you ensure that you're not introducing issues in your code.

A quality gate is configured for Clean as You Code when it has the following conditions on new code:

  • No issues are introduced (the quality gate fails when the Number of issues is higher than 0)

OR

  • Reliability Rating is not worse than A
  • Security Rating is not worse than A
  • Maintainability Rating is not worse than A

In addition, it ensures that:

  • Security Hotspots Reviewed is not less than 100%, meaning that all the security hotspots are reviewed.
  • [Configurable] There is a limited amount of Duplicated Lines.
  • [Configurable] Coverage of code by tests is sufficient.

The duplication and test coverage conditions are configurable to your specifications. The other conditions are locked and cannot be edited if you wish to remain compliant with Clean as You Code. 

To ensure that your new code is always clean, we recommend including the "No new issues are introduced" condition in your quality gate. You can do this by clicking the Review and Optimize Quality Gate button if your quality gate is already compliant, or the Review and Update Quality Gate button if your quality gate is not compliant.

For more information, see Quality gates.

Potential drawbacks of stricter quality gates

The Clean as You Code Quality gate is designed to achieve clean increments of code with the least amount of friction in the development process. Adding more conditions may lead to bottlenecks in the pace of development with minimal benefit. You also run the risk of an ignored quality gate because frequent failures may cause a debate on which conditions to prioritize.  

It is also important to note that adding conditions on overall code will shift your focus away from new code to old code thus moving you away from the Clean as You Code methodology. This makes it hard for developers to take ownership of their own code as they have to worry about older code.

Frequently asked questions (FAQ)

General

What is ‘Clean as You Code’ (CaYC)?

The Clean as You Code methodology is a software development practice that requires each addition or modification of code to comply with code quality standards. By always committing clean code, you incrementally improve the quality of your code base.

I currently run dedicated sprints to address technical debt. What are the drawbacks of this approach? 

  • The team needs to plan dedicated time for such sprints, delaying new feature development.
  • As debt-addressing sprints don’t happen frequently enough, team is slowed down by the debt introduced during recent feature sprints. 
  • Developers are fixing old issues introduced by someone else. The ownership is low and complexity to fix is high.
  • Such sprints are often a burden for developers lowering their morale.
  • Developers are not motivated to deliver clean code outside of such debt-addressing sprints

Which issues are prioritized in the Clean as You Code methodology? Why? 

With the Clean as you Code methodology, you focus on issues in the code you wrote or changed recently (we call it new code). Those issues are easy to fix as the context is fresh in memory and it's appealing to deliver the feature with no bugs. 

Note that when changing existing code, some old issues might pop up. Those are prioritized too, as that way you incrementally with little hassle clean the whole code base.

How is Clean as You Code different from Clean Code? 

Clean Code refers to the state of a code base. Clean as You code is the methodology that helps developers reach the Clean Code state most efficiently. 

How to define the quality standard? 

With a quality profile, you can choose which rules should be enforced in your team/organization. Coverage and duplication thresholds can be set with a quality gate. Together, these comprise the quality standard.

Is Clean as You Code enough in all scenarios? 

In some cases, you would need additional measures, e.g. to ensure the quality of the overall code base or to perform refactoring. Clean as You Code does not hinder you from pursuing these additional use cases.

The quality of the overall code is important to us. What should we do? 

By practicing CaYC, you are ultimately improving your overall code quality. With CaYC, you will also clean parts of the old code you are changing. Thus cleaning the overall code over time.

Along with that, you can enable quality gate conditions on the overall code to enforce some overall quality standards.

Setting up for CaYC

How to set up my project and processes to practice CaYC? 

    • Set up a new code definition for your project (e.g. previous version)
    • Use quality gates configured for Clean as You Code 
    • Set up your CI to run frequent branch scans (daily or on every commit)
    • Set up pull request scan through DevOps integration (when available)
    • Install and use SonarLint in your IDE 

How can I make the transition to CaYC practice smooth? 

    • Start practicing CaYC at the beginning of a release cycle, so that you start with no issues in the new code.
    • Try to fix issues in new code as soon as possible.
    • Ensure frequent scans of the project, so that your quality gate status is up to date.

What is new code? How to define new code?

The Clean as you code methodology suggests focusing on recent code changes, this is called new code. This includes both added code and changed code.

New code can be defined to suit your development context. The most common options are:

  • based on the project’s releases: all unreleased code is considered recent
  • based on a number of days: all code not older than X days is considered recent.

For pull requests, all new or changed lines are considered new code.

Which quality gate and quality profile should I use?

The best way to start is to rely on the built-in ‘Sonar way’ quality gate and built-in ‘Sonar way’ quality profiles (there is one for each language). You can always create custom ones if you need additional quality gate conditions or more/other rules activated.

© 2008-2024 SonarSource SA. All rights reserved. SONAR, SONARSOURCE, SONARLINT, SONARQUBE, SONARCLOUD, and CLEAN AS YOU CODE are trademarks of SonarSource SA.

Creative Commons License