That is a view that /sounds/ right - after all, if some documentation or
commenting is good, then surely more is better?
However, the reality is different. Documents and comments need to be
written and maintained - but they don't contribute directly to the
working of the code. So their maintenance is invariably given lower
priority than the code, and they quickly become something to fix when
you have time, rather than an integral part of development. They get
more and more separated from the reality of the code, and become
confusing, outdated, or even completely wrong. At this point they are
highly counter-productive.
Compare these two code snippets for scaling an analogue measurement from
a weighing system:
// Convert raw analogue input value into a weight measurement
// The reference weight is 5 kg, and it gives a reading of 4 volts.
double scale(double x) {
return x * 1.25;
}
vs.
double adc_to_weight_in_kg(double raw) {
const double referenceWeight = 5.0;
const double referenceReading = 4.0;
return x * referenceWeight / referenceReading;
}
What happens to the code when a more accurate check of the reference
weight gives 4.02 volts, found just before the system goes live? The
code gets changed to:
// Convert raw analogue input value into a weight measurement
// The reference weight is 5 kg, and it gives a reading of 4 volts.
double scale(double x) {
return x * 1.243781095;
}
vs.
double adc_to_weight_in_kg(double raw) {
const double referenceWeight = 5.0;
const double referenceReading = 4.02;
return x * referenceWeight / referenceReading;
}
Which do you prefer now - the one with the data in the code, or the one
with the comments?
You /could/ argue that you can have the data and names in the code,
/and/ comments. But then your final version would not be much better:
// Convert raw analogue input value into a weight measurement
// The reference weight is 5 kg, and it gives a reading of 4 volts.
double adc_to_weight_in_kg(double raw) {
const double referenceWeight = 5.0;
const double referenceReading = 4.02;
return x * referenceWeight / referenceReading;
}
Having /no/ comments is clearer, safer, and easier to maintain than
having the comments.
Of course, the correct balance between code and comments (or other
documentation) is going to vary significantly depending on the project,
its lifetime, its complexity, the development team, and many other
factors. I am not giving a "one size fits all" answer here - I am just
suggesting that it is something to think about, and comments are not
necessarily a helpful thing.
(No comments, /and/ poor choice of names and no indication of what is
going on is also a very bad idea - but I think everyone agrees on that!)
>>> And, to be consistent with other documentation in the project, by
>>> having such documentation here, it makes it an easy task to see
>>> what's what quickly.
>>>
>>> I like Doxygen. I think it's a little obtuse in some cases, but
>>> what it provides is great. I have used such documentation rather
>>> heavily on Java-based apps where it was built-in. I've used it
>>> rarely in other software.
>>
>> I like doxygen too. As well as for commenting my own code, I have found
>> it useful for analysing existing code - set it up to document
>> /everything/ regardless of comments, and include caller, callee and
>> include graphs. It makes it easy to navigate around the code, see
>> cross-references, find messy parts, etc., using just a browser.
>>
>> But what I don't like is when there is a documentation standard that
>> says you need big sections of doxygen commenting (or any commenting, for
>> that matter) for every little function or bit of code, so that you can't
>> find the real code in all the commentary.
>
> Yes. Yours is one viewpoint. I've also worked with/for people who
> like to see documentation over code, because then even managers can
> follow along with what it's doing.
>
I am not arguing against comments or documentation - I am arguing
against /unnecessary/ comments and documentation.