J
js_dev
Java comments
Commenting in Java
Basics:
1) Javadoc comments are for permanent API documentation
2) Non-Javadoc comments are for the programmer to read and understand
things that are not so obvious from the code.
3) Professional programmers write flow commenting style (informal step
by step) for non-javadoc comments and also Javadoc style comments for
the API docs
Details:
1) Javadoc comments are a bit formal sounding, need to be factually
correct, and comprehensive of special cases. They are for long-term
reference of others.
2) Non- javadoc comments should preferably be informal, busy, and
light-sounding - preferably, only those things that are not obvious
from the code. At the beginning of a block of more than 50 lines, a
comment of size >= 5% of total code block(5 lines at least for 100
lines of meaningful code) is due if your code is to be called
well-commented. Better 10%. One comment every 10-15 meaningful lines
(not bracket-only lines and not System.out.println lines) is optimal
and good-looking. Note that the person reading your code should get
guidance in his thought process just as soon as he asks for it in his
mind or just before he needs it- if he gets the direction of the code
before reading the actual code, its fairly good commenting. Try to
guide your reader along. Use (a)tutor-like,
(b)trainer-like,(c)co-programmer-like language.
Use a conversational style.
3) Flow commenting means informal language and step by step treatment
of the logic.
It is a good idea to write all javadoc comments later, after coding and
testing and debugging, but it is imperative to write non-javadoc
comments while coding. Else, your commenting will not be high-quality
or sometimes will not even be accurate.
Read the article on Literate programming at Wikipedia
(http://en.wikipedia.org/wiki/Literate_programming)
and
this article on documentation
(http://en.wikipedia.org/wiki/Software_documentation)
In the ideal situation, your source code file should be the best manual
for another programmer. And the javadoc must look as good (or bad) as
Sun's JDK docs. User documentation is an altogether different ball
game. For that see the other blog.
Now, sometimes the code is so big or complicated that you simply cannot
put in comments and make the source file impossible to assimilate. Here
your code speaks for itself.
The following will be useful in such a scenario as also in general:
1) lucid, self-explanatory naming of variables e.g. txtTelNo or
cmbSpecialHolsList
2) good, consistent indentation (4 spaces/tab is accepted by all as
being easy on the eyes)
Some editors and IDEs (Notepad++ and Eclipse for instance) have
code-folding - they are lifesavers when it comes to files greater than
100 lines of code.
3) clean, simple programming - no complex for statements, multiple
initialisations, etc.
I insist that you do a google search for Good commenting practice and
good programming style.
In summary, you should know:
a) how to code in a flow-commenting style (conversational)
b) how to code with javadoc-comments
c) how to code without any comments at all
d) when to do which, out of (a), (b) and (c)
Happy commenting!
Must-see's:
http://filia.uni.lodz.pl/mocny/doc/j2sdk/docs/tooldocs/solaris/javadoc.html
http://java.sun.com/j2se/javadoc/writingdoccomments
http://barney.gonzaga.edu/java/tooldocs/javadoc/standard-doclet.html
Commenting in Java
Basics:
1) Javadoc comments are for permanent API documentation
2) Non-Javadoc comments are for the programmer to read and understand
things that are not so obvious from the code.
3) Professional programmers write flow commenting style (informal step
by step) for non-javadoc comments and also Javadoc style comments for
the API docs
Details:
1) Javadoc comments are a bit formal sounding, need to be factually
correct, and comprehensive of special cases. They are for long-term
reference of others.
2) Non- javadoc comments should preferably be informal, busy, and
light-sounding - preferably, only those things that are not obvious
from the code. At the beginning of a block of more than 50 lines, a
comment of size >= 5% of total code block(5 lines at least for 100
lines of meaningful code) is due if your code is to be called
well-commented. Better 10%. One comment every 10-15 meaningful lines
(not bracket-only lines and not System.out.println lines) is optimal
and good-looking. Note that the person reading your code should get
guidance in his thought process just as soon as he asks for it in his
mind or just before he needs it- if he gets the direction of the code
before reading the actual code, its fairly good commenting. Try to
guide your reader along. Use (a)tutor-like,
(b)trainer-like,(c)co-programmer-like language.
Use a conversational style.
3) Flow commenting means informal language and step by step treatment
of the logic.
It is a good idea to write all javadoc comments later, after coding and
testing and debugging, but it is imperative to write non-javadoc
comments while coding. Else, your commenting will not be high-quality
or sometimes will not even be accurate.
Read the article on Literate programming at Wikipedia
(http://en.wikipedia.org/wiki/Literate_programming)
and
this article on documentation
(http://en.wikipedia.org/wiki/Software_documentation)
In the ideal situation, your source code file should be the best manual
for another programmer. And the javadoc must look as good (or bad) as
Sun's JDK docs. User documentation is an altogether different ball
game. For that see the other blog.
Now, sometimes the code is so big or complicated that you simply cannot
put in comments and make the source file impossible to assimilate. Here
your code speaks for itself.
The following will be useful in such a scenario as also in general:
1) lucid, self-explanatory naming of variables e.g. txtTelNo or
cmbSpecialHolsList
2) good, consistent indentation (4 spaces/tab is accepted by all as
being easy on the eyes)
Some editors and IDEs (Notepad++ and Eclipse for instance) have
code-folding - they are lifesavers when it comes to files greater than
100 lines of code.
3) clean, simple programming - no complex for statements, multiple
initialisations, etc.
I insist that you do a google search for Good commenting practice and
good programming style.
In summary, you should know:
a) how to code in a flow-commenting style (conversational)
b) how to code with javadoc-comments
c) how to code without any comments at all
d) when to do which, out of (a), (b) and (c)
Happy commenting!
Must-see's:
http://filia.uni.lodz.pl/mocny/doc/j2sdk/docs/tooldocs/solaris/javadoc.html
http://java.sun.com/j2se/javadoc/writingdoccomments
http://barney.gonzaga.edu/java/tooldocs/javadoc/standard-doclet.html