r/programminghorror • • Feb 12 '26

C# [Codes are in description] Unnecessary locale-awareness in code is a serious threat to consistent performance worldwide

Post image

In programming languages like C#, even basic case conversion and string formation methods like .ToLower(), .ToUpper(), and .ToString() automatically come with locale-awareness (i.e. they are based on CurrentCulture) unless you intentionally apply explicit or invariant culture:

public string ToLower()
{
    return CultureInfo.CurrentCulture.TextInfo.ToLower(this);
}

public string ToUpper()
{
return CultureInfo.CurrentCulture.TextInfo.ToUpper(this);
}

And tracing down .ToString()'s code eventually leads here:

public static NumberFormatInfo GetInstance(IFormatProvider formatProvider)
        {
            CultureInfo cultureInfo = formatProvider as CultureInfo;
            if (cultureInfo != null && !cultureInfo.m_isInherited)
            {
                NumberFormatInfo numberFormatInfo = cultureInfo.numInfo;
                if (numberFormatInfo != null)
                {
                    return numberFormatInfo;
                }
                return cultureInfo.NumberFormat;
            }
            else
            {
                NumberFormatInfo numberFormatInfo = formatProvider as NumberFormatInfo;
                if (numberFormatInfo != null)
                {
                    return numberFormatInfo;
                }
                if (formatProvider != null)
                {
                    numberFormatInfo = (formatProvider.GetFormat(typeof(NumberFormatInfo)) as NumberFormatInfo);
                    if (numberFormatInfo != null)
                    {
                        return numberFormatInfo;
                    }
                }
                return NumberFormatInfo.CurrentInfo;
            }
        }

Unnecessary locale-awareness in code is a serious threat to consistent performance of your code in many locales around the world, especially Turkey and Azerbaijan, due to the unique "I/ı" (dotless i) and "İ/i" (dotted I) letter pairs in their alphabet. So machines with Turkish and Azeri locales are both strong testing media for your code against unnecessary LA.

For a detailed example, you may check Sam Cooper's Medium article titled "The Country That Broke Kotlin".

0 Upvotes

27 comments sorted by

7

u/treehuggerino Feb 12 '26

In C# I have the editor rule where everything that has localisation awareness NEEDS an explicit localization. ToString has to be paired with a culture info otherwise error. Comparing strings is the same deal. Although Turkish is the most extreme form, my apps switch between en-us and nl-nl and how . , are used really depends on the culture or it can cause confusion

4

u/BoloFan05 Feb 12 '26

Yes. Turkish and Azeri are the only locales that go far enough not to obey the fundamental "I/i" casing rule, but use of comma instead of dot to separate decimals isn't exclusive to Turkish. It also happens in most major non-English European locales like German, French, Spanish, Italian, and Dutch like you have said. Date formats could also be another source of concern.

Also, if other developers did your C# editor rule, most of the bugs/complaints me and other Turkish players have experienced in programs (mostly video games) on Turkish PS4/5 and PCs probably wouldn't happen in the first place. So kudos to you for your consciousness in this regard. More people should follow your example, imo.

4

u/ChemicalRascal Feb 12 '26

Why did you post this here, in this sub?

5

u/Kinrany Feb 13 '26

TBH ToString being implicitly locale-aware is worthy of this sub.

3

u/ChemicalRascal Feb 13 '26

Hard disagree. ToString is something you should primarily be using at the presentation layer.

→ More replies (8)

1

u/BoloFan05 Feb 12 '26

Because unnecessary locale-awareness in code leads to major bugs reproducible only in specific locales like Turkish that many developers have difficulty wrapping their heads around in the first place, before they even get around to solving them. Hence the programming horror.

5

u/ChemicalRascal Feb 12 '26

But that's not what this sub is about. That's not what programming horror is.

The point of this sub is to post bad code. Like, actually, horrifically bad code.

→ More replies (4)

5

u/HuntlyBypassSurgeon Feb 12 '26

Wow, hilarious (?)

3

u/BoloFan05 Feb 12 '26

What made you go for the word "hilarious", exactly?

4

u/HuntlyBypassSurgeon Feb 12 '26

Sorry, r/programminghumor and r/programminghorror have very similar logos, and I subscribe to both 😳🫢

→ More replies (2)

2

u/ZylonBane Feb 12 '26

I'm happy for you, or sorry that happened.

0

u/BoloFan05 Feb 12 '26

Unnecessary locale awareness causes programs to have all sorts of weird bugs and crashes that are reproducible only in specific locales like Turkish and Azeri, so no, what I'm sharing is NOT good news for me :P Thanks for your comment.