The Source for Java Technology Collaboration
User: Password:



Simon Morris

Simon Morris's Blog

Justifiable Homicide

Posted by javakiddy on January 24, 2007 at 03:15 AM | Comments (7)

Despite not being a violent man (honest!) I've found myself increasingly being moved by the argument that programmers, and perhaps other technical staff too, should be given limited license to kill people under the defence of justifiable homicide.

All you programmers reading can stop cheering though -- I'm not suggesting a complete free-for-all. We can't have programmers just going around killing people willy-nilly, that would just be unfair. And far too arbitrary. For example. there's a guy in my office who would love to decapitate anyone who makes excessive use of the colour printers. Which, to my thinking, is a tad extreme (although, admittedly, does score favourably in the "saving the planet" stakes.)

No, instead I propose that the license to kill be restricted to certain key phrases. If a client, management, or even fellow workers utter these words, or anything remotely like them, all programmers within earshot are given immediate dispensation to dispatch them to the great Steering Group Meeting in the sky, pronto.

And so, with tongue planted firmly in cheek, I present the first in an occasional series (for which, read: "if anyone likes them I might write some more") of IT crimes which should be covered by the defence of justifiable homicide...

"I don't mind working with your code, because at least you comment it"

My first nomination for the 'justifiable homicide' defence would be a comment made by a fellow programmer, upon being given a small project of mine to finish while I was away on holiday. Actually it wasn't this comment, so much as the sheepish admission which followed that he hardly commented his own code.

Ah yes. In-line documentation. We all know we should do it. We all claim that we do do it. But, let's have a show of hands, how many of us really do it (properly)?

Exactly! (And no, Javadocs don't count. Just in case you thought you could get off lightly. They document the API from an external viewpoint, they don't document the internal implementation and mechanics.)

All too often I find myself looking through source code were the number of comment lines can be counted on the fingers of one hand. Indeed it is too frequently the case that the number of genuine comment lines is heavily outnumbered by the number of source code lines left commented out.

Back when I was at college one Computer Science lecturer requested the class each supply one printed example of a short program they had written, in any language, minus everything except the in-line comments. He shuffled and re-distributed the print-outs, and told us we had twenty marks each to start with. Every time we found a comment which betrayed which programming language the code had originally been written in, we had to knock off a mark. Every time we found a comment which merely parroted the code ("Add 1 to x") we had to knock off a mark. And so on...

As well as being a light hearted exercise (nobody scored more than five, with most people skulking in the negative numbers) it served to re-enforce an important point about commenting source code. Specifically: what to comment, where, and how much!

The golden rule, we were told, was that code should be commented sufficient that a fellow programmer could re-implement your algorithm in a totally different language from the comments alone. Excuses like "oh, but the code is self documenting" simply didn't cut the mustard (whatever that actually means!) The code explains to the compiler what should actually happen, while your comments explain what you (the author) think is happening.

(Yes YOU -- the weakest link in the chain! The fool who causes all the errors! The real culprit behind the phrase "computer error"!!)


# Make sure the key is all digits
if($key =~ /[0-9]+/)

The above is a reconstructed fragment of a web script I had the misfortune to debug some time back. The entire script contained only two comments, the first acknowledged the script's author and creation date, while the second documented a regular expression. As you can see, code and comment don't quite tally.

I wasn't specifically looking for this bug -- indeed thus far the bug had never manifested itself, so nobody was looking for it -- but thanks to that one single comment my attention was drawn to that odd section of code, and after careful examination I was able to rectify the problem before it even emerged.

These days it seems the push is for cleaner, simpler, less error prone methods of working. From the language syntax, to methodologies, to testing, there has been a renewed focus on minimising the potential for human error, and maximising the chance of trapping errors when they occur. About time too! But it is ironic, perhaps, that the most powerful tool for preventing and trapping errors is to simply get the author to write a plain English description of what she thinks the code is supposed to achieve to accompany each step of the algorithm (along with any noteworthy observations -- "does minimal error checking" or "not thread safe", for example.)

But how many bother with anything other than terse one liners? Or simply don't bother at all?

We're all guilty of being a little sloppy with when it comes to explaining our reasoning. Certainly I have to hold my hand up and admit I've written a few bits of code which have been decidedly thin in the in-line documentation stakes. We've all done it. But that doesn't make it right, and it doesn't mean it should be treated as the norm rather than the exception.

Taking that extra sixty seconds to provide a human-readable description of that morsel of code you're working on not only gives unfamiliar eyes a fast track to understanding your code, but also helps to rationalise your thinking (you'd be surprised how many flaws you find when you are forced to commit an idea to text) while affording colleagues a opportunity to spot discrepancies between what you wanted to do and what your code actually does.

All those guilty of not commenting their code should now line up against the wall, blindfolds at the ready. (Me too, I guess!) The programming firing squad will be here shortly, just as soon as they finish documenting their three-step procedure. :)


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • Code is supposed to be clear enough not to need inline comments except in rare circumstances. From http://www.codinghorror.com/blog/archives/000589.html: "There's a fine line between comments that illuminate and comments that obscure. Are the comments necessary? Do they explain "why" and not "what"? Can you refactor the code so the comments aren't required?" Many comments inside a method definition is a sure sign the code needs to be refactored.

    Posted by: jmcclure on January 24, 2007 at 08:02 AM

  • @jmcclure: if the comments are necessary to explain what the code is doing then it is a sure sign that your code is too cryptic, agreed. Comments should only be used to explain cryptic code when there is absolutely no way to make the code any clearer, and still have it perform within required parameters. However, even if your code is crystal clear, like a simple integer increment, you should still provide human understandable annotations at reasonable points throughout the source, explaining the "why" (as the code will usually provide the "how".)

    Posted by: javakiddy on January 24, 2007 at 08:56 AM

  • How about providing an example method with what you consider too few comments and what you consider acceptable comments.

    Posted by: jmcclure on January 24, 2007 at 11:27 AM

  • Acceptable comments are enough to concisely describe, in a form which scans when read (by humans), what the associated chunk of code is trying to achieve, and why. In a nutshell, to borrow a phrase from millions of high school exam papers: "give reasons for your answer".

    Posted by: javakiddy on January 25, 2007 at 02:48 AM

  • Saying "Code is supposed to be clear enough not to need inline comments" should also be grounds for justifiable homicide ;-). When we can write programs in English then the need to comment the code will no longer exist. Let's have a vote - how many of you would like to strip all the comments out of the java.* packages?

    Posted by: emorning on January 25, 2007 at 10:29 AM

  • all the inline comments, sure. We're not talking about javadoc here, and other comments describing the business logic a method is meant to achieve. We're talking about comments you see a lot in code like "increment the counter so we can process the next record", "throw a NullPointerException if we get a null argument", and things like that sprinkled around methods as oneliners.

    Those are worse than useless, and even worse are often incorrect as the code changes around them but the person changing the code fails to update the comments to match the new code.

    Posted by: jwenting on January 26, 2007 at 12:45 AM

  • But let's expand this idea of giving programmers cause for justifiable homicide to the elimination of endusers who fail to read the manual or refuse training and call us every hour of the day with questions to which they would have known the answer had they used the resources provided to them for learning how to use the system.

    Posted by: jwenting on January 26, 2007 at 12:46 AM



Only logged in users may post comments. Login Here.


Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds