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

Visual Studio 2008 Code Metrics Saves you Money

, posted: 27-Mar-2008 09:00

One of many new features available in Visual Studio 2008 Team Developer and Team Suite editions is Code Metrics (CM).

Code Metrics measures industry-standard software characteristics. Covering a range of aspects, it can provide useful and quantifiable feedback for developers.

Displaying code metrics is very easy; simply right click on a project or a solution in the Solution Explorer, and select Calculate Code Metrics. Here's what the results look like:

The results window displays each of five different metrics: Cyclomatic Complexity, Depth of Inheritance, Class Coupling, Lines of Code, and an overall Maintainability Index. By expanding the tree on the left, you can drill right down from a whole project to individual methods within a class. But what do all the numbers mean?

Cyclomatic Complexity
Cyclomatic Complexity is the total number of linearly independent paths that can be taken when executing a method, based on the number of 'if', 'switch', 'while' and other similar branching statements in it. If a method has no such statements in it, there is only one path that can be followed, and so the Cyclomatic Complexity is one.

In this example, there are three realisable paths that could be followed when this method is executed, depending on the value of X:
  • The path that is followed when X is 1
  • The path that is followed when X is 2
  • The path that is followed when X isn't 1 or 2

Cyclomatic Complexity tells you two important things. First, it indicates the general complexity of a method. Lower is better; if the number is high, the method is probably trying to cover too many different situations (i.e. trying to be too clever) and needs to be broken up into simpler, specialised methods that cater specifically to individual situations (and are much easier to maintain).

Secondly, in order to ensure that every scenario has been tested properly, you must create a unique test case for each path. Cyclomatic Complexity tells you the total number of test cases you need to write, to ensure that all possible situations have been covered.

If Cyclomatic Complexity is higher than the number of unit tests for a piece of code, you will know that some scenarios are not being accounted for, and may be harbouring bugs.

Depth of Inheritance
Depth of Inheritance is the total number of levels of inheritance in a class hierarchy, starting at one (remember everything inherits from Object). This example has a depth of inheritance of three:

Lower is better; if Depth of Inheritance goes higher than three or four, your code may be over-engineered and prove difficult to maintain.

The higher the number, the more subclasses you will have to modify -- costing time and money, and potentially introducing bugs -- if you make a breaking change to a base class.

If you consider that every change you make to those subclasses may introduce potential bugs with all of the objects they interact with... well, you get my point.

Class Coupling
Class Coupling counts the total number of distinct types a class interacts with, excluding primitive and built-in types like Int32 and String. Lower is better; the higher the number, the more "tied-down" a class is, with more dependencies on the rest of its immediate environment.

As a class's coupling increases, the harder it becomes to 'rip out' and use in another project. In short, the higher a class's coupling, the less reusable it is.

We've all seen examples of a class that should be reusable, in theory, but in practice, proves to be highly project-specific and has to be more-or-less completely re-written each time it is "ported" to another project.
If this class had been better designed when it was originally created, we could have saved a lot of time later on.

Lines of Code
Lines of Code (LOC) measures the size of a piece of software by counting the total number of executable lines of code in it. As with the other metrics, lower is generally better, simply because you end up with less source code to wrestle with. Bear in mind though, there are a few caveats you should be aware of before using this metric:

  • Not everyline is equal. Some lines are very simple, and others are very complicated and take hours to figure out.
  • It doesn't count code that has been deleted and replaced. 
  • Sometimes splitting things up onto multiple lines makes them easier to comprehend; in this case increasing the LOC improves maintainability.
  • Unless well-designed, thoroughly-tested, and production-ready code flows directly from your fingers every time you sit down at your keyboard, LOC doesn't accurately reflect the amount of effort that has been put into a piece of software.

Use this metric wisely. In the past, LOC has been famously abused to measure daily programming productivity, with disastrous results. Remember there's a lot more to software development than just typing code!

Maintainability Index
Last, but not least, is the Maintainability Index, which goes from 0 to 100 and indicates the overall maintainability of a class, member, namespace or project. It's effectively an aggregate of the other metrics, but it also includes a few extra bits like the Halstead Volume, which measures the total length and vocabulary size of the program. For a change, higher is better here.

The Maintainability Index also provides a red/yellow/green traffic light icon which allows you to quickly see problem areas at a glance. The traffic light turns yellow when the number dips below 20, and red below 10, at which point you should be running for the hills.

Visual Studio 2008's Code Metrics doesn't measure performance or security, and won't help you resolve any bugs. It will, however, help you to identify potential problem areas in your software, as you are developing it.

If you address these issues early, you will save time and money that would have to be spent re-factoring unmaintainable code later on. You will also feel good about yourself knowing that you got it right the first time!

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
Richard "Dingo" Dingwall is a rock star .NET developer in Provoke's Bespoke Solutions team. Heralding from the South Island of New Zealand, Richard's lateral thought, problem solving strengths and his eye for detail puts him in the elite of Microsoft software engineers in the country.

Other related posts:
The New Zealand ALM Conference 2011 (Application Life Cycle Management)
Writing your own Html Helpers for the ASP.NET MVC Framework
Automating Visual Studio 2008

Comment by James Heires, on 4-May-2008 05:07

Nice review of CM! I was impressed by the amount of information that CM makes available, but disappointed by the inability to measure LOC deleted and replaced. In addition, the ability to compare two versions is immeasurably intuitive, but alas, not mentioned here either. I can only assume that this comparison feature is also not supported by CM. Studying the number of added, deleted, modified and unmodified LOC can tell you a lot about a body of code over its lifetime. The author correctly warns us not to use LOC by itself, but rather as a component of a more complete code metrics strategy. I find LOC to be a very useful normalization factor when used with other measures (e.g., defects / LOC), in order to better understand your software development process. Keep the good articles coming! Regards, James T. Heires, PMP President James Heires Consulting, Inc. Home of EZ-Metrix code counting utility

Comment by ejes, on 13-May-2009 06:44

just to note in your example for x=1; x=2; or x=anything but 1 or 2; the three paths are actually 2+1 paths The path that is followed when X is 1 The path that is followed when X is 2 The path that is followed always. unless your if statements read: if (x==1) { // code that is executed when x=1 return; // this line is missing, otherwise it // will continue to execute. }

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.