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.)