Condividi tramite


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, switchcases, and do, while, foreachand forloops) 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:

 

Icon

Level

Range

Green

High Maintainability

Between 20 and 100 inclusive

Yellow

Moderate Maintainability

Between 10 and 19 inclusive

Red

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.

 

Conclusion

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

  • Anonymous
    October 03, 2007
    PingBack from http://www.artofbam.com/wordpress/?p=4844

  • Anonymous
    October 03, 2007
    Le metriche del prossimo Visual Studio 2008

  • Anonymous
    October 03, 2007
    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?

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

  • Anonymous
    October 04, 2007
    ASP.Net, C#, .Net Framework, SQL Server: Error Handling in .Net with Example Great article on error...

  • Anonymous
    October 04, 2007
    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.

  • Anonymous
    October 04, 2007
    It is time for another weekly roundup of news that focuses on .NET, agile and general development related

  • Anonymous
    October 04, 2007
    Great info! http://blog.hill-it.be

  • Anonymous
    October 05, 2007
    In Visual Studio 2005, we integrated a couple of internal static analysis tools - FxCop and PREfast into

  • Anonymous
    October 08, 2007
    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.

  • Anonymous
    October 10, 2007
    The comment has been removed

  • Anonymous
    October 10, 2007
    The comment has been removed

  • Anonymous
    October 15, 2007
    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): http://codebetter.com/blogs/patricksmacchia/archive/2007/10/03/how-do-you-count-your-number-of-lines-of-code-loc.aspx

  • Anonymous
    October 21, 2007
    In the first installment of this series I discussed an introduction to WiX . In the second installment,

  • Anonymous
    October 31, 2007
    I'm currently trying to pro-actively drive the adoption of the .NET Framework 3.5 and VS 2008 within

  • Anonymous
    November 04, 2007
    Dans deux de mes derniers messages, j'ai mis en avant deux des �E9;volutions du Profiler de Visual

  • Anonymous
    November 08, 2007
    Code Metrics in Visual Studio 2008

  • Anonymous
    November 15, 2007
    Nice

  • Anonymous
    November 17, 2007
    Hi All, The visual studio code analysis team introduces this great article of "Code Metrics"...

  • Anonymous
    November 19, 2007
    What else can I say? The big day has arrived (read the official word here on Soma's blog ) and we're

  • Anonymous
    November 19, 2007
    What else can I say? The big day has arrived (read the official word here on Soma's blog ) and we're

  • Anonymous
    November 27, 2007
    Code Metrics in Visual Studio 2008

  • Anonymous
    December 04, 2007
    Como muchos ya sabréis hace poco Microsoft lanzó al mercado Visual Studio 2008. Si estáis

  • Anonymous
    December 17, 2007
    Having inherited a lot of code in past lives as a development lead or dev manager, determining code complexity

  • Anonymous
    December 17, 2007
    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.

  • Anonymous
    December 19, 2007
    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.

  • Anonymous
    January 14, 2008
    Чем отличается Visual Studio 2008 Standard Edition от Visual Studio 2008 Professional Edition http://msdn2.microsoft.com/en-us/vs2008/products/bb980920.aspx

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

  • Anonymous
    February 05, 2008
    This is my follow-up to Rodrigo's excellent post " How to be a better developer? ", where he

  • Anonymous
    February 26, 2008
    One of the new features in Visual Studio 2008 Developer and Team Suite is code metrics.  What's

  • Anonymous
    March 05, 2008
    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

  • Anonymous
    March 09, 2008
    Como muchos ya sabréis hace poco Microsoft lanzó al mercado Visual Studio 2008. Si estáis interesados

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

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

  • Anonymous
    August 13, 2008
    Seekordne postitus on viimane Visual Studio 2008 seeriast ja toob võrdlusesse juurde Team System versioonid

  • Anonymous
    October 23, 2008
    VS 2008 предоствляет 4 метрики, которые подробно описаны в блоге Code Analysis Team: http://blogs.msdn.com/fxcop/archive/2007/10/03/new-for-visual-studio-2008-code-metrics.aspx

  • Anonymous
    November 13, 2008
    One of the challenges, I always faced was the availability of approaches for quantification of the aspects

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

  • Anonymous
    March 14, 2009
    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