Monday, March 29, 2010

Improve code quality using Visual Studio Code Metrics

Background

In my previous post, I had blogged about improving code quality using software code metrics. In this post I’ll try to demonstrate how to use Visual Studio Team System edition and the code metric feature to improve code quality.

Visual Studio Team System Code Metrics

You can open the solution or project for which the code metrics has to be analyzed. This can be done at the complete solution level or at individual project level.

menu

As can be seen from above screenshot, you can select the Analyse menu option from the menu bar. Towards the bottom of the pulled down menu you can see Calculate Code Metrics for the selected project or Calculate Code Metrics for Solution options.

I have selected the project option and the output is shown below

summary

This shows the various metrics at the project level as well as the individual namespace levels within the project. Except for Depth of Inheritance all other metric are aggregates of the values at each namespace level. This is just the beginning. What I like the most is the drilldown feature where in you can drill down to the method level by navigating through the hierarchy in the following order project –>Namespace –> Class –> Method.

drilldown

In the above screenshot I have drilled down to the method level. If you look carefully, in the Maintainability Index column you can find a green box next to the value of Maintainability Index. VSTS gives you a visual indicator based on the value of the Maintainability Index at each level. The indicator values based on the range in the following order

  • 0-10 : Red which means that the class is very hard to maintain
  • 10-20 : Orange / Amber which means that the class is somewhat maintainable
  • 20-100 : Green which indicates that the class is maintainable and easier to change

If you observe carefully, the depth of inheritance matrix is not applicable at the method or function level. As mentioned in my earlier blog post, Depth of Inheritance tells you how many classes are inherited by the current class or how many levels of inheritance is involved in implementing a particular class. Hence this metrics is not applicable at the function level.

Lines of code is pretty straightforward metric which tells how many executable lines of code is there in a method or class or namespace level.

Two of the most interesting metrics are the Class Coupling and Cyclomatic Complexity. Class Coupling tells how many unique classes are used to implement a method or function. It also indicates the aggregate of unique classes used within a class implementation at the class level. At the namespace level its the aggregate of unique classes used in all the classes and same applies at the project and solution level. This metrics can be used to split the responsibilities among  classes. If some of the classes are overloaded trying to do too many thing you might get to know using this metric.

Cyclomatic Complexity is one of the most commonly used metric to measure code quality. It indicates the number of decision points within a function or class. To be very frank this requires a dedicated blog post in itself and I might do that sometime in future. For the time being I’ll demonstrate it using a small code snippet.

I have a function which verifies if a string is a valid EAN13 type barcode based on the length of the string which is passed in as a parameter.

private bool ValidateBarcodeLength(string barCode)

        {

            if (barCode.Length == 13)

            {

                return true;

            }

 

            return false;

        }

I’ll run the code metric and monitor Cyclomatic Complexity for this function.

before

If you look at the highlighted line above it shows the Cyclomatic Complexity for this function as 2. I’ll do a small refactoring and lets see if it impacts the Cyclomatic Complexity.

 private bool ValidateBarcodeLength(string barCode)

        {

            return barCode.Length == 13;

        }

Again I run the Code Metrics once again.

after

Immediately the Cyclomatic Complexity drops down by 1. This is a very trivial example but good enough to demonstrate the feature I wanted to show.

Conclusion

One of the best thing with code metrics in Visual Studio Team System is that it integrates nicely into the IDE. You need not open separate windows.

Using these metrics can help us to improve code quality to a good extent. Its very recent that I have started using these metrics. I might soon do a  full fledged post on Cyclomatic Complexity.

Till then Happy Programming :)

1 comment:

  1. Hi Nilesh,

    The post was awsome. it makes better with the example. can you give an example for 'Maintainability Index'

    ReplyDelete

VSTS CI Build for Dockerized .net Core 2.1 multi-container app

Background This is 2nd part of the series on Continuous Deployment of Multi-Container app in Azure using VSTS and Docker . In the first part...