Little Geo Stories: Square Miles
Tags: geo, distance, code, codequality
Published: 2018-01-24 19:24:59

---
title: "Little Geo Stories: Square Miles"
published: true
description: How I sensed a bug while not seeing any, and what came of it.
tags: geo, distance, code, code quality
cover_image: https://thepracticaldev.s3.amazonaws.com/i/rq935egsfzc0ipn9bbhx.JPG
---

Dear reader,

Today I found a place on our website where we display the size of a recreational area in square kilometers. This is what I saw.
<pre>199 km²</pre>
I cannot fully explain what happened next, but I had a hunch something must be wrong with this feature.

<em>Narrator</em>: there was.

<hr />

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 <em>felt</em> 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.
<ul>
 	<li>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.</li>
 	<li>It is very unusual to see a symbol in a superscript.</li>
 	<li>I'm a magnet for bugs.</li>
</ul>
But I digress.

<hr />

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:
<pre>124 miles²</pre>
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.

<img src="https://thepracticaldev.s3.amazonaws.com/i/pddy29h09mcygpczycgr.png" alt="199 square kilometers equals 76.8343 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.

<hr />

So, dear reader, did you manage to understand? Don't worry, I will explain.

What happened in the code, dear reader, was the following.
<pre>int value = loadArea(); // in square km
String valueToDisplay = formatDistance(value);
valueToDisplay += '²';
return valueToDisplay;</pre>
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 <em>formatDistance</em>.

Dear reader, by now you probably realize that <em>formatDistance</em> is meant for <em>distances</em>.

The author of the code converted a <em>distance</em> in kilometers to a <em>distance</em> in miles, and then added "squared" in the end.

<hr />

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.