Sloppy Programming

There are certain things in computer programming that some people do right, and some people do lazily. There have been times where I’ve taken the lazy option, but those are very rare, and when they do happen they don’t affect the user experience.

The worst kind of laziness is the kind that the person using the software can immediately notice, and nothing is more noticeable than bad grammar. A computer program doesn’t understand spoken language grammar, but the person writing the code should.

The classic example of this shoddiness is when an email program tells you:

You have 1 new messages.

It’s uncomfortable to read, and it’s completely un-necessary. Some programmers might take the next level of laziness as acceptable and show you:

You have 1 new message(s).

OK, it’s better, but it’s still up to the reader to decide if the s in brackets is needed to make the sentence work. It’s still incredibly lazy, because the code required to make it perfect is so trivial:

print(“You have “+msgs+”new message”+ (msgs == 1 ? “.” : “s.”));

So now, if the user has 1 message, there will be no s, otherwise message will be pluralized. A simple piece of extra code has given the end result the polish it deserves. You could even take this example a stage further by converting ‘1’ into ‘one’. The code for that isn’t so trivial but it would be a nice touch.

The example that made me write this post is a little different. I’m currently addicted to the Traveler IQ Facebook application, which reports in my news-feed when I improve my traveler IQ. The latest report says:

Mark now has a Oceania/Australasia Traveler IQ of 91.

Ignoring the fact that my southern hemisphere geography seems to suck, it’s  a sloppy sentence. I can’t read it out loud, or even in my head, without cringing slightly, and it’s another trivial piece of code to make it right:

print(name+” now has a” + (substr(challenge,1,1) in (‘A’,’E’,’I’,’O’,’U’,’H’) ? “n ” : ” “) + challenge + ” of ” + score + “.”);

(Note I’m using pseudo-code in these examples, but most languages have similar constructs.)

So if you’re writing programs which output information to the user in plain English, please take a moment to consider if that plain English will be correct for all possible values of your variables. It doesn’t take long, and your end product will look more professional.

8 thoughts on “Sloppy Programming”

  1. He’s a geek! Burn him!

    I noticed something similar in a ticker I use on my baby blog to track Jack’s age. It would say something like “Jack is 5 months and 1 days old”. Gah.

  2. H?

    I think its an hop from vowels to Hs.
    That doesn’t make an happy man out of me. It doesn’t give me an high. It looks like an half-baked idea.
    Let me give you an hand in fixing it. I have an head on my shoulders. You just have to adjust it by an hair. I don’t need much money, just offer me an hamburger, an hammer, or an ham sandwich.
    Consider putting an hold on that code in the meantime.

    (Sorry, don’t mean to give you an hard time)

  3. In my defense, the code was in the context of geographical quizzes so in most cases the an before the H would be appropriate in that context. The fact that the Traveler IQ app doesn’t have any challenges starting with H means I could’ve left it out though.

    I never understood the an before H thing anyway, we had it drummed into us at school but it never made sense to me. In theory your comment is grammatically correct, but it’s horrible to read.

    Luckily we’re not staying in an hotel for our upcoming vacation.

  4. It’s not that less trivial, is it? Something like:
    print(”You have “+ (msgs==1 ? “one” : msgs)+” new message”+ (msgs == 1 ? “.” : “s.”));

Leave a Reply

Your email address will not be published.