views:

77

answers:

3

What is the best practice for specifying CurrentCulture or InvariantCulture and not specifying the culture at all?

From what I have read, if you're doing serialization, for instance, you need InvariantCulture as a means of specifying a canonical representation of a data value. That's a relatively small percentage of culture-based string manipulations.

I find it long, verbose, and ugly most of the time to specify it every time I do, say:

var greeting = string.Format(CultureInfo.CurrentCulture, "Hello ", userName); 

However, my team recently turned FxCop on and now there's a push to always use CultureInfo EVERYWHERE. What is the best technique to combine brevity, readability, and functionality?

Some good reading material: http://msdn.microsoft.com/en-us/library/4c5zdc6a.aspx and http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx.

+1  A: 

Don't specify culture: the framework will use the current. Specify the current at application start or web session or request start.

onof
This is not necessarily good advice if you're doing a large-scale, international software package, IMO...
Reed Copsey
+5  A: 

There is an inherent trade-off in play here.

At a minimum, you'll want to specify CultureInfo to use InvariantCulture whenever you are doing anything internal within your program. For example, using this with Serialization forces the data representation to always be the same, so you don't have to worry about internationalization issues with your internal data formats.

That being said, specifying this everywhere has some advantages - mainly in terms of forcing you to make sure you're handling this correctly. Internal program work vs. UI work needs to have a different culture specified (provided you want to properly localize your application). As a result, a complex program tends to require this to be specified everywhere, as leaving the "default" is dangerous at best, and tends to introduce bugs over time.

However, specifying this, as you noticed, tends to increase the size of your code, and potentially reduce the readability. This leads to the trade-off - readability and maintainability via shorter code vs. proper internationalization and localization and maintainability via being more explicit everywhere.

In my opinion, there is no "right" answer here - it really depends on your application. If your application is completely about presentation, and not doing a lot of data manipulation, especially not with any type of self-managed file storage, setting the current culture (and ui culture) once may be fine. I've found that more complicated applications tend to not work as well in this fashion, however, in which case the FxCop suggestions of specifying this everywhere seem more attractive.

Reed Copsey
@Reed Copsey: I have also found that adding it everywhere as an FxCop reflex creates bugs -- several times I've seen instances where the wrong culture option was specified. This is obviously just an education problem, but is a common occurrence when something like FxCop tells you to do things but you don't necessarily understand why.
Scott Stafford
@Scott: True. But this is the case with everything in programming - if you are going to use it you need to understand it or it'll bite you. The only sane way to do localisation is to consider it when you first write the code - localising a completed product is one of the worst jobs to do, and an order of magnitude more expensive than thinking about it from the beginning.
Jason Williams
I don't agree. Would I put the culture in all ToString() of my classes? I think logic and objects should be unaware of culture.
onof
BTW, one way to get around the FXCop warnings and keep the code readable is to write wrapper methods of your own that hide the CultureInfo. i.e. write a FormatStringForUI() that will always use the UI culture. This cleans up your code and also gives programmers a simpler range of clearer choices: (e.g. UI or Serialisation?) (@onof: exactly :-)
Jason Williams
@onof: I typically have a service to provide the culture, and specify it there. However, tt's really a matter of scale here - very large scale, internationally targetted projects need to take a lot of care to get culture "right" - and putting it everywhere tends to help.
Reed Copsey
Got it. Indeed i did not consider to inject a localization service in business logic. Thanks
onof
+1  A: 

The default is already the current culture as initialized by Windows. So using CultureInfo.CurrentCulture explicitly is just a waste of time. Any decent serialization format (including binary serialization and XML serialization) will serialize a DateTime in a culture invariant way.

Using a culture that is not the default is very dangerous. A thread will always be started with the default culture as specified by Windows and configured by the user when she installed Windows. .NET starts threadpool threads all the time and you'll risk getting a culture in that thread that is different from your main thread. Which can cause all manner of subtle problems. Like having a SortedList that suddenly isn't sorted anymore.

Hans Passant