#224 – One Example of a Problem that Code Analysis Would Catch

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.

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

2 Responses to #224 – One Example of a Problem that Code Analysis Would Catch

  1. tribalthirst says:

    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.

  2. kai zhou says:

    Better to use Language Resources for globalization if the customers spread to different locales.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: