A hilarious coding joke: Laughter is the best medicine

I stumbled upon this funny joke when I was searching “story of coder” to find out how other coders struggled to become professionals in their field, so that I can share some of them to get and share inspiration. But I didn’t know what I would find was more amazing than I was looking for.

We’re working on this computer game called Terra Nova, where you run around outside in powered battle armour with your squad mates beating people up.

One guy on the team is a junior programmer, who is a fine guy and an okay coder, but whose coding style isn’t all that ‘mature’, as we say in the business — the code often isn’t very elegant or robust.

Anyway, he’s working on this system for displaying data about your powered battle armour. There’s a little 3-digit LED that shows the suit’s temperature, and he’s putting the temperature into string form for displaying there. So his code looks something like this:

temp_to_str (int temp)
{
char str[4];
sprintf (str, "%d", temp);

and so forth.

I tell him, “Hey, this is no good, because if the temperature ever goes over 1000 (or under -100), you’ll overflow your string and the game will just totally crash without warning.”

He says, “Oh yeah, okay, whatever, I’ll fix it.”

So I look back at the code a couple days later, to make sure that he’s fixed it, and he has indeed changed the code. It now looks like this:

temp_to_str (int temp)
{
  char str[4];
  if (temp <= -100 || temp >= 1000)
    fprintf (stderr, "WARNING: PROGRAM IS ABOUT TO CRASH!\n");
  sprintf (str, "%d", temp);

Credit: Dan Schmidt