Welcome to MSDN Blogs Sign in | Join | Help

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:

 

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).
Published Wednesday, October 03, 2007 7:00 AM by David M. Kean

Comments

Wednesday, October 03, 2007 10:22 AM by Techy News Blog » New for Visual Studio 2008 - Code Metrics

# Techy News Blog » New for Visual Studio 2008 - Code Metrics

Wednesday, October 03, 2007 1:04 PM by Technology Experience (Reborn)

# Le metriche del prossimo Visual Studio 2008

Le metriche del prossimo Visual Studio 2008

Wednesday, October 03, 2007 2:49 PM by Joe

# re: New for Visual Studio 2008 - Code Metrics

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?

Wednesday, October 03, 2007 9:35 PM by Eric

# re: New for Visual Studio 2008 - Code Metrics

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

Thursday, October 04, 2007 10:15 AM by MickeyHeadShot Mickey Gousset

# My Latest Del.icio.us Links - 10/04/2007

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

Thursday, October 04, 2007 9:53 PM by Derek

# re: New for Visual Studio 2008 - Code Metrics

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.

Friday, October 05, 2007 1:09 AM by From the software development trenches

# Izindaba #12

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

Friday, October 05, 2007 7:36 AM by Adventures in .net and other Microsoft worlds

# Visual Studio 2008 - Code Metrics and .net source code

Friday, October 05, 2007 1:33 PM by Somasegar's WebLog

# Code Analysis Features in VS 2008

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

Monday, October 08, 2007 5:41 PM by Skip Valentine

# re: New for Visual Studio 2008 - Code Metrics

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.

Wednesday, October 10, 2007 10:32 AM by Team System News

# VSTS Links - 10/10/2007

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

Wednesday, October 10, 2007 7:35 PM by David M. Kean

# re: New for Visual Studio 2008 - Code Metrics

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.

Monday, October 15, 2007 4:18 PM by Patrick Smacchia

# re: New for Visual Studio 2008 - Code Metrics

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

Sunday, October 21, 2007 8:15 PM by John Robbins' Blog

# WiX: A Better TALLOW – PARAFFIN

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

Wednesday, October 31, 2007 6:15 PM by From the software development trenches

# Getting .NET Framework 3.5 and VS2008 Adopted

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

Monday, November 05, 2007 3:00 AM by Azra [Florent Santin]

# [VS 2008] Récapitulatif des nouveautés de la version Team Dev

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

Thursday, November 08, 2007 2:17 PM by Vitaly's WebLog

# Code Metrics in Visual Studio 2008

Code Metrics in Visual Studio 2008

Thursday, November 15, 2007 5:28 PM by Martinos

# re: New for Visual Studio 2008 - Code Metrics

Nice

Sunday, November 18, 2007 6:34 AM by Crash Course

# New for Visual Studio 2008 - Code Metrics

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

Monday, November 19, 2007 12:15 PM by Jeff Beehler's Blog

# Visual Studio Team System 2008 ships!

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

Monday, November 19, 2007 12:19 PM by Jeff Beehler's Blog

# Visual Studio Team System 2008 ships!

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

Tuesday, November 27, 2007 11:34 AM by Vitaly's WebLog

# Code Metrics in Visual Studio 2008

Code Metrics in Visual Studio 2008

Tuesday, December 04, 2007 4:02 PM by Yo sólo pasaba por aquí pero ya que estoy....

# Visual Studio Team System 2008

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

Monday, December 17, 2007 4:48 PM by Are you thinking what I'm thinking?

# Determining Code Complexity

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

Tuesday, December 18, 2007 6:29 AM by Ben Richardson

# re: New for Visual Studio 2008 - Code Metrics

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.

Thursday, December 20, 2007 12:32 AM by Duane Fields

# Code Metrics for non Team System users

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.

Tuesday, January 15, 2008 1:48 AM by Віктор Шатохін [MSFT]

# Полезные ссылки 15.01.2007

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

Saturday, February 02, 2008 2:09 AM by Roman

# Code analysis and Code metrics

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

Tuesday, February 05, 2008 4:49 PM by Caio Proiete on ASP .NET

# How to be a better developer?

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

Tuesday, February 26, 2008 5:43 PM by ASPInsiders

# Visual Studio 2008 - Code Metrics

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

Thursday, March 06, 2008 4:24 AM by Infosys | Microsoft

# Calculate Code Metrics feature in Visual Studio 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

Sunday, March 09, 2008 5:45 PM by cross-posting de geeks.ms

# Novedades en Visual Studio Team System 2008

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

Wednesday, April 09, 2008 10:09 PM by .NETicated

# Boston Code Camp Wrap Up

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

Thursday, May 22, 2008 3:39 PM by Русская версия

# Visual Studio 2008 - Полное описание

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

# Visual Studio 2008 Seeria (3): Professional vs Team

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

Friday, October 24, 2008 5:43 AM by Дмитрий Лапшин

# Измерение качества кода с помощью Visual Studio 2008

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

Thursday, November 13, 2008 2:26 PM by Babu George's Blog

# Code Metrics

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

Thursday, February 05, 2009 12:33 PM by Termékinformációk fejlesztőknek

# Visual Studio Team System 2008 Development Edition with MSDN Premium Subscription

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

Saturday, March 14, 2009 1:47 PM by Gabriel Mongeon

# Métriques de VS2008

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

New Comments to this post are disabled
 
Page view tracker