I want to hear from you any advice and experience of writing comments in your code. How do you write them in the most easy and informative way? What habits do you have when commenting parts of code? Maybe some exotic recommendations?
I hope this question will collect the most interesting advices and recommendations forcommenting, something useful that everyone can learn from.
OK, I will start.
-
Usually, I don't use
/* */
comments even when I need to comment many lines.Advantages: code visually looks better than when you mix such syntax
with one-line comments. Most of IDEs
have an ability to comment selected
text and they usually do it with
one-line syntax.Disadvantages: Hard to edit such code without IDE.
-
Place "dot" in the end of any finished comment.
For example:
//Recognize wallpaper style. Here I wanted to add additional details int style = int.Parse(styleValue); //Apply style to image. Apply(style);
Advantages: Place "dot" only in comments that you finished. Sometimes you can write temporal information, so lack of "dot" will tell you that you wanted to return and add some additional text to this comment.
-
Align text in the enumerations, commenting parameters etc.
For example:
public enum WallpaperStyle { Fill = 100, //WallpaperStyle = "10"; TileWallpaper = "0". SizeToFit = 60, //WallpaperStyle = "6"; TileWallpaper = "0". Stretch = 20, //WallpaperStyle = "2"; TileWallpaper = "0". Tile = 1, //WallpaperStyle = "0"; TileWallpaper = "1". Center = 0 //WallpaperStyle = "0"; TileWallpaper = "0". };
Advantages: Just looks better and visually more easy to find what you need.
Disadvantages: Spending time to align and harder to edit.
-
Write text in comment that you can't obtain by analyzing code.
For example, stupid comment:
//Apply style. Apply(style);
Advantages: You will have clear and small code with only useful information in comments.
Best Answer
Some of the statements below are quite personal, though with some justification, and are meant to be this way.
Comment Types
For the brief version... I use comments for:
Read below for the details and (possibly obscure) reasons.
Trailing Comments
Depending on the language, either using single-line comments or multi-line comments. Why does it depend? It's just a standardization issue. When I write C code, I favor old-fashioned ANSI C89 code by default, so I prefer to always have
/* comments */
.Therefore I would have this in C most of the time, and sometimes (depends on the style of the codebase) for languages with a C-like syntax:
Emacs is nice and does that for me with
M-;
.If the language supports single-line comments and is not C-based, I will be more enclined to use the single-line comments. Otherwise, I'm afraid I've now taken the habit. Which isn't necessarily bad, as it forces me to be concise.
Multi-Line Comments
I disagree with your precept using single-line comments for this is more visually appealing. I use this:
Or this (but I don't that often any more, except on a personal codebase or mostly for copyright notices - this is historical for me and comes from my educational background. Unfortunately, most IDEs screw it up when using auto-format):
If need really be, then I would comment inline using what I mentioned earlier for trailing comments, if it makes sense to use it in a trailing position. On a very special return case, for instance, or to document a
switch
'scase
statements (rare, I don't use switch often), or when I document branches in anif ... else
control flow. If that's not one of these, usually a comment block outside of the scope outlining the steps of the function/method/block makes more sense to me.I use these very exceptionally, except if coding in a language without support for documentation comments (see below); in which case they become more prevalent. But in the general case, it really is just for documenting things that are meant for other developers and are internal comments that really need to really stand out. For instance, to document a mandatory empty block like a "forced"
catch
block:Which is already ugly for me but I would tolerate in some circumstances.
Documentation Comments
Javadoc & al.
I'd usually use them on methods and classes to document versions introducing a feature (or changing it) especially if that's for a public API, and to provide some examples (with clear input and output cases, and special cases). Though in some cases a unit case might be better to document these, unit tests are not necessarily human readable (no matter what DSL-thingy you use).
They bug me a bit to document fields/properties, as I prefer trailing comments for this and not all documentation generation framework support trailing documentation comments. Doxygen does, for instance, but JavaDoc doesn't, which means you need a top comment for all your fields. I can survive that though, as Java lines are relatively long anyways most of the time, so a trailing comment would creep me out equally by extending the line beyond my tolerance threshold. If Javadoc would ever consider improving that, I'd be a lot happier though.
Commented-Out Code
I use single-line for one reason only, in C-like languages (except if compiling for strict C, where I really don't use them): to comment-out stuff while coding. Most IDEs will have toggle for single-line comments (aligned on indent, or on column 0), and that fits that use case for me. Using the toggle for multi-line comments (or selecting in middle of lines, for some IDEs) will make it harder to switch between comment/uncomment easily.
But as I'm against commented-out code in the SCM, that's usually very short lived because I'll delete commented-out chunks before committing. (Read my answer to this question on "edited-by in line comments and SCMs")
Comment Styles
I usually tend to write:
A note on Literate Programming
You might want to get interested in Literate Programming, as introduced in this paper by Donald Knuth.
As a side note and example: The underscore.js JavaScript framework, notwithstanding non-compliance with my commenting style, is a pretty good example of a well-document codebase and a well-formed annotated source - though maybe not the best to use as an API reference).
These are personal conventions. Yes, I might be weird (and you might be too). It's OK, as long as you follow and comply to your team's code conventions when working with peers, or do not radically attack their preferences and cohabitate nicely. It's part of your style, and you should find the fine line between developing a coding style that defines you as a coder (or as a follower of a school of thought or organization with which you have a connection) and respecting a group's convention for consistency.