4

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?

AakashM
  • 62,551
  • 17
  • 151
  • 186
Andre
  • 3,717
  • 4
  • 25
  • 29
  • 1
    This is inevitable, the analyzer works from the IL, not the source code. And using *dynamic* tends to generate a wholeheckofalot of calls to the C# binder. You'd need a different kind of analyzer. – Hans Passant Apr 24 '12 at 12:19

1 Answers1

4

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.

Nicole Calinoiu
  • 20,843
  • 2
  • 44
  • 49
  • As for the statement that it creates cyclomatic complexity of 25 is not important imo, so my actual code i was using had a few more statements and it exceeded 25, the point is not using dynamic on that same code results in a metric of 1, but nonetheless this answered my question thanks – Andre Apr 24 '12 at 13:47
  • @Andre: The exact CC value is important only because this problem reflects a bug in the metric calculation. Any change in the value between VS versions is an indication of a potential attempt to address the bug. A different but still incorrect value would potentially indicate that an attempt to fix the bug did not succeed as expected. – Nicole Calinoiu Apr 26 '12 at 13:22