DEV Community

Cover image for Little Geo Stories: Square Miles
Elena
Elena

Posted on

Little Geo Stories: Square Miles

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.

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.

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.


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.

Top comments (15)

Collapse
 
ben profile image
Ben Halpern

This kind of reminds me of an interaction I had with a journalist who I was chatting with as she worked on a piece. It was about a refugee slum where a certain number of people lived in a certain number of square KM. The point was that it was a lot of people packed into a small amount of space.

But the math was weird. I don't remember the details, but conversions between miles and KM and multiplying squares and everything is just super unintuitive. It would have been very easy to make a mistake and move on. A false positive on correctness.

This code was written ten years ago.

😰😰😰😰😰


In a total tangent, this also reminds me of the 500 mile email. The details are not similar really, but everyone should read that one. It's a great bug story.

Collapse
 
ice_lenor profile image
Elena

I read this one, great story. Miles are insidious.

And the conversion is actually super easy.
1 km = 0.621 mi.
1 km2 = 0.386 mi2.

Collapse
 
evanoman profile image
Evan Oman

+1 for 500 mile email, fantastic story

Collapse
 
jfrankcarr profile image
Frank Carr

On the bright side, at least you didn't discover the bug when it was 33.9+ million miles (54.6+ million kilometers) away.

Collapse
 
ice_lenor profile image
Elena

Awesome. I knew the story, but completely forgot about it - and it's also about units. Thank you for reminding me!

Collapse
 
voins profile image
Alexey Voinov

Nice catch. :) But let me just remind you, that there is always a lot of Europeans in US (tourists for example), who would prefer to see square kilometers even when they're traveling. It is so obvious, that many companies forget about it and enforce settings by geo location. :)

Collapse
 
bohdanstupak1 profile image
Bohdan Stupak

Thanks for a nice post. As for myself, I love how such types of issues are eliminated with F# with the help of units of measure. You might want to check it out at some point

Collapse
 
ice_lenor profile image
Elena

What a neat approach. Next time when starting a new personal project, I'll definitely consider F#. Thank you.

Unfortunately, here at work it is not possible.(

Collapse
 
codemouse92 profile image
Jason C. McDonald • Edited

Gorgeous catch! I get those nagging little feelings about things too. I admit, I feel a little silly that I didn't get what was wrong w/ the code until you explained it, but I'm tired and on half my usual coffee intake, so mileage may vary (pun intended).

Hey Ben Halpern, y'all should really add one of those fancy "spoilers" expander boxes for stuff like this, so the answer can be hidden. I suspect people would really make a lot of use of that!

Collapse
 
rhymes profile image
rhymes

We need to go back to measuring things in leagues

Collapse
 
saadbashar profile image
Saad

Nice way of writing! :D I really enjoyed reading it! Keep up the good work!!

Collapse
 
val_baca profile image
Valentin Baca

Neat post :) Having worked in maps, as soon as I saw "km2" I knew what was coming :D

Collapse
 
ice_lenor profile image
Elena

Yeah, right? You just know.

Collapse
 
jakedohm_34 profile image
Jake Dohm

Thanks for writing this, Elena.

I loved the story, and really enjoyed the unique writing style!

Collapse
 
ice_lenor profile image
Elena

Aw, thank you, I'm really flattered about the style:)