Domanda

I have code that performs a mail merge type operation, and I am using dynamic/ExpandoObject to assemble the properties. I am doing this instead of building an anonymous class because different methods along the way add properties before the mail merge occurs. The code runs fine.

The issue is Visual Studio Code Analysis grades each property set operation on the ExpandoObject as 1 point of cyclomatic complexity. So a simple linear method setting a bunch of properties with no branching logic is getting scores > 25 which is the value that violates this rule.

Is this an issue with my code, or an issue with how the code analysis works?

My code looks like this:

        dynamic replacementFields = new ExpandoObject();
        replacementFields.time = DateTime.Now();
        replacementFields.url = Request.Url;
        replacementFields.server = Environment.MachineName;
        replacementFields.firstName = "Jeff";

        ... (a bunch more)

        replacementFields.phone = "555-1212";
È stato utile?

Soluzione

This is a known issue with the implementation of the CA1502 rule (https://connect.microsoft.com/VisualStudio/feedback/details/729236/use-of-dynamic-types-makes-cyclomatic-complexity-almost-meaningless), which generates inappropriately high cyclomatic complexity stats for methods that use anonymous methods or dynamic types. To see why, take a look at the compiled IL (which is what FxCop rules analyze) using a decompiler like Reflector (but without any optimizations enabled, which would hide the problematic generated code).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top