How can the cyclomatic complexity be 27 in a method with 13 event handler subscriptions?

ぐ巨炮叔叔 提交于 2019-12-04 04:49:36

Remeber that the Code Analysis is looking at the IL in your assembly, not your source code. There is nothing in the IL that natively supports lambda expressions, so they are a construct of the compiler. You can find the specifics of what is ouput here. But basically your lambda expression is turned into a private static class that is an anonymous deligate. However, rather that create an instance of the anonymous deligate every time it is referenced in code, the deligate is cached. So each time you assign a lambda expression, it does a check to see an instance of that lambda deligate has been created, if so it uses the cached deligate. That generates an if/else in the IL increasing the complexity by 2. So in this functions complexity is 1 + 2*(lambda express) = 1 + 2 *(13) = 27 which is the correct number.

The C# compiler actually generates some fairly "interesting" IL for anonymous methods, including lambdas. For each one, it creates a private field then, before assigning its value in the consuming method, it checks if the value is null, which adds an If branch to the compiled method. The code metrics tool ought to ignore this (http://social.msdn.microsoft.com/Forums/eu/vstscode/thread/8c17f569-5ee3-4d26-bf09-4ad4f9289705, https://connect.microsoft.com/VisualStudio/feedback/details/555560/method-using-many-lambda-expressions-causes-high-cyclomatic-complexity), and we can hope that it eventually will. For now, you pretty much have to ignore the problem if you feel that it is a false positive.

Best guess is that this is probably due to the statements above being converted to the event accessor format, viz

class MyClass
{
  private event EventHandler MyPrivateEvent;

  public event EventHandler MyEvent
  {
    add
    {
      MyPrivateEvent += value;
    }
    remove
    {
      MyPrivateEvent -= value;
    }
  }
}

See http://msdn.microsoft.com/en-us/magazine/cc163533.aspx and http://www.switchonthecode.com/tutorials/csharp-tutorial-event-accessors for discussions on the event accessor format.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!