Code Churn — A Magical Metric for Software Quality
Learn about the code churn metric and how it helps pinpoint issues and protect software quality.
Join the DZone community and get the full member experience.
Join For FreeThere are many ways of protecting the quality of the software and devising various techniques to ensure the right test plan for each software release. Based on my experience in many projects from small size to enterprise level, code churn-based analysis and testing are very effective in uncovering issues and protecting software quality. Code churn is nothing but code changes made for the software to achieve a certain functionality.
Code Churn
While designing test plans, the basic question to ask would be to find out what code changes are done in the software. There are multiple contexts to this; one is from the functional perspective and another from a code perspective.
The functional perspective would help to do black box testing. If you understand the functionality well and what has been changed in the current feature, then it would be easy to come up with an effective test plan to test the modified functionality.
However, in legacy or enterprise level software, it is not sufficient. The code written to change or add a functionality would have many side effects, like
- Direct dependent code
- Reused code
- Business rules changing, especially various code paths
In such cases, black box testing would not be enough. It is important to know the code churn (changes).
What to Look for in Code Churn
Below are a couple of important takeaways we can derive from code churn:
- Code churn size
- Type of churn
- Breadth of code churn
- Depth of code churn
Let’s briefly understand them one by one.
Code Churn Size
In this metric, we mainly need to understand the number of lines changed to achieve the functionality. It will provide information like
- If it's a completely new code (class, method).
- Whether it requires unit-level tests to ensure it is tested for various inputs, output, and associated business rules.
- What existing code was modified for specific (new or modified) business rules, data changes, etc.
- The overall size of the code as a generic factor (e.g. for a story of size 5 point, are there 100 lines of code or few, etc.); it will give a sense of criticality of the change.
Types of Churn
This is mainly to understand the type of tests to include. Some of the important factors to understand are
- Server-side change – Test cases required for application layer code and stored procedure changes.
- Is it functional or non-functional?
- Client-side change like HTML, JS, CSS, etc. — This will help us to put test cases related to UI responsiveness, multiple browser tests, accessibility tests, device tests, etc.
Breadth of Code Churn
It will help to identify a list of dependent functionalities and modules to be regressed. From a functional perspective, it might not be dependent on other modules, however, if the code is reused in other modules, then it may be required to consider them under regression tests.
If the code change is done in a utility function which is reused in multiple modules, it may be required to regress the dependent areas as well.
Depth of Code Churn
This will help testers to define what level of test cases are to be written. If the code is completely rewritten or changed on multiple levels, then unit-level, integration, and regression test cases are required. If it is just a specific area of code changed, then identify only the test cases specific to the area of the change.
Useful Tools
By looking at the above advantages of using code churn, it is easier said than done, unless we have the right tools to provide such useful information.
There are many tools we can use for such analysis, however, a few tools that we found to be very useful are listed below:
Git – code repository. We wrote a small tool that will connect to the git repository and pull the metrics for the given date period from the git repository range, like
- List of files changed
- Type of files changed based on file extensions like .vb, .cs, .js, .css, .html, etc.
- Number of lines added
- Number of lines modified
- Total code churn
N-Cover — This is a comprehensive tool to identify code coverage. We will be able to identify how much of the code is tested through tests executed and any gaps in the testing. It adds more value to figure out the gaps in testing and filling those gaps immediately.
How to Use Code Churn Data
Effective use of code churn can be made in many ways.
- The code churn report should be generated on a daily basis and should be used for BVT (Build, Verification, Test).
- Code churn should be the basis for sprint tests so that each day of code is tested on a day-to-day basis.
- The code churn report can be used for finalizing release-level test plans to identify the list of modules, components, and subcomponents to be regressed.
- Code churn should be used during group technical reviews, as well, to understand the complexity, size, and dependency of code changes.
To summarize the benefits of code churn, it is like hitting the bull’s eye instead of executing test cases blindly based on merely functional aspects. Using a code churn-based test plan, it will test the exact code that is added/modified/deleted. This way, we will not be shooting in the dark.
Opinions expressed by DZone contributors are their own.
Comments