Domanda

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?

È stato utile?

Soluzione

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.

Altri suggerimenti

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.");
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top