Dear reader,
Today I found a place on our website where we display the size of a recreational area in square kilometers.
199 km²
I cannot fully explain what happened next, but I had a hunch something must be wrong with this feature.
Narrator: there was.
Dear reader, I immediately had a feeling there must be a problem. There must be a bug. No way there is no tiny nasty mistake in the code. I didn’t see it, but with my sinking heart, with my stomach tied up in knots, I felt it was there.
Dear reader, I analyze this post factum, hours after this happened. I realize now there might have been several reasons for me to have this strange, dull premonition.
- As far as I know, that’s the only place where we display such large areas – all other places are much smaller and measured in meters.
- It is very unusual to see a symbol in a superscript.
- I’m a magnet for bugs.
But I digress.
The page looked very normal. There was nothing visibly wrong with it. But I knew to trust my feeling.
I tested the page in all ways. Five minutes into my process, I made the website think I am from US. We don’t display kilometers for people from the US, we display miles. So this is what I saw:
124 miles²
I don’t understand, said I to myself, why do Americans use miles? How do they manage to keep it in their heads? Miles, gallons, ounces and degrees Fahrenheit.
And, dear reader, this was the moment I had a revelation.
I opened a calculator and converted 199 square kilometers to square miles.
Oh, said I to myself. 76 square miles don’t look like 124 square miles. There is almost twice more square miles on our website than needed. That’s too many square miles. That’s wrong.
Dear reader, I offer you a puzzle for your enjoyment. Try to figure out yourself why this was happening. Take a breath. Take a pauze. Close your eyes. Think.
So, dear reader, did you manage to understand? Don’t worry, I will explain.
What happened in the code, dear reader, was the following.
int value = loadArea(); // in square km String valueToDisplay = formatDistance(value); valueToDisplay += '²'; return valueToDisplay;
Ugh, thought I upon seeing the code. You don’t just append something to a string that you’re going to display to the user. You have to translate it and all.
And then it dawned on me.
Someone just used formatDistance.
Dear reader, by now you probably realize that formatDistance is meant for distances.
The author of the code converted a distance in kilometers to a distance in miles, and then added “squared” in the end.
That’s the end of the story, dear reader.
This code was written ten years ago. It is a ghost from the past. Its menacing presence haunted the users for years.
I will fix this tomorrow. Tomorrow our users shall be free from its reigning shade. But the day after tomorrow, dear reader, will offer new challenges.