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.
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.
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.
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 gates that comply with the Clean as You Code approach help you benefit from the most efficient approach to delivering Clean Code. All quality gates that comply with the Clean as You Code approach ensure that:
- No new issues are introduced
- Reliability Rating for new code is not worse than A
- Security Rating for new code is not worse than A
- Maintainability Rating for new code is not worse than A
In addition, they ensure that the following criteria are met:
- All new security hotspots are reviewed
- [Configurable] New code has limited duplication
- [Configurable] New code is properly covered by tests
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.
With the Clean as You Code approach, you should always focus on new code, so we do not recommend adding conditions for overall code to your quality gate.
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.
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.
© 2008-2023, SonarSource S.A, Switzerland. Except where otherwise noted, content in this space is licensed under a Creative Commons Attribution-NonCommercial 3.0 United States License. SONARQUBE is a trademark of SonarSource SA. All other trademarks and copyrights are the property of their respective owners.