Last time I desperately tried to convince you to take source code documentation seriously and not treat it as a hurried afterthought, lest the technical debt management unit catches up with you and demands an explanation as to why you executed an explicit commit on that database handle, when it is running in auto-commit mode. Believe me, in a year you will have forgotten the completely valid reason for doing so unless you document it today.
I would like to dwell a little on the difference between API documentation and the rest, because it's an essential difference. In Java, API documentation can be generated from your source files, provided your code comments are properly formatted for the javadoc standard. Modern development studios like Eclipse and Netbeans make life very easy in that regard.
On the other hand we have these little one-word or one-line comments scattered throughout. Let's call them inline comments. Whereas these are an integral part of your source code, API docs are extracted to be read separately, intended for your users. Users of a third-party software library are programmers, but users nonetheless insofar as they should be expected to use your API without reference to the sources. In many commercial software libraries they will not even have access to them. Therefore make sure that the documentation for your classes and methods are mini-manuals.
The manual tells you what your library (or toaster) does and how to operate it. It only touches on technical details where they are relevant for the operation. Documentation for a class or method should cover behaviour (what goes in, what comes out, what can go wrong) but no explanations as to why. If it becomes necessary to explain something unintuitive in your design it's your job to fix the design. Unless of course you're not in a position to do so.
If you find yourself writing a lot of inline documentation, one or more of the following may be the case:
- // loop though all Address objects
for ( Address adr : getAddresses()){
//throw a ValidationException if the postcode doesn't match the regular expression
if ( !POSTCODE_PATTERN.matches(adr.getPostcode()) ){
throw new ValidationException(...)
}
}
You underestimate the intelligence of your fellow programmers. Assume that the reader of your code is a competent programmer. If it doesn't pass you own 'duh-test', leave it out. Your typical opening and closing braces are regularly more than a few hundred lines apart. You keep getting tangled up in your own spaghetti and have to tell yourself what you're doing every three lines. This is really bad and I'm not even going to explain why. In fact, I won't even give you an example. It's time for some serious refactoring.
You're programming against an undocumented, unintuitive or buggy API (probably all three at the same time). It has methods called doStuff or patrick_solution. You have my sympathy. This is where inline comments are indispensable, because you can tell the the world it's not you who is incompetent.
Dog fido = zoo.getDogByName(“Fido”);
//Yeah, it says Id, but it's actually a lookup by name
Cat minou = zoo.getCatById(“Minou”); proxy.insertAndCommit(newRecord);
//insertAndCommit doesn't seem to autocommit on MySQL4.X . //AARRGH!!
proxy.getConnection().commit();You get paid by lines of code. Where do you work, and are they hiring?
As far as inline comments go I see a lot of type one and type two, and not nearly enough of type three, especially in code that makes frequent use of open source libraries. There are a of unintuitive and badly documented libraries around that actually work fine once you know where the pitfalls are. Why any programmer would not run that extra mile to make their stuff actually usable is beyond me. Unless they really are only working for their own pleasure.


