If we Google – “how to write good code”, we end up getting tons of articles. Over 90% of them talk about thousands of guidelines, which are never easy to follow and sometimes confusing. Most of the articles don’t talk about the measurement of code quality. Whatever we have read in our Computer Science curriculum has taken a back seat, but relax, Visual Studio has taken very good care of this and provides the complete measurement of needed parameters for our code, project, and solutions. Many developers are not familiar with this, so I thought of writing about this as a refresher for everyone. I follow SOUND for my code and code review, but still, that is not about code quality measurement.
How to run code analysis
For this, you don’t need to do anything additional, no new extension installation. It comes by default with Visual Studio and you can access it from the Analyze tab.

Code Metrics come from Analyze tab and another from solution explorer.

And we get this window with all the needed parameters and values,

So we saw these parameters and their values; now let us try to understand each of them in detail. For this article, I have used Visual Studio 2019.
Cyclomatic Complexity
Cyclomatic complexity is a software metric used to indicate the complexity of a program. It is a quantitative measure of the number of linear independent paths through a program’s source code.
- Wikipedia
Cyclomatic Complexity is calculated using the control flow diagram. It uses the following formula,
Cyclomatic Complexity (CYC) = E – N + 2
Where E and N are the number of edges and nodes, respectively in the control flow diagram for a particular program.
If it is not clear to you, no worries, this simple example of comparing two numbers will explain this,

But if you are thinking this is the simplest program and, in your assignments, you get a complex program and it will be impossible to make an equivalent control flow diagram for everything you code, you are absolutely right. Visual Studio calculates this for you and here is the output for the same example,

Important Points
A lower number of CYC is recommended for any method. There is no strict guideline but based on different blogs and software scientists' opinions, CYC can have these ranges –
- CYC < 10, is considered to be easy to understand.
- CYC between the range of 10-20 is complex but still comprehensive.
- CYC greater than 20 is considered very complex.
- CYC greater than 30 should be split into smaller methods.
The number of test cases for any method id directly proportional to Cyclomatic Complexity.
With this explanation, I hope you understand the Cyclomatic Complexity now, which was the intention of this article.
Maintainability Index
This uses a complex formula and as per MSDN,
Maintainability Index = MAX(0,(171 – 5.2 * ln(Halstead Volume) – 0.23 * (Cyclomatic Complexity) – 16.2 * ln(Lines of Code))*100 / 171)
If you don’t understand this formula, don’t worry. What we need to infer from here, I will explain in a few points,
- Its value ranges from 0 to 100.
- The higher the value of the Maintainability index, the more maintainable the code is.
- The range for them in Visual Studio is defined as follows.






Santosh NadellaPosted Aug 31, 2020, 12:00 PM
LOC matters the quality of code.
Jayesh AgrawalPosted Aug 30, 2020, 10:36 AM
Very insightful...code quality is bigger concern, sometimes we missed this in faster delivery of features and thats backbone so many technical depts.