Elaborating on the Why, Not the What

Nat Pryce writes a good article about self-documenting code not necessarily being completely bare of comments, of which I am in total agreement of. Like many practices, developers tend to abuse the self-documenting code practice, suggesting that code should be stripped of all comments. Excessive amounts of comments are probably a good indicator of poorly written code, but some advocates forget that the premise of this practice is to get rid of as many redundant comments as possible, not necessarily all of them.

Applying the renaming suite of refactorings such as “Rename Method” can be used to get rid of ridiculous comments that describe what they do, like “This method returns a sum of numbers.” This can also be applied to all other items like classes, fields, constants, aspects and attributes.

This post started off as a comment for Nat’s entry, but I thought it would be useful to give more examples on why you might add comments.

Choices of Algorithms
Implementing some functionality normally requires some sort of trade off and these trade offs are sometimes important to document. Factors such as a framework, deployment or performance constraints might require some code to be written in a certain way, normally resulting in obfuscation. Communicating to other developers why this obfuscation exists can help because if that constraint goes away, then the code could be refactored without consequence.

Capturing Richer Domain Knowledge
Domain objects tend to be used at the core of a system, and I find, are usually the least documented. Well named attribute and class names can fundamentally describe what they represent, but may contain information or context for people new to the domain. For example, it took me a while on my current project to work out that the two terms our business analysts used actually referred to the same domain object. One was used when referring to it outside of the company context, the other used when dealing with it from inside. Especially when you are stuck with a domain model that cannot be changed it can be important to document things like differences between one attribute and another and the constraints some attributes may have.

Referencing User Requirements
Sometimes developers have to code a few things that don’t necessarily make a lot of sense on its own. Special cases mandated by users and captured by business analysts just must be completed. Fixes to bugs caused by incorrectly written user stories or overly complex domain models are another circumstance that may introduce code that, on its own, may not make a lot of sense. I find it has been useful, having done much maintenance programming lately, to add a reference to the issue that a block of code was added for. In the overly complex part of the system (caused because of the overly complex domain model that cannot be changed at the moment), the references to the Jira issues help to provide more context to the code that was added and why they are necessary.

Like all aspects of writing software, comments still requirement good judgement of how much business value they add. If you think it’s going to help someone else understand it, use it, or maintain it, perhaps its worth documenting. On the flip side, if you find existing documentation to be wrong, misleading or just a plain duplicate, then do the correct thing and remove it or fix it so it will not cause any further harm. On a final note, well written test cases can also be used to remove some classes of comments (those that describe business requirements or required functionality).

5 Replies to “Elaborating on the Why, Not the What”

  1. Good call, Pat – I fully agree. I also enjoy it when people are a bit more creative with their comments – for example, before reading through a particularly troublesome piece of code, it’s nice to read a comment like, “Sorry, but Errol made me do it” :-).

    I remember reading an audit of some of the code that makes up the Windows Kernal somewhere (can’t find the article online anymore). One of the things that they commented on was the comments preceeding the hacks – my favourite being, “The magnitude of this hack compares favourably with our national debt”. Go Microsoft!

  2. Apologies – it looks like some of the quotes were replaed with ‘smart quotes’. Should be fixed now. Thanks for letting me know.

  3. Hmm, I wonder how far back this could go? I’ve fixed those posts that were being troublesome for the current one. *Sigh*

Comments are closed.