My general rules for commenting (derived from On Coding Standards and Code Reviews) are:
- Separate user versus maintainer documentation.
- Generally, comments should describe what and why, not how (strive to make the code obvious).
- Include a comment block on every non-trivial method describing its purpose.
- Comment any code that is likely to look wrong or confusing to the next person reading the code.
- Don't optimize prematurely. Benchmark before you optimize. Comment why you are optimizing.
See also Perl Best Practices, Chapter 7, Documentation, which has some excellent
advice on how to document your programs.
References Added Later
Updated: Reorganized the bullet points; extra references were added later.