Click here to monitor SSC

Andrew Hunter

Commentary

Published Monday, June 08, 2009 3:19 PM

I write a lot of comments in my code: these days it's second nature. Every declaration I write has a comment describing what it's for and maybe some remarks describing things I think are the associated gotchas. Inside method definitions, I maintain a running commentary describing what the method is supposed to be doing and why. I feel that comments are the best place for code documentation for one simple reason: the source file is the one thing that you can guarantee that a future developer will read.

This code style is something that has evolved over many years. When I was starting out in development I took a more conventional attitude, that if the code was readable (sensible names, well structured) then it was also automatically understandable. Time changed this attitude. At first, I grew used to looking at the code of other developers and wondering what they could possibly have been thinking while they wrote it and wishing I could throw the whole thing away. Later I found myself wondering the same thing about code that I had written long ago. If I made changes, they would fail in unpredictable ways. The variable names, they did nothing.

Occasionally the problem turned out to be genuinely bad or lazy code, of course. Mostly, though, the problem was one of understanding. Code written in the languages we have available today explains in great detail the steps taken by a program to complete a particular operation. The name of a method or class will usually explain what the operation is. It gets fuzzier as you 'draw back' from the individual operations and try to get a higher level overview.

The problem is that developers can usually fill in the gaps in their own code: they can remember writing it and their own reasoning. What seems obvious now might not be obvious to the next developer. At best, this makes their life harder: they need to spend longer looking at the code and its documentation before changes can be made successfully. More often, these 'obvious' things will result in new bugs being introduced during maintenance: at worst, these bugs won't be discovered before the code is in production. It's better to document something that seems self-explanatory now rather than wait until later when nobody remembers why it was supposed to be self-explanatory in the first place.

So, I set out to document as much as possible, forcing myself to try to write something interesting even about things that seemed they would be obvious. It wasn't particularly easy at first: the idea was to write comments first and code later but I always felt more productive writing code so I'd have to go back and write the comments later. As I was doing this I found something interesting: sometimes by trying to describe what I was doing I would discover what was wrong with it and I'd be able to fix it much earlier.

I think this idea also appears to be at the heart of the idea of pair programming: if you can't comfortably explain what you are doing, you are probably doing it wrong. I found that by writing about what I was doing before I did it I would spot logic errors and parts that were not entirely thought through much more quickly: by refining my comments I was finding ways to refine the code. This improved my productivity: the code was easier to read, more structured and I avoided introducing bugs - spending time on the comments meant I wrote less code in a day, but I also spent less time fixing it or working around problems I had introduced.

This is an agile approach to development: documenting design in the code with a 'just-in-time' approach. It works well because it is a focussed way of solving a problem - designing and implementing the solution in a single pass. It's also effective because it makes it easier to keep the documentation in sync; if the documentation is kept in the same place as the thing that it is describing then it's much easier and more natural to keep it up to date as changes are made.

I think there is a tendency for new developers to eschew comments for reasons that are perhaps ill-founded and which become ingrained. Commenting frequently not only ensures that code remains readable once the original development cycle is distant memory but also provides a time-saving sanity check that can prevent poor code from being written in the first place.

Comments

 

Alex.Davies said:

I like writing comments to clarify ideas before writing the code too. The only trouble is that comments, despite being vastly better than external docs, still get out-of-sync with the code.
June 8, 2009 9:25 AM
 

beechgrovejoe said:

Writing comments to clarify code is a childish choice made by newbee coders (taking examples from their text books) or companies that really don't understand the problem.  The issue is always wrapped up in attempting to allow maintenance coders (usually not the original coder) to somehow be able to understand the code more quickly.  The probem is that the comments don't have any real significance to the operation of the program.

If you are looking to understand the code, READ THE CODE not the comments.  If you cannot understand the program without comments, you simply are not qualified to be modifing the code.

All the comment proponents always ignore the additional costs of paying coders to enter comments that don't do anything.  Furthermore they ignore the waisted time maintenance coders use up assuming those comments are usefull.

The only people that benifit from comments in code are the coders getting paid (by the hour) to enter them.  To everbody else they just drive up the costs!
June 8, 2009 7:25 PM
 

Phil Factor said:

I'd like to suggest a rather different approach to BeechgroveJoe. I don't think you should write any code at all until you have a full set of documentation/comments  that is intelligible to your peers. This documentation should explain at a high level what the objectives of the module are, and to detail the algorithm, classes and methods (or procedures and functions) including the parameters, sample output and examples of use. It takes far less time to change documentation than to change code, and with the eye of experience, errors become just as glaringly obvious from documentation as they do when cutting and testing code. Once you've hammered out the documentation and it, at last, seems right to yourself and your colleagues, and presents a logical and consistent system, then the actual cutting of code is a far happier task with little of the anxiety of speculating whether the thing will work. A huge advantage of all this is that a team can assist by applying their special skills and knowledge to a problem through annotating the documentation (with tracking on)  before anything is prematurely 'set in stone' by cutting code. Amongst other areas of expertise that can be brought to bear, Database Developers can spot mismatches with the relational model, Testers can ensure that the application is 'built for testing', Production staff can advise on ways of easing their maintenance and support task, and Team Leaders can make sure that the design is fit for purpose, and more easily tracked for progress.

Hmm. I suspect that this is the antithesis of 'Agile', but it certainly works. I wonder whether I should start a new development methodology with this thought.
June 9, 2009 4:02 AM
 

timothyawiseman@gmail.com said:

Andrew, I think you have some excellent points.  Comments are often far more useful than external documentation and less likely to get lost as well.

I believe this is what Knuth referred to as Literate Programming.

June 9, 2009 10:48 AM
 

Arles said:

It’s interesting what Phil said about having a “different” approach,  I believe that Phil’s suggestion should actually be the “common” approach. I wonder how many houses or buildings would still be standing today if an architect didn’t take the time to create the blueprint of the house first?  Would you blame a plumber for setting a commode in the kitchen if he didn’t get the documentation (“blueprint”) of where the bathroom was?
June 9, 2009 4:43 PM
 

BuggyFunBunny said:

>> Hmm. I suspect that this is the antithesis of 'Agile', but it certainly works. I wonder whether I should start a new development methodology with this thought.

When I first got involved with this sickness in the 1970's, COBOL coders (not that I will admit to writing any) proceeded as such; modulo Database Developers, as there were none (IMS doesn't count).  This has since been replaced with Object Oriented coding (manifested with Data classes and Action classes), which is development in the Debugger.  Progress, I guess.
June 9, 2009 9:53 PM
You need to sign in to comment on this blog
<June 2009>
SuMoTuWeThFrSa
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011
How to Kill a Company in One Step or Save it in Three
 The majority of companies that suffer a major data loss subsequently go out of business. Wesley David... Read more...

Migrating from OCS 2007 R2 to Lync: Part 4
 Having migrated the rest of our users and legacy resources across and started getting ready to... Read more...

Automated Script-generation with Powershell and SMO
 In the first of a series of articles on automating the process of building, modifying and copying SQL... Read more...

Seth Godin: Big in the IT Business
 Seth Godin has transformed our understanding of marketing in IT. He invented the concept of 'permission... Read more...

Using SQL Test Database Unit Testing with TeamCity Continuous Integration
 With database applications, the process of test and integration can be frustratingly slow because so... Read more...