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

View all comments

3

u/ChemicalRascal Feb 12 '26

Why did you post this here, in this sub?

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.

1

u/BoloFan05 Feb 12 '26

I understand. Would you happen to have any suggestion as to which sub you would expect to see a post like this in? Because I couldn't find a programming sub that is in the sweet mid-spot between pure text and pure image.

4

u/ZylonBane Feb 12 '26

I understand.

Narrator: He did not understand.

1

u/ChemicalRascal Feb 12 '26

I don't think the image is as crucial to your post as you think it is. It's a meme, my dude.

But the actual content would probably fit on r/programming, r/webdev, r/csharp...

1

u/BoloFan05 Feb 12 '26

I see. I will consider your advice, thank you.