Tips, tricks and all the info about Microsoft Visual Studio 2008

Visual Studio 2008 Code Metrics – build more maintainable software

, posted: 14-Mar-2008 09:00

Cyclometric complexity, depth of inheritance… these are terms that many developers paid very little attention too once their software engineering paper at university was finished and they were baking in the final summer before heading off into the “real world”.

Now, in Visual Studio 2008, these terms are back with the new code metrics engine.

There is a reason that Cyclometric complexity and other such metrics are taught in university – they are often great indicators of just how maintainable a software project is. Before digging in deeper and looking at these metrics one by one, how can you generate these metrics?

Open a solution in Visual Studio 2008, and under the “Analyze” menu, select “Run code analysis on ”.

After a short while you will be presented with a snap in pane much like how everything in Visual Studio is presented. Resizing this pane, you should see something similar to the following:

Maintainability Index
We can see in this solution that the Designer project has a maintainability index of 80. A maintainability value is a figure between 0 and 100 where 100 is fantastic and zero means you should probably change your profession :-)

A color indicator gives an instant indication of what this values means – green is universally accepted as “good”, red as “bad”.

So far so good but is there anywhere that is causing concern? Setting the filtering to find anything below a value of 20 we get a single result. Drilling in we can find exactly where that code exists:

By double clicking the row we are taken directly to the code that is at fault and, surprise, it’s the automatically generated code that Visual Studio makes for initializing a form! While it’s not the best code in the world it’s at least reliable and it’s not something we actively have to maintain so we can safely ignore that method.

More information on how this value is calculated can be found on the Microsoft Code Analysis Team Blog here.

Cyclometric complexity
Cyclometric complexity is a well known way of calculating how complex code is. We won’t go into detail here about exactly how to calculate it yourself but I would recommend checking out Wikipedia for more information.

A lower figure for cyclometric complexity is important and testing this value is now included in FXCop, the built in static analysis tools for visual studio meaning that warnings may be generated if the cyclometric complexity of a method goes beyond a healthy level.

Depth of inheritance
This metric gives a quick indication of how many layers of code there are forming an object.

For example, if class A inherits class B then class A has a depth of 2. Understandably, once code inherits back too far it can become quite cumbersome trying to maintain that code – a change at one layer may have an impact on code elsewhere.

One thing to keep in mind with this metric however is that it examines the entire .Net framework. What I mean here is that if you inherit, for example, off System.Windows.Window, you will get a reasonably high figure immediately because of the inheritance depth in the framework.

This diagram from the Microsoft Code Analysis Team Blog shows how depth of inheritance is calculated:

Class coupling
This metric provides a figure of how many classes that a given project, class or method relies upon. It only takes account of the class itself and not the sub classes in the inheritance hierarchy. As with the depth of inheritance, less is best.

Another diagram from the Microsoft Code Analysis Team Blog helps explain class coupling:

Lines of code
Possibly one of the less useful metrics, lines of code was once looked upon as a key metric of how large a software development project was.

The truth is that the lines of code is often a misleading figure as you can have a lot of lines that do very little or very few that do a lot (and a mix of the two).

How lines of code are counted is also up for debate – should you include whitespace? What about lines that do not equate to direct code like “{“? The engine for calculating lines of code in Visual Studio is reluctant to count anything that isn’t a line of code that will evolve into executable code. This means the figure may be lower than the figure presented by other tools.

Having said all this, generally speaking, a lower figure is always better if possible – just don’t sacrifice readability to push it lower in every case.

Team based development
There is plenty of extra usefulness in the code metrics additions in Visual Studio 2008 when it comes to working with your team. The ability to export to Microsoft Excel is useful when needing to send the stats to other team members who might not be developers – project managers for example. If you use Team System you’re also in luck as you can create work items based on the metrics.

Visual Studio 2008 is a solid step forward in aiding developers in taking a more holistic approach to software development – rather than just for writing code, Visual Studio now can be used to better understand the interactions of our code and how that impacts the maintainability and robustness of our solutions.

I hope this post has helped illustrate what the metrics provided by Visual Studio mean and what value they provide to your development process. If you would like to learn more about code metrics and code analysis in Visual Studio I’d highly recommend reading the Visual Studio Code Analysis Team Blog here.

Download Visual Studio 2008 90 day trial
For detailed information and to request a free 90-day trial DVD of Visual Studio 2008 Team Suite to be sent out to you, go to the Microsoft Visual Studio webpage.

About the Author

John-Daniel Trask is a co-founder of Mindscape, a product development company based in Wellington New Zealand. He also maintains a popular blog about software development and business.

Comment by kumar, on 5-Nov-2008 07:42

How do we export the code metric results to MS Excel, considering I have got Office XP installed and not the latest version of Office.

About the Visual Studio 2008 Blog

In the years since the hugely successful release of Visual Studio 2005, Microsoft has used developer feedback from all over the world to introduce over 250 new features in the 2008 release.

This sponsored blog will bring Visual Studio 2008 tips and tricks from well known developers in the Australian and New Zealand tech community directly to you.

Make sure you bookmark this blog or subscribe to our RSS feed.

Other blog posts

The New Zealand ALM Conference...
Writing your own Html Helpers ...
Automating Visual Studio 2008...
Windows Azure Table Storage - ...
Windows Azure Table Storage - ...
XML and XSLT in Visual Studio ...
Introduction to New SQL Server...
Using the Visual Studio 2008 P...
Getting started with Windows A...
Why consider sitting for an MC...

Some recent comments

Praveen M on Visual Studio 2008 support for building SilverLigh: I installed SILVERLIGHT TOOLS for VS2008 at my work. I am able to do silverlight...

zefo on Unit testing with Visual Studio 2008: Just minor recommendation: better use *.png for the screenshots. Jpeg is more su...

gil ziny on Unit testing with Visual Studio 2008: When I associate the unit test with TFS testing policy, if fails during check-in...

lynette on Visual Studio 2008 JavaScript Debugging: i have enable in IE the ability to debug scripts. if i go to my section in my c...

Junkie on Why consider sitting for an MCTS or MCP Certificat: The way you presented the case for certification has been completely different f...

Brian Link on Unit testing with Visual Studio 2008: You seem to be incorrect in one of your opening remarks - it seems that web.conf...

Dipanwaya on Unit testing .Net Compact Framework applications w: Steps Behind the Test in the VS 2008? How to test a module in the VS 2008 Envir...

Ricardo on Visual Studio 2008 support for building SilverLigh: Yes! Found the solution myself :) Reset the environment of Visual Web Developer...

Ricardo on Visual Studio 2008 support for building SilverLigh: Have the same problem after installing Visual Web Developer 2008 SP1 and Silverl...

kjward on Automating Visual Studio 2008: thanks lots for all the great info. i'm sure the answer to my question is in th...


The Visual Studio 2008 blog is sponsored by Microsoft NZ. The blog posts are the authors' genuine accounts of their experiences with Visual Studio 2008 and are not influenced or filtered by Microsoft NZ in any way.