0

So do I just ignore this code analysis warning by suppressing it? Or is there a way to truly fix it?

Here is a user story that comes close to mine, but I changed it slightly so that company information isn't on the site...

Say I have a website for a company that ships to 15 countries, and they want to show the names of those countries in the user's language of choice from the appropriate resources.resx file. Now my "options" in a list are more complex than just a name/value or key/value pair. So the current code has a method that returns all the options, so it might look like:

return new[]
{
    new CountryOption(code1, resourceKey1, someOtherValue1),
    new CountryOption(code2, resourceKey2, someOtherValue2),
    new CountryOption(code3, resourceKey3, someOtherValue3),
    ... (repeat 12 more times so I have 15 countries)
};

Thus I get a list (IEnumerable<CountryOption>) of all the countries from which to choose.
Most such applications will read this sort of information from a database, but this data rarely changes and putting it into a database will slow the performance of the site. One could put this into a flat file to read, but again, compiled in code will be faster. Finally, we do have some unit tests to make sure this information is correct that run with each build (harder to do for information in a database).

Is the only way to reduce cyclomatic complexity for a list of known values to read it from some source outside the code? (If so, the suppressing the message is probably the right thing to do.)

KarlZ
  • 170
  • 9
  • 4
    May I ask why the cyclomatic complexity is high in this specific case? There is just only path to follow, no branching of some sort, right? As I understand it, the method you displayed would have a cyclomatic complexity of 1. Am I correct? – Kzryzstof Nov 29 '15 at 01:04
  • 3
    Don't reduce cyclomatic complexity just for the heck of it. Use cyclomatic complexity as a tool for finding potential snippets of code in need of refactoring. – Mephy Nov 29 '15 at 01:32
  • 1
    It is difficult, to say the least, to see where anything described in your question would lead to a high cyclomatic complexity score. Please provide a good [mcve] that reliably reproduces the issue. Please provide additional details as to why the score is high and what you've already tried in terms of reducing it. – Peter Duniho Nov 29 '15 at 05:54
  • @Kzrystof, @Peter Duniho - I'll investigate more. I tried with a simple class that had 2 properties, `int Key` and `string Name`, and only got a cyclomatic complexity of 1? So I'm not sure why an even smaller number of items added to the array with our class generates a 27 (basically 2 per item in the array.) – KarlZ Nov 30 '15 at 14:39
  • My guess is that the CountryOption class ctor is doing a lot of things :)Could you look into that? – Kzryzstof Nov 30 '15 at 14:44
  • @Kzrystof - To delay the loading of the text the constructor had `func` and something like `() => Resources.ResourceKey1` was passed in. If you just pass in Resources.ResourceKey1 instead of that func, the cyclomatic complexity drops to 1. I'll see if the performance gain warrants that complexity. – KarlZ Nov 30 '15 at 16:42

1 Answers1

0

As others pointed out, the creation of a collection by itself had a cyclomatic complexity of 1. But the object that was in the collection was using a func<string> and that drove the complexity up - basically 2 for each item in the collection! Thanks for the help.

KarlZ
  • 170
  • 9