Why JavaDocs suck
It’s just a funny coincident that Martin Fowler lately blogged about the “code as documentation” approach when Marcus and I were discussing our javadoc and documentation policies.
Over the years I came to the conclusion that javadocs just suck! …not as a concept – but what most teams make out of it. Especially if they are enforced via a strict checkstyle policy. Have a look at the following code and javadoc snippet:
/**
* Comment for
m_manager
*/
private ServiceManager m_manager;
/** HttpRequest */
private HttpRequest request;
/**
*
* @param manager sm
* @throws ServiceException on error
*/
public void service(final ServiceManager manager) throws ServiceException {
m_manager = manager;
}
The main problem of documentation is that it needs to kept up to date. So having it separate from the code is really hard to mantain. So javadocs make this a bit easier. Especially if your favorite IDE knows how to handle them. But looking at the above code …what’s the benefit of having those comments? It is just redundant! Usually we programmers -as a species- hate redundency …but why do we write such docs then? Well, we are lazy asses and we don’t want checkstyle to barf at us. …and the IDE will just generate it for us with a keystroke.
But taking a step a back and looking at this …useless comments just clutter the code and do NOT help understanding what’s going on. So what I found useful is to focus on the general concept of the class. Explain the algorithms and usecases on the class level. At the end of the day it’s going to be a developer that will look at the code. So understanding the general idea will help him much more and he we will quickly work out the details. Maybe it’s not what you’ve learned at university – but am I the only one that sometimes removes comments to get more condensed code and visually see what’s going on there?
So my few rules are….
- Explain as much as possible at the class level
- If you want to force proper javadocs with checkstyle do it only for non-private or even public scoped elements
- Focus on self-explanationary names. Often that’s enough or even better than comments because the explanation is visible not only at declaration time but with every use.