5

Sometimes, we need to stuff an integer ID into a string, for example when generating HTML form <select> items.

Calling int.ToString() can result in digits outside the range [0-9] in certain cultures. For that reason, you need to specify the invariant culture in culture-spanning apps (e.g. int.ToString(CultureInfo.InvariantCulture)).

Is it worth taking care and doing that even if it is known that the application will only run in one specific western culture (e.g. German)? It seems to be the "correct" thing to do. Or is that effort not well spent?

Update: It appears that integer formatting is almost immune to culture anyway. My assumption was wrong but I'll keep the text for context.

  • See here: Is int32.ToString() culture-specific? – Robert Harvey May 9 '16 at 18:28
  • @RobertHarvey: The formatting of ints may vary by culture in theory, but in this particular example (InvariantCulture vs German) it will always be the same. So the question is purely stylistic - should the InvariantCulture be explicitly stated even though it won't make any difference. – JacquesB May 9 '16 at 18:41
  • 3
    My philosophy is that, if you add things to your code that are not needed, you will increase the cognitive load for the next programmer because they will have to spend time reading the unnecessary code and wondering why it is there. All other things being equal, less code is always better than more. – Robert Harvey May 9 '16 at 18:45
  • Also, if you read the post I linked carefullly, it says that the formatting of ints does not vary by culture in practice. – Robert Harvey May 9 '16 at 18:49
  • How is it known that the application will only run in one culture? – topo morto May 9 '16 at 19:18
4

Since the output will be exactly the same, this is a pretty subjective choice. If you know the application only ever will run in one specific culture, then I would say leave it out for simplicity.

But if the application might be ported to multiple cultures, I would say leave it in. Even if you know all cultures you port to all have the same integer formatting, you should not require that all readers of the code know that, and without the knowledge, the code looks like a subtle bug. So make the intention clear in the code that the value is supposed to be invariant rather than culture specific.

1

I would say yes.

The problem is even if the people are all in germany that doesn't nesacerally mean they will be running german versions of windows. People immigrate and sometimes bring their computers with them, people run english versions of windows because other software is broken on german versions of windows.

As you say this is not too much of an issue with ints but it's a big issue with floating point numbers.

1

Even if you never intend to run on another system or in another culture, once that fact changes, and you may be surprised how often that happens, stating the invariant culture in this case marks your intent, which will help future maintainers of your code, whom might be yourself.

In other words; if being implicit doesn't properly convey your intent, be explicit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.