Поделиться через


Team System 2008 : the sum is greater than the parts (alt: Use Code Metrics in VS 2008!)

There's a ton of great new features in VS 2008, for Web development, OBAs, data access, devices and so on, all of which will help to make developers more productive - I've blogged about some of them already and will do more in the future, but today when I was looking at the new support for Code Metrics in 2008 I was reminded of what people used to say about eXtreme Programming when it first hit the scene, viz. that it was gestalt, the sum was greater than the parts. The implication was that in order to reap the full benefits of the process you had to embrace every part of the process, from the readily acceptable (short iterations) to the downright uncomfortable-with-the-idea (pair programming). The message was, put aside your scepticism, give it a whirl and judge it on its results.

Now obviously Team System is a different animal completely, but if you look closely at some of the new features in VS 2008 you can see that there's a case for a similar argument to be made. Take Code Metrics for example. I first used Code Metrics tools about 5 years ago and I initially approached them with a level of scepticism that I could probably sum up as : "Pah! Code Metrics? I know what I'm at!" Until I saw what they could do for me, and in a nutshell that was

  • Help me simplify my code
  • Help me idenfity anti-patterns or refactoring smells
  • Help me make my code easier to test, which ultimately lead to
  • Increased code coverage

In VS 2008 Development Edition you gather code metrics as shown below. Just select either an individual project or gather them for all projects in your solution.

image

After the paint dries you end up with a results window that looks a little bit like the below, which is a snapshot of the Code Metrics for the AJAXControlToolkit library from codeplex (check out the nice green Maintainability Indices!).

image

You can drill down into types and methods / properties as shown below:

image

 

You get 5 metrics:

  1. Maintainability Index: this is a quantitative measure of the complexity of the code. A Green light here means the code should be reasonable easy to maintain (higher numbers are better).
  2. Cyclomatic Complexity: this is one I like and the one that helps you figure out how hard it will be to test your code. It's an indication of the number of possible paths through your code - high numbers here (over 25 according to the tool, I would say shoot for a smaller number than that) are not a good thing.
  3. Depth of Inheritance: Lower values are better here. I think of this one as an "understandability" index as well.
  4. Class coupling: The number of distinct types referenced by the method/property or class depending on what you're looking at.
  5. Lines of Code: Self explanatory, but useful for quickly identifying long methods.

In and of themselves the metrics are interesting...but in the context of the whole being greater than the sum of the parts, think about how you would use them. If your code coverage for example has plateau'd  (i.e. you have a lot of red areas when you view a file after gathering coverage stats), then have a look at the cyclomatic complexity for the class or method in question. The higher the number the more paths through the code and the harder for your test to exercise every line of code. Similarly if you've got high class coupling figures you may have code smells like Long Parameter Lists that you would want to refactor.

I hate the word "synergy" and there should be a swear jar for "dovetailing", so let's just say that the features complement each other nicely. In the Development Edition along, think of the "complementing" possible between unit testing / performance profiling / code metrics / static   code analysis and refactoring. All really useful in their own right, but add them together and you've got something really powerful.

 

Technorati Tags: Microsoft, Developer, Visual Studio 2008, Visual Studio Team System, Refactoring, Code Metrics, Cyclomatic Complexity, Maintainability

Cross posted from ronan's blog