Вопрос

I have a piece of code that is similar to this:

dynamic a = new ValueHolder();
dynamic b = new ValueHolder();
dynamic c = new ValueHolder();

a.MtdActual = b.MtdActual + c.MtdActual;
a.YtdActual = b.YtdActual + c.YtdActual;
a.MtdVariance = b.MtdVariance + c.MtdVariance;

I created this for an example, the cyclomatic complexity of that piece of code according to Code Analysis is 25.

When dissasembling the code in IL spy you can see this:

object a = new ValueHolder();
object b = new ValueHolder();
object c = new ValueHolder();
if (Program.<ConsumeA>o__SiteContainer2.<>p__Site3 == null)
{
    Program.<ConsumeA>o__SiteContainer2.<>p__Site3 = CallSite<Func<CallSite, object, object, object>>.Create(Binder.SetMember(CSharpBinderFlags.None, "MtdActual", typeof(Program), new CSharpArgumentInfo[]
    {
        CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), 
        CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
    }));
}
Func<CallSite, object, object, object> arg_159_0 = Program.<ConsumeA>o__SiteContainer2.<>p__Site3.Target;
CallSite arg_159_1 = Program.<ConsumeA>o__SiteContainer2.<>p__Site3;
object arg_159_2 = a;
if (Program.<ConsumeA>o__SiteContainer2.<>p__Site4 == null)
{
    Program.<ConsumeA>o__SiteContainer2.<>p__Site4 = CallSite<Func<CallSite, object, object, object>>.Create(Binder.BinaryOperation(CSharpBinderFlags.None, ExpressionType.Add, typeof(Program), new CSharpArgumentInfo[]
    {
        CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), 
        CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
    }));
}
...

My question is clearly the if else and compiler generated logic is increasing the complexity. But how can I configure Code Analysis to not operate on this level and rather to evaluate on the simpler coded (pre compilation) stage as the first code snippet, if even possible?

Это было полезно?

Решение

the cyclomatic complexity of that piece of code according to Code Analysis is 25

I get 13 for a method containing only that code. Which version of Visual Studio are you using, and which .NET Framework version are you targeting?

My question is clearly the if else and compiler generated logic is increasing the complexity.

Yes, the compiler is injecting instructions that are increasing the complexity. This is not the only scenario in which it does so. (Use of anonymous method is another well-known scenario in which CC is "enhanced".)

But how can I configure Code Analysis to not operate on this level and rather to evaluate on the simpler coded (pre compilation) stage as the first code snippet, if even possible?

This is not possible. The calculation engine used by both Visual Studio Code Analysis and Code Metrics (which is the same beastie under the hood) analyze only the compiled IL. They never look at the source code.

If you would like the metric calculation bug addressed, you should consider upvoting the bug report at https://connect.microsoft.com/VisualStudio/feedback/details/729236/use-of-dynamic-types-makes-cyclomatic-complexity-almost-meaningless.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top