This version of the SonarQube documentation is no longer maintained. It relates to a version of SonarQube that is not active.

See latest version
Start Free
10.4 | User guide | Clean as You Code

Clean as You Code

On this page

What is Clean as You Code?

Clean as You Code is an approach to code quality that eliminates many of the challenges that come with traditional methodologies. As a developer, you focus on maintaining high standards and taking responsibility specifically in the new code you're working on. SonarQube gives you the tools to set high standards and take pride in knowing that your code meets those standards.

Focus on new code

With Clean as You Code, your focus is always on new code (code that has been added or changed according to your new code definition) and making sure the code you write today is clean and safe.

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.

Understanding the benefits of Clean as You Code

To get a visual representation of the benefits of Clean as You Code based on the data in your projects, navigate to More > Clean as You Code in SonarQube.

Personal responsibility

With Clean as You Code, you aren't responsible for anyone else's code. You own the quality and security of the new code you are working on today. If you add new issues, SonarQube automatically assigns them to you so you can maintain the quality of your code.

For more information on issues and how they are assigned, check out the Issues page.

Quality gate

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.

What conditions are required for your quality gate to implement the Clean as You Code methodology?

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 Fix Quality Gate button if your quality gate is not compliant.

For more information, see Quality gates.

Pull request analysis

You can use pull request analysis and pull request decoration to make sure that your code meets your standards before merging. Pull request analysis lets you see your pull request's quality gate in the SonarQube UI. You can then decorate your pull requests with SonarQube issues directly in your DevOps platform's interface.

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

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.


Was this page helpful?

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

Creative Commons License