1

Is there anyway to calculate the Code Metrics for a VS 2010 Ultimate solution, but to omit the Code Contracts?

Right now, my best idea is to do a calculation, dump the excel file, then reflect over the IL of each class, count the amount of lines referencing a contract, and then subtract that amount from the lines of code for that method. The problem is that it's way more work than it's worth, and I'll only be able to shave off lines-of-code, but I still have the resulting cyclomatic complexity measures, etc.) Better ideas?

Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
smartcaveman
  • 41,281
  • 29
  • 127
  • 212
  • Cyclomatic complexity tends to measure the amount of control flow you have. How do code contracts affect it, since they don't influence control flow? – Ira Baxter Mar 06 '13 at 06:24
  • @IraBaxter , e.g. `Contract.Requires(typeof(string).IsAssignableFrom(typeof(TInput)) ? !string.IsNullOrEmpty((string)(object)input) : (typeof(TInput).IsValueType || !ReferenceEquals(input,null)), "Input object must represent an actual value.");` has a cyclomatic complexity of 5. – smartcaveman Mar 06 '13 at 06:39
  • I see; your contracts are full of conditionals. OK, somebody is writing the contracts, and that takes effort to do, and effort to understand. If you think these are a valuable part of your software, why would you want to exclude them from your metrics? – Ira Baxter Mar 06 '13 at 06:43
  • Well, the example I gave you was contrived to demonstrate cyclomatic complexity. The real case is fluent extension methods where the full method body is written twice: once as a method body; and once in an `Ensures` clause. This means that my team and I can see the full source of these helper methods via intellisense and [Code Contracts Editor Extensions](http://visualstudiogallery.msdn.microsoft.com/85f0aa38-a8a8-4811-8b86-e7f0b8d8c71b). It serves a purpose, but isn't very helpful in the context of a code review. – smartcaveman Mar 06 '13 at 18:31
  • @smartcaveman You want to see the whole contract via IntelliSense (through the editor extensions)? In what world is `typeof(string).IsAssignableFrom(typeof(TIn‌​put)) ? !string.IsNullOrEmpty((string)(object)input) : (typeof(TInput).IsValueType || !ReferenceEquals(input,null))` more clear than `IsNotNullOrEmpty(input)`? :P Complexity is _never_ good, and I rather read a properly named method call than this contract one-liner which I had to copy-and-paste to Notepad++ and dissect to get it to make sense... – Daniel A.A. Pelsmaeker Mar 06 '13 at 18:40
  • @Virtlink, that's not the real use case. `Contract.Ensures(Contract.Result() == !string.IsNullOrEmpty(value))` actually is a real use case for my `StringCondition.IsNotNullOrEmpty` method. The place where I find it particularly helpful is with reflection / LINQ expressions. – smartcaveman Mar 06 '13 at 18:52

2 Answers2

1

If you're determined to do this (which, like Ira, I wouldn't particularly recommend), a fairly easy way to exclude the contracts from your metrics would be to compile without runtime contract checking enabled, then run the metrics against that compiled version.

Nicole Calinoiu
  • 20,843
  • 2
  • 44
  • 49
  • +1, Although I'm not sure if my tool will allow checking a compiled dll. See my response to Ira - and let me know if you still think its a bad idea for this case. – smartcaveman Mar 06 '13 at 18:34
1

Go to your project's properties, and on the Code Contracts tab set the Perform Runtime Contract Checking option to None. Then recompile your project and let your analyzer analyze the resulting assembly.

However, if you have complex contracts, then I'd recommend extracting them into a method, like this:

[Pure]
public void IsNotNullOrEmpty(object input)
{
    if (typeof(string).IsAssignableFrom(typeof(TInput)))
        return !string.IsNullOrEmpty((string)(object)input)
    else
        return typeof(TInput).IsValueType
            || !ReferenceEquals(input, null);
}

Contract.Requires<ArgumentException>(IsNotNullOrEmpty(input),
    "Input object must represent an actual value.");
Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
  • +1 - good answer. However, check my comment back to @IraBaxter on my post and you'll understand why it is sometimes helpful to have the more 'complex' 1-liner contract definitions. – smartcaveman Mar 06 '13 at 18:33