Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Commenting nowadays is easier than ever. In Java, there are some nice techniques for linking comments to classes, and Java IDEs are good at making comment shells for you. Languages like Clojure even allow you to add a description of a function in the function code itself as an argument.

However we still live in an age where there are often obsolete or poor comments written by good developers - I'm interested in improving the robustness and usefulness of my comments.

In particular I'm interested in Java/Clojure/Python here, but answers don't need be language-specific.

Are there any emerging techniques that validate comments and automatically detect either "flimsy" comments (for example comments with magic numbers, incomplete sentences, etc..) or incorrect comments (for example, detecting mispelled variables or the like).

And more importantly: Are there accepted "commenting-policies" or strategies out there? There is plenty of advice out there on how to code - but what about "how to comment?"

share|improve this question

migrated from Dec 29 '11 at 0:21

This question came from our site for professional and enthusiast programmers.

up vote 34 down vote accepted
  • Names/documentation should tell you what you are doing.

  • Implementation should tell you how you are doing it.

  • Comments should tell you why you do it the way you do.

share|improve this answer
comments should also tell you why you are not doing it another way - i.e. important design choices - because future maintainers will not have this information otherwise. – user1249 Jan 15 '12 at 8:39
I believe there are many times a comment should say WHAT you are doing too. This idea of only "why" comments is an anti-pattern in my opinion. It is a myth that you can write all code clear enough that any programmer can understand it at a glance, and my experience is most programmers who think they write clean code don't. It's the same as saying, "I don't have to name this function descriptively because anyone can just read the code in the function to see what it does." - that doesn't make sense either, does it? – dallin Sep 3 '13 at 21:05
@dallin if your code isn't clear about what it is doing; consider refactoring it. otherwise add a comment explaining why it's implemented like that (which happens to also explain the how better). Your comparison with descriptive names is apples/oranges, a descriptive name makes it clear where the function is used, and you may not have access to the source code of the function. – ratchet freak Sep 4 '13 at 7:33

This might be controversial, but my advice would be to write as FEW comments as possible. Use nice, clear class names, variable names and method names instead. Write your code in the clearest way that you can; and consider this to be the most important attribute of your code (other than that it meets its requirements). Only write a comment if you've made a method as clear as you possibly can, and you still think it requires further explanation.

And have an organisational practice, that whenever anyone changes a class in any way, they have to make sure the comments are still all correct.

share|improve this answer
This is a good start, but I think to satisfy the OP's question you will need to write something that addresses his actual concerns with respect to automatic validation. – Robert Harvey Dec 29 '11 at 0:24
+1 - I also think that comments should only be used to explain why code has been written. I know what if (a == b) then c(); does, but if I don't know why it does it - that's when a comment should be used :) – Deco Dec 29 '11 at 1:29
Deco - I agree completely. Comments are good when I want to understand how a particular method fits into the process as a whole. In other words, WHY you'd call this method, or WHY it does what it does. – David Wallace Dec 29 '11 at 5:31
In addition to make written code clear, we should also make sure to retain (code-level) ideas, thoughts, etc. using TODO comments. E.g. if you see a function/class is able to handle current data size properly, but potentially will fail to handle load after 2 years, then make sure to write your observation there using TODO comment. Future developers will indeed appreciate your efforts. Don't ever try to note these things in a separate txt/word document, while writing code, no one really refers such documents. – TechCoze Dec 29 '11 at 10:54

I'm not sure about other languages, but python allows you to write doctests which are a form of self-validating comments. Of course, they shouldn't be used in place of real unit testing, but are a quick and easy method of documenting specific functions that may not be as obvious as they should be. They come with the added benefit of being able to execute the comment tests to verify that comments are still correct (at least the portion of them that contain tests).

share|improve this answer
And Sphinx compares code against documentation to provide coverage metrics. – S.Lott Dec 29 '11 at 1:21

One of the most authoritative location to find how to use code comment to generate documentation automatically is surely Doxygen. Though there could me more of such tools.

This defines the standard of comment writing which should be followed to automatically generate documentation. However, this gives more of a structure but doesn't validate semantically; for example it cann't check if you have used misleading english to describe the purpose of a function!

While, this is the best thing that make comments structured, personally i feel there is more documentation needed to make code more maintainable as such. Some time back there was a question in P.SE Can code be the documentation in open source developer tools? How frequently is it? Of course, This applies to non-open-source projects as well.

To make code more maintainable - it is practically more important that there exists a external documentation that helps create structure of how to treat code, and then comments inside the code should be restricted to see

I think, if you want to define the policies for comment writing you should include as a holistic approach included in the coding standard. See this: What could be some pitfalls in introducing a style guide and documentation generating software in a development team?

Usually a comments constitutes less than 5% of the code. And in practice while code-reviews itself draws much less attentions (over other aspects of development) is is practically difficult that comments are also reviewed.

share|improve this answer
I disagree with the last sentence here. I've just finished a contract, working under a team leader who gave very detailed reviews. His reviews always included the comments - how they were worded, whether the variable names were correct, whether they contained all the information that a future developer might want to know. Before long, I knew what he expected to see in each comment, and was able to produce comments to his standard. So, provided it's organisational policy to have code reviews, and include comments in the code review, then it will happen. – David Wallace Dec 29 '11 at 21:06
This is usually the only kind of comment I write, especially for methods to document what goes in and what comes out of it (I work with loosely typed languages). – DanMan Apr 23 '14 at 17:54

Are there any emerging techniques that validate comments and automatically detect either "flimsy" comments (for example comments with magic numbers, incomplete sentences, etc..) or incorrect comments (for example, detecting mispelled variables or the like).

There is a well-know technique - it is called "code-review", and has a sister named "pair-programming". Don't expect anything "automagically" here.

And more importantly : Are there accepted "commenting-policies" or strategies out there ? There is plenty of advice out there on how to code --- but what about "how to comment?"

"Code complete" contains not only all about how to code, but also chapters on "how to comment", especially on how to write self-documenting code.

share|improve this answer
+1 for Code Complete. Clean Code by Robert Martin also has a good chapter on writing useful commends. I'm not sure about Java world but in .NET world we have Resharper, which can 'automagically' validate references to code elements in comments :) – MattDavey Dec 29 '11 at 9:58

Specific to Java one source I've enjoyed is Oracle's How to Write Doc Comments for the Javadoc Tool:

This document describes the style guide, tag and image conventions we use in documentation comments for Java programs written at Java Software, Sun Microsystems.

And Item 44: Write doc comments for all exposed API elements:

If an API is to be usable, it must be documented. Traditionally API documentation was generated manually, and keeping it in sync with code was a chore. The Java programming environment eases this task with the Javadoc utility. Javadoc generates API documentation automatically from source code with specially formatted documentation comments, more commonly known as doc comments.

from Effective Java 2nd Edition.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.