by wool_gather · 5y ago · 0 comments
A data field called "imageURL" that's a string -- is that a remote or local URL? Where did it come from/how much can I trust that it's well-formed? (And why isn't it already an actual `ParsedURL` type?) Another datatype has three fields "name", "username", and "id", all strings, with no explanation of their purpose or differences.
This means that I either have to track down the teammate that wrote the code and ask them every single time I run into something like this. Or I have to execute the whole program and reverse engineer the answer. Both are colossal wastes of time compared to a few "useless" or "repetitive" comments.
//Assign 5 to the val called a
val a = 5;
// Chosen by fair die roll. Completely random
const kEschatonThreshhold = 5;
val a = kEschatonThreshhold;For me comments should concentrate on the why and maybe the how but mostly ignore the what. That should be obvious from the code.
Do you think that's possible for every field in all, or even most, applications and languages? I do not. And so I don't think we should be allergic to a few wasted sentences of prose.
Put that information in your types and (i) you don't need to write those comments and (ii) the compiler can help you check if you haven't made mistakes.
By that logic, the code is already written and doesn't include that information in its comments, either.
> if the person who wrote the code had written a doc comment, I would know [the invariants].
If you're going to ask that the person who wrote the code have written it differently, there's (in general, at least) no reason to ask for comments in preference to types.
string imageURL; // Url of an image
To a point. Believe me, there are levels of repetition you would find less tolerable than a lack of documentation. Here's a real world example: we had a bunch of classes with lots of attributes. The getters and setters for those attributes were automatically generated (with a macro), because we had lots and lots of them. In the headers however, we wrote the prototypes in full, so the tools could find them more easily:
Foo getFoo();
Bar getBar();
Baz getBaz();
void setFoo(const Foo &foo);
void setBar(const Bar &bar);
void setBaz(const Baz &baz);
So far so good. Now documentation is an important thing. So important in fact that we had to document every single method in all classes. And since we were using Doxygen, we were a bit constrained. Foo getFoo(); ///< Get foo
Bar getBar(); ///< Get bar
Baz getBaz(); ///< Get baz
void setFoo(const Foo &foo); ///< Set foo
void setBar(const Bar &bar); ///< Set bar
void setBaz(const Baz &baz); ///< Set baz
OK that's redundant, not too bad, and QA is happy now. Well, no. First, we are supposed to be using Javadoc style documentation. Second, we need to document every arguments, and the return values. How are we supposed to understand a getter if we don't document its return value? /**
* Get foo
*
* @return foo
*/
Foo getFoo();
/**
* Get bar
*
* @return bar
*/
Bar getBar();
/**
* Get baz
*
* @return baz
*/
Baz getBaz();
/**
* Set foo
*
* @foo The new Foo
*/
void setFoo(const Foo &foo);
/**
* Set bar
*
* @bar The new Bar
*/
void setBar(const Bar &bar);
/**
* Set baz
*
* @baz The new Baz
*/
void setBaz(const Baz &baz);
And so on, often for 20 attributes instead of just 3. I am not even kidding. Now we could say it's just a matter of space, but it isn't: sometimes, there was an attribute that was a bit special, and the comment reflected that: /**
* Set wiz
*
* @baz The new Wiz. Must not be Merlin.
*/
void setWiz(const Wiz &wiz);
Except the additional comment could as well be absent, because it was totally lost in the noise. I've missed several cogent pieces of information that way.Worse, the tech lead, who acknowledged that this practice was useless at best, and had the clout to have this restriction relaxed, refused to do so. To this day I'm not sure why. I have a couple hypotheses (plain inertia, don't fix what's not blatantly broken, don't want to argue with QA, afraid of regulation…), but nothing solid.
If I had to chose between no comment at all and that, I'm not sure I would chose that. Sure, I would lose valuable comments, but if I already lost them because of sheer noise, I didn't lose much at all.
And the reason is that you're not always just reading the header. When I'm looking at a `setWiz` call site, my IDE can pop up just that one docstring and save me a bunch of aggravation.
You have to actually notice it for it to matter. A missed comment is no better than an unwritten comment.
> When I'm looking at a `setWiz` call site, my IDE can pop up just that one docstring and save me a bunch of aggravation.
Didn't know about that. My IDE never did that (I used Visual Studio at some point, and now QtCreator).