Announcing Visual Studio Code Metrics!


Announcing the new Code Metrics feature for Visual Studio ‘Orcas’! Available in Visual Studio Team Developer and Team Suite, this new feature allows users to generate code metrics for projects and solutions and displays the results in the Code Metrics Results tool window:



As you can see above, this feature currently calculates five different metrics; Maintainability Index, Cyclomatic Complexity, Depth of Inheritance, Class Coupling, and Lines of Code. All metrics are averaged at the type, namespace, and assembly levels with the exception of Class Coupling. The Class Coupling metric displays the total number of distinct types referenced at the method and type levels rather than the total number of type references. This is why above, the ProgramLibrary type’s class coupling is 83 instead of 103.


Users will are able to sort the results in the window by column. For example, above the results are sorted by the Maintainability Index column. Note that the proper hierarchy is maintained after sorting.


Users can also filter the results from a particular metric for values between a specified minimum and maximum value. As you can see in below the results of filtering out any results with a Maintainability Index greater than twenty are displayed. (The Library project is still displayed because it contains the code elements within the filter.



The Maintainability Index column includes a maintainability indicator light that changes based on the maintainability of the code. Green indicates good maintainability, yellow indicates moderate maintainability, and red indicates poor maintainability. The thresholds for the maintainability indicator light and the scale of the Maintainability Index column are still being finalized. Look for more information on this in future blog posts.


Users can also export these results into Excel where they can perform their own calculations and transformations.


Finally, to generate code metrics, simply do the following:



  1. In Solution Explorer, right-click on your solution/project and choose Generate Code Metrics


Download the March CTP (either as a Virtual PC image or a self-extracting install) and tell us what you think! What do you like? What needs improvement? And if we were to add support for additional metrics in the future which ones are you most interested in seeing?


Todd King
Visual Studio Code Analysis Team

Comments (57)

  1. Sin duda, una de las herramientas que más me llamó la atención cuando la conocí era NDepend, una de las

  2. Сьогодні вийшов Visual Studio Orcas – March 2007 CTP. Зкачати його можна тут . Але це мабуть не цікаве,

  3. chris says:

    how do you derive the "manageability index"?

  4. Werner says:

    Cool!

    last year, i wrote a tool like this, using the

    CCI-Assembly from FxCop!

    i think it is the same ?

    Suggestion/metrics that i want use

    -counter: try/finally

    -counter: try/catch

    -counter: try/catch with reThrow

    -counter: tryCatch without Catch-Code

    -Counter: Usage of Assert

    -Lines of code vs. Lines of Non-Code (using CCI and pdb)

    + many idea from the (german) book: "Code-Quality-Management" (ISBN 3-898643883)

  5. Will the code metrics integrate into Team Build? Will we be able to report against them?

  6. davkean says:

    Chris: Maintainability Index is based on Halstead metrics. In future CTPs, you will see that we’ve made the number a little more friendly, with a range between 1 and 100.

    Werner: Thanks for the feedback. Obviously, it looks like users would like a lot more metrics than the 5 we provide so far.

    Grant: Not in the CTP. Is this something you would want to compare build-to-build and track over time?

  7. Ranju. V says:

    Any chance of this making it into VS 2005?

  8. John Bergman says:

    CodeRush! has had this for quite sometime.  It shows up as a little red number next to the function so you can see it while you are editting.

    Its good to see that Visual Studio is going to have Metric that we can track over time (hopefully).

  9. The FxCop team announced that the new CTP of Orcas now has code metrics . I have the VPC setup for the

  10. GoodSyntax says:

    How nice…

    Microsoft reskinned FXCop and NDepends for us.

  11. hessie says:

    That’s a really great feature!

    But I guess it is only available with managed code?

    I’d really love to do this with C++ also, as I got a huge codebase there…

  12. Kevin I says:

    Any chance that Rico Mariani’s metrics for the framework could be integrated in some way as well, to show potentially ‘fat’ functions?

    Kevin

  13. davkean says:

    Ranju: This won’t make it into Visual Studio 2005, only ‘Orcas’.

    John: Unfortunately, there is no overlay on the editor. What would you like to see around this? Perhaps something similar to the code coverage overlay?

    hessie: This is only for managed code, including C#, Visual Basic and C++/CLI.

    Kevin: That’s a really awesome idea. Expect to some of Rico’s work in some other form in the future.

  14. The tool NDepend has support for many metrics.

    http://www.ndepend.com/Metrics.aspx

    Also results can be visualized in a treemap, and sifted with a dedicated language that looks like SQL:

    SELECT METHODS FROM NAMESPACE "MyNamespace" WHERE CyclomaticComplexity > 20 OR NbLinesOfCode > 50

    A dependency structure matrix allows to browse coupling between assemblies/namespaces/classes/members

    Soon, NDepend will allow to see differences between 2 builds.

  15. Jasmine says:

    "This won’t make it into Visual Studio 2005, only ‘Orcas’."

    …then who cares? Give me something I can use. Tooting your own horn about stuff that isn’t available yet is starting to really drive me nuts (with the whole industry). Don’t announce products that aren’t ready yet!

  16. davkean says:

    Jasmine: I understand how you feel, I was a customer of Microsoft just over a year ago. However, we announce and provide you an early look at these features so that you can have an impact on them. Once the feature has been released, it’s too late to fix bugs and broken designs. The early we release these, the more time we have to react to customer feedback.

    However, stayed tuned, we will still continue to provide tips and information that you can use today in both Visual Studio 2005 and FxCop 1.35.

  17. Daniel Moth says:

    Code metrics in VS "Orcas"

  18. Scott says:

    Integrating with Team Build would be great! I think being able to do build-to-build comparisons and trending would be very helpful.

    Will there be an "extension" interface that allows us to develop our own metrics that can plug in to this framework?

  19. This is a very cool feature that’s long overdue.  I’m not sure how much I like the overlay idea though at least not on by default.

  20. Cleve Littlefield says:

    Integration into Team build is a must, so we can report and trend this over time.  Integration with VSTS work items would be cool so we can do things like relate # of bugs to cyclomatic complexity or LOC.

    Lines of code/lines of comments is important.

    For ASP.Net, lines of JS and lines of HTML.

    Would be nice if it worked for VSTS for DB to get # of SQL loc.

  21. Wayne M. says:

    So how do they get a CCM of 0?  A 1 indicates a straight line function with no branches and loops.  Why does the CCM have two decimal places?  CCM gives an integral result.  

  22. Daniel says:

    Will this be available in Orcas "Professional" (ie not just the $10K+ Team products.)?  I was disappointed that FxCop was only available integrated in Team System.  I think things like this should be seen as an important learning aid, much like intellisense.  Come to think of it, will MS be keeping the same Express/Standard/Pro/Team lineup for Orcas?

  23. Buck Hodges on Schema for the WorkspaceMapping.xml file. Michael Ruminer on TFS Event Subscription…

  24. davkean says:

    Wayne: Cyclomatic Complexity of 0 means that there is no code within a type. We have fixed the decimal places and the rollups in later builds.

  25. Sam Gentile says:

    Harry wonders if it has been a slow week. It started that way for me but its certainly not now with both

  26. John Schroedl says:

    Please add this for unmanaged C++! The whole world is not managed code folks.

  27. How do you calculate the Maintainability Index exactly? Only based on Halstead metrics as mentioned before? Or is this based on the formula of Oman (which also includes Cyclomatic Complexity, LOC, Comments)? Or another formula?

    I am very curious about this, as I am preparing a Master Thesis research to determine an indicator for maintainability by using .NET code analysis.

  28. B# .NET Blog says:

    I was just creating some screenshots on Code Metrics in Orcas when I saw the fxcop feed getting bold

  29. There really is just so much in the recently released Visual Studio March CTP that it’s frankly hard

  30. Keith Patrick says:

    "Lines of code/lines of comments is important"

    On that note, a statement count would be good to have (I use a lot of lines for single statements, so my line count is skewed)

    I’d also like to see some kind of metric corresponding to the amount of exceptions that are explicitly thrown in code (even references) but not caught anywhere..it’s been one I’ve wanted for a while (ties into XML documentation of exceptions, as well)

    Maybe some security metrics as well, such as some kind of safety profile based on number of unmanaged calls, requested permissions that go unused, etc.

  31. Visual Studio "Orcas" にCode Metrics機能がつく(今回 March 2007 CTPより提供)

  32. C’est énorme (enfin je trouve :-)) : Les fonctionnalités d’analyse de code de Visual Studio Team Edition

  33. Rob Bryce says:

    Yeah…no code metrics for unmanaged code…there went a day of wasted effort…

  34. Scott Rudy says:

    Are these metrics based on the individual languages or the generated MSIL?

  35. Steve says:

    Add two checks:

    – Dangling property

    – Fat body

    – Else slope

    Dangling property: Check specifically for properties that have Get and set methods that only do the default behavior.  We’ve thousands of non-useful spacefiller lines of code filled with dangling properties based on old coding standards.

    Fat Body: Flag loops with a fat body such as a for loop with a body longer than 50 lines.

    Else Slope: Flag methods/blocks of code that have large amounts of code in the IF block and 1 or two lines of code in the else block.  Especially, flag the methods with multiple levels of unnessary nesting like:

    void TooNest(int a)

    {

    if (a > 0)

     {

     if (a | 1)

        {

        if ((a | 3) == 3)

            {

            (more than 20 lines of code)

            }

         else

            return(a + 1);

        }

     else

        retun(5);

     }

    else

     return(a -1);

    return(a);

    }

    The main criteria is excessive nesting combined with a large ration of (number of lines in if part) / (number of lines in else part)

  36. Peter Ritchie says:

    Nice, it’s great there’s a GUI for this stuff now.  Great work.

  37. Aardvark says:

    I have been trying to use "Generate Code Metrics" with my C# project using the March CTP and it tells me "This file contains no managed code.".  That is a bit odd because it is all managed code except for some DllImport’s in my NativeMethods class.  If I use the "Run Code Analysis" option, I do get some Cyclomatic Complexity reports back so some of the analysis is working.  Any ideas?

  38. Aardvark,

    Can you post a bug report with a repro project over on http://connect.microsoft.com/VisualStudio?

    Regards

    David

  39. Jon says:

    I can relate to so many of the posts. I like the metrics but it should be incorporated into Code Analysis such that the build will fail if you are not within acceptable ranges. The problem with making it an optional tool is some developers in my group will use it, but others will not. This is a way to enforce good programming practices.

    My general opionion of the Microsoft suite of tools is that you do not provide enough functionality. It always seems like 80% of the functionality is there but your always waiting for the other 20% (ie sharepoint, ajax, continuous integration, code analysis etc)

    I’ve become frustrated with the "in the next version" mentality and will not spend my time integrating partial products or searching for the small amount of documentation that exists to write some custom processes. If you don’t include all of the necessary functionality I’ll look elsewhere.

  40. Software Metrics 是將軟體設計或規格加以量測與量化的科學,過去在 Visual Studio 2005 Team Edition for Developers 版本中僅提供了靜態程式碼分析

  41. The Visual Studio Code Analysis Team has added code metrics to Visual Studio. You can read their original

  42. Andy Bliven says:

    A more useful measure of the complexity of a class is the average complexity of each method in it.  Since this value is normalized it can be used to compare two classes to decide which is more likely to be a maintainence risk, while the simple sum cannot.  Alternatively you could define the complexity of the class to be the maximum of the complexity of all its methods, which is similarly useful.

    I suppose you’ve thought of making the metric computation an extension point so I can add my own, perhaps calculated from the primitive measures you’ve already computed (# methods, # operators, etc.)?  You’ll never get away with just this fixed set!  

    I am delighted to see this in Visual Studio, it’s a great feature.

  43. Surprise! (not the good kind) If you use FxCop or Visual Studio Static Analysis and haven't yet started

  44. As if you need another reason to download the CTP / Beta 1 of Orcas: Code Metrics

  45. If you’ve been living under a rock (or just distracted by this Silverlight thingy), you might have missed

  46. Building quality in ! – The next release of Visual Studio Team developer has some very nice features

  47. C’est énorme (enfin je trouve :-)) : Les fonctionnalités d’analyse de code de Visual Studio Team Edition

  48. While we’ve already briefly spoken about Code Metrics when we announced it , I thought I would discuss

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

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

  51. It seems to me pay day cash advance payday loan klingelt�ne f�r handys

  52. At cash advance loan texas pay day cash advance payday loan

  53. Source Monitor Custom MSBuild Task