Code metrics in Visual Studio is a tool for measuring the quality and complexity of our code. It provides us various metrics whose values validate our code. This tool comes with Visual Studio Premium and Visual Studio Ultimate editions. We can also download its extension and install that in our Visual Studio. Using this tool to validate our code is a good practice since it will not only improve how we code but also make our application maintainable and less complex. To calculate code metrics we need to select our project from Solution Explorer (in case we have more than 1 project and if we want to see the code metrics for a single project otherwise we can select calculate code metrics for the solution) and then go to the analyze option in the menu bar and choose calculate code metrics for the selected project. Please refer to the following screen shot.
After clicking we will get the following output:
As you can see we get various metrics that have different meanings. I am explaining the meaning of these metrics below:
- Maintainability Index
Maintainability index is a metric that indicates how maintainable our code is. The higher the value of this metric, the more maintainable our code will be. Its values are between 0 and 100. According to Microsoft a value above 20 will be considered as good and a value below that is considered to be bad. But I prefer we should try to maintain its high value to the maximum we can.
- Cyclomatic Complexity
This metric indicates a number of branches in the code. Its value must be lower because the number of branches indicates the complexity in the code. Its value generally increases when we have nested if statements or switch statements, so we should try to avoid if statements where we can. It can be possible by dividing our code into a number of smaller modules that results in small lines of methods that make our code look cleaner and gives lesser cyclomatic complexity.
- Depth of Inheritance
This metric is measured as a length of object inheritance hierarchy that indicates how deep our class is at the inheritance level. A lower value of this metric is preferred. A class situated too deeply in the inheritance tree will be relatively complex to develop, test and maintain. A low value of this metric also indicates that code is less reusable. So we must implement inheritance carefully by keeping in mind that our code should remain reusable and less complex also. A preferable value of this metric lies between 0 and 4.
- Class Coupling
It indicates the number of classes referenced in a class. A lower value of this metric is preferred since it also increases the complexity in our code. If we have a class that does not reference other class then its class coupling will be 0 whereas if we refer to various classes in our class (like creating complex type properties) then it will increase class coupling.
- Lines of Code
This metric indicates the number of lines of code. We should design our methods so that they must have less lines of code. A greater lines of code indicates that the specific method is doing a lot that disobeys the Single Responsibility principle. So we must try to divide our code as much as we can so that it becomes cleaner and less complex.
Note: The values of the metrics that I have indicated are general values. There is no rule that we must follow these values as a base. Every organization has their own standards. But running the code metrics tool regularly is considered to be a good practice.