#224 – One Example of a Problem that Code Analysis Would Catch
January 27, 2011 2 Comments
As an example of the type of issues that code analysis would catch, consider the following piece of code.
static void Main() { Console.WriteLine("Grande schmande, just give me some coffee.."); }
This code obviously works fine–it just prints out a string. But if we want to someday localize this program, we’ll want to be reading the string from a resource file, rather than hard-coding it like this. So when we build the project with code analysis turned on, we see the following warning:
CA1303: Do not pass literals as localized parameters
An externally visible method passes a string literal as a parameter to a constructor or method in the .NET Framework class library, and that string should be localizable.
We see this warning in the Error List window when we build the project.
This is a great example for many reasons. First, how many developers never code for localization until it is too late. Also, it helps from hard coding the “magic strings/values” syndrome. Good tip.
Better to use Language Resources for globalization if the customers spread to different locales.