New for Visual Studio 2008 – Code Metrics

While we’ve already briefly spoken about Code Metrics when we announced it, I thought I would discuss it in a little more depth, in particular the metrics it provides.

As mentioned previously, Code Metrics is a new tool window that helps users find and act upon complex and unmaintainable areas within an application.

The following shows the results of running Code Metrics over a fictional business application:

Code Metrics Results tool window

As you can see from above, for Visual Studio 2008, we’re providing five metrics. These are detailed below.

Class Coupling

At each level, this indicates the total number of dependencies that the item has on other types. This number excludes primitive and built-in types such as Int32, String and Object. The higher this number, the more likely changes in other types will ripple though this item. A lower value at the type level can indicate candidates for possible reuse.

The following shows how coupling is calculated:

Class Coupling

For example, as you can see above, Account is coupled to two other types, Address and Order, whereas Country is not dependent on any other type.

Depth of Inheritance

At the type level, depth of inheritance indicates the number of types that are above the type in the inheritance tree. For example, a type that derives directly from Object would have a depth of inheritance of 1. At the namespace and project level, this indicates the highest depth of inheritance of all the types contained within it. This number does not take into consideration the depth of any implemented interfaces. Deep inheritance trees can indicate an over-engineering of a problem and can increase the complexity of testing and maintaining an application.

The following shows how depth is calculated:

Depth Of Inheritance 

For example, in the above inheritance hierarchy, ListControl and Label have a depth of inheritance of 3, whereas Component has a depth of inheritance of 1.

Cyclomatic Complexity

At each level, this measures the total number of individual paths through the code. This is basically calculated by counting the number of decision points (such as if blocks, switch cases, and do, while, foreach and for loops) and adding 1. This number is also a good indication on the number of unit tests it will take to achieve full line coverage. Lower is typically better.

The following shows how complexity is calculated:

 Cyclomatic Complexity

Lines of Code

At each level, this is a measure of the total number of executable lines of code. This excludes white space, comments, braces and the declarations of members, types and namespaces themselves. Lower is typically better.

The following shows how the lines are calculated:

 Lines of Code

Maintainability Index

At the member and type level, this is an index from 0 to 100 indicating the overall maintainability of the member or type. At the namespace and assembly level, this is an average of the maintainability index of all types contained within it. This index is based on several other metrics, including Halstead Volume (which factors in the number and use of operands and operators), Cyclomatic Complexity and Lines of Code. A low number indicates code that is complex and hard to maintain.

The Maintainability Index column also includes a icon that gives a quick indication as to the overall maintainability and complexity of a particular item. The following table shows the range at which an icon is shown:






High Maintainability

Between 20 and 100 inclusive


Moderate Maintainability

Between 10 and 19 inclusive


Low Maintainability

Between 0 and 9 inclusive


These icons allow you to see at a glance any trouble spots that you should start to focusing on or filling bugs against.



Now that you know what these metrics measure, and how they are calculated, the next thing you’ll want to know is what you should do when you start to see some red in your application. In a future post, I will cover why should should care about each metric, and how to go about fixing some of the common issues that cause particular metrics to high (or low in the case of Maintainability Index).

