Our own Daily WTF
- by Dennis Vroegop
Originally posted on: http://geekswithblogs.net/dvroegop/archive/2014/08/20/our-own-daily-wtf.aspxIf you're a developer, you've probably heard of the website the DailyWTF. If you haven't, head on over to http://www.thedailywtf.com and read. And laugh. I'll wait.
Read it? Good. If you're a bit like me probably you've been wondering how on earth some people ever get hired as a software engineer. Most of the stories there seem to weird to be true: no developer would write software like that right? And then you run into a little nugget of code one of your co-workers wrote. And then you realize: "Hey, it happens everywhere!"
Look at this piece of art I found in our codebase recently:
public static decimal ToDecimal(this string input)
{
System.Globalization.CultureInfo cultureInfo = System.Globalization.CultureInfo.InstalledUICulture;
var numberFormatInfo = (System.Globalization.NumberFormatInfo)cultureInfo.NumberFormat.Clone();
int dotIndex = input.IndexOf(".");
int commaIndex = input.IndexOf(",");
if (dotIndex > commaIndex)
numberFormatInfo.NumberDecimalSeparator = ".";
else if (commaIndex > dotIndex)
numberFormatInfo.NumberDecimalSeparator = ",";
decimal result;
if (decimal.TryParse(input, System.Globalization.NumberStyles.Float, numberFormatInfo, out result))
return result;
else
throw new Exception(string.Format("Invalid input for decimal parsing: {0}. Decimal separator: {1}.", input, numberFormatInfo.NumberDecimalSeparator));
}
Me and a collegue have been looking long and hard at this and what we concluded was the following:
Apparently, we don't trust our users to be able to correctly set the culture in Windows. Users aren't able to determine if they should tell Windows to use a decimal point or a comma to display numbers. So what we've done here is make sure that whatever the user enters, we'll translate that into whatever the user WANTS to enter instead of what he actually did.
So if you set your locale to US, since you're a US citizen, but you want to enter the number 12.34 in the Dutch style (because, you know, the Dutch are way cooler with numbers) so you enter 12,34 we will understand this and respect your wishes! Of course, if you change your mind and in the next input field you decide to use the decimal dot again, that's fine with us as well. We will do the hard work.
Now, I am all for smart software. Software that can handle all sorts of input the user can think of. But this feels a little uhm, I don't know.. wrong..
Or am I too old fashioned?