Comments (39)

  1. Le metriche del prossimo Visual Studio 2008

  2. Joe says:

    Just out of curiosity, why did you guys opt for a higher is better system for the maintainability index?  Considering that all on all the other indexes, lower is better?  Is there more information on how exactly this is calculated – do extraneous comments count against me, is an inline assignment less visual noise and therefore more maintainable than one split across two lines, etc?

  3. Eric says:

    Will there be a TFS policy plugin (powertoy or otherwise) that can check/enforce metrics (such as a max cyclomatic complexity), or anything similar?

  4. MickeyHeadShot Mickey Gousset says:

    ASP.Net, C#, .Net Framework, SQL Server: Error Handling in .Net with Example Great article on error…

  5. Derek says:

    Will this be available as a standalone application ala FxCop? This seems like something that would be nice to run during a build process, with output translated to HTML for viewing.

  6. It is time for another weekly roundup of news that focuses on .NET, agile and general development related

  7. In Visual Studio 2005, we integrated a couple of internal static analysis tools – FxCop and PREfast into

  8. Skip Valentine says:

    Is there any way to disable the metrics for a particular method? Some of my WinForms.InitializeComponent methods in my solution are showing a low Maintainability Index, and I have some other methods (i.e. setting up data relations in my Data Access Layer) that I would like to have ignored in the type/namespace level averages when I sort the list by the Maintainability Index.

    Aside from that little nuance, this looks really cool. It was nice and quick on my 30+ project solution.

  9. The Teams WIT Tools blog on TFS Power Tools Release – Work Item Templates – Introduction. Willy-Peter…

  10. davkean says:

    Joe: We (and our UE team) felt that because Maintainability Index was a normalized index from 0 to 100 (and felt like a percentage), that it was easier for a new user to understand that 100 was good, and 0 was bad. We don’t have any more information available on how this is calculated, however, because I’ve been asked this a lot recently, I plan to cover this in a future post.

    Eric and Derek: For Orcas RTM there will not be a standalone application, command-line or API for generating or enforcing the metrics (although Code Analysis does come with some new rules that enforce this). This is something we are looking at for a future version, or if we have time, a powertoy.

    Skip: Nothing in Orcas will allow you to do this – however, thanks for the suggestion, I’ll add it to the list. Glad to hear that you had good experience other than that.

  11. I would be curious to know how you compute exactly the LOC metric.

    In this post I describe how we’re doing in the NDepend tool to count the LOC (basically we just count the number of sequence points in the PDB minus the open/close brackets sequence points):

  12. In the first installment of this series I discussed an introduction to WiX . In the second installment,

  13. I'm currently trying to pro-actively drive the adoption of the .NET Framework 3.5 and VS 2008 within

  14. Dans deux de mes derniers messages, j’ai mis en avant deux des �E9;volutions du Profiler de Visual

  15. Code Metrics in Visual Studio 2008

  16. Crash Course says:

    Hi All, The visual studio code analysis team introduces this great article of "Code Metrics"…

  17. What else can I say? The big day has arrived (read the official word here on Soma’s blog ) and we’re

  18. What else can I say? The big day has arrived (read the official word here on Soma’s blog ) and we’re

  19. Code Metrics in Visual Studio 2008

  20. Como muchos ya sabréis hace poco Microsoft lanzó al mercado Visual Studio 2008. Si estáis

  21. Having inherited a lot of code in past lives as a development lead or dev manager, determining code complexity

  22. It’s so disappointing that this isn’t included in the professional edition. In fact, it seems to me like it would be in Microsoft’s best interest to have this in all editions.

  23. Duane Fields says:

    If you don’t have Team System, but would still like to take advantage of Code Metrics, you might want to check out StudioTools from Exact Magic Software. It’s  a free plugin for Visual Studio 2005 and Visual Studio 2008 that gives you many of the same capabilities.

  24. Чем отличается Visual Studio 2008 Standard Edition от Visual Studio 2008 Professional Edition

  25. Roman says:

    La semana pasada me tocó dar una plática de VSTS 2008 , como parte del entrenamiento que estamos

  26. This is my follow-up to Rodrigo’s excellent post " How to be a better developer? ", where he

  27. ASPInsiders says:

    One of the new features in Visual Studio 2008 Developer and Team Suite is code metrics.  What’s

  28. With more than 250 new features, Visual Studio 2008 includes significant enhancements in every edition, including Visual Studio Express and Visual Studio Team System. In this post, I would like to draw your attention to this new feature in Visual Studio

  29. Como muchos ya sabréis hace poco Microsoft lanzó al mercado Visual Studio 2008. Si estáis interesados

  30. .NETicated says:

    What a weekend: 52 hours, 732 miles, 3 presentations, and one great time! Yes, despite the distance,

  31. Что нового…. По сообщениям корпорации AMD, новейший инструментарий разработчика от Microsoft —

  32. Seekordne postitus on viimane Visual Studio 2008 seeriast ja toob võrdlusesse juurde Team System versioonid

  33. VS 2008 предоствляет 4 метрики, которые подробно описаны в блоге Code Analysis Team:

  34. One of the challenges, I always faced was the availability of approaches for quantification of the aspects

  35. [ Nacsa Sándor , 2009. január 19. – február 5.] Ez a Team System változat fejlett eszközrendszert kínál

  36. Voici un article bien détaillé expliquant le fonctionnement de Code Metrics de Visual Studio 2008. Et leur explication de leur calcul de l’indice de maintanibilité basée sur la formule d’Halstead