The Problem With Comments
blog.zacharyvoase.com
blog.zacharyvoase.com
And by "you" I mean whomever wrote the code I'm looking at. Usually my past self.
I think what I'm saying is that the problem with comments is semantics, not UX.
Another cause is paranoid architects. Lint-testing to ensure even one-line getters and setters are commented. Come on! But it happens in real life.
Whys are usually highly context-dependent and you should not include all the context into the comment ("in 2004 we benchmarked A,B and C on such and such a machine and B was fastest"). If it is not context-dependent the solution is obvious and needs no comment.
Going through a few files on one of my codebases, here's some of the useful comments that I can find from a quick skim:
- Explaining why a dummy argument existed on a method (a third-party library required it)
- Explaining why we need to set a weird inheritance column on an ActiveRecord model (we're using 'type' as an actual column)
- Calling out some technical debt that could be fixed by someone at some point.
All of these are really about calling out why the solution is not obvious.
I had written some code that was hard to understand. So I wrote a comment to explain how it was supposed to work. I come back a couple months later, and guess what? The comment might have made complete sense to the guy who just wrote the code, and therefore already understood how it worked. But to someone who didn't it was completely opaque, and therefore only served to make me more irritated the person who wrote it. (i.e., me a couple months ago.)
So that's why I don't like that style of comments. If the code alone doesn't make sense, then 90% of the time the comments that try to explain it don't make any sense either. Maybe the only situation where this kind of comment is acceptable is if you have to use some hairy dynamic programming algorithm or something like that, in which case the comment should just be the name of the algorithm and a URL for a page that explains it.
The real reason comments are de-emphasised is not because someone thinks they're less important, but because you use code in two different modes. In one mode, you're getting an overview and might want comments for that. In another mode, you're working on the code and are so immersed in it, you don't need the comments. That's why many environments support comment expanding/collapsing.
There are more sophisticated things imaginable for code representation, like where it is held in a relational DB or something, but simple flat text files tend to work best, so we're stuck with the reality of having two show a dumb sequence of characters in these two different modes. So to me, code is like those pictures with two different meanings ("Boring Figures"). You choose to see it with or without the comments. And de-emphasised text works for that.
When I'm working on a piece of code I want the comments to fade quietly into the background.
Some sort of toggle might be useful though. Finish editing your code, hit key combo, comments take centre stage, check they still make sense, return to usual state.
Being able to bind this to certain actions such as save or build might be useful too.
But not in my face all the time please.
In the article, I have some doubts about the usefulness of the comments of the second example, it is almost paraphrasing the code.
When I was developing, the number of lines of code, comments, blank were computed. Projects having this kind of "quality" indicators had generally a lot of annoying comments.
I recently began experimenting with a coding style in which blocks of more than 5 lines get refactored into methods with self explanatory names and arguments and so far it's working quite well. Code reads like prose[2]. Couple that with proper TDD, and comments get very rare indeed. Those that remain are there for very good reasons and yes, should probably be displayed in a very contrasting color by the IDE. As far as I can tell, Uncle Bob is right.
[1] http://butunclebob.com/ArticleS.TimOttinger.ApologizeIncode
[2] http://www.cleancoders.com/codecast/clean-code-episode-5/sho...
Or the language, either programming lang or nature lang, is a fail by design?
There are cases where I disagree. For example, TODO and FIXME annotations cannot be always replaced by something like NotImplementedException. Sometimes code is so concise that it is not obvious why it solves the problem. Sometimes it is good to state why an "obviously better solution" is not applicable in a special case.
[0] https://github.com/MatzeB/libfirm/blob/master/ir/opt/cfopt.c...
It's much better to me if things that are conceptually linked are chunked together. I don't mind scrolling. I don't mind reading. I hate hunting when I am trying to crystallize ideas.
The exception is if there are reusable methods involved. These I always factor out.
AFAIK the way to fix bad or stale comments is do it during code review.
For example, what about docstrings? That is, comments that contain documentation that is supposed to be sufficient even if the code is not available? I daresay those should not be emphasized.
Or what about instructional code that is written to be read by people with less than perfect knowledge of the programming language or problem domain?
In the end, I think highlighting keywords like TODO, FIXME or NOTE in comments would be a better solution.
And I still don't think that solves your "problem". Even if they appear brighter colours, then it's still very easy to phase them out so that you're just focused on the code. It's only when the comment colours begin to hurt your eyes that I find it distracting.
Recently I've noticed, that I comment less, and instead write more information in commit messages, and do svn blame more often.
It's better, because it has more context (you see to which changes this comment is relevant), and is always up-to-date, because you always write fresh commit messages.
We have a rule in company, that you have to add task number from JIRA to each commit message, and this alone is a huge improvement - you can always check WHY some change was made).
I can't really see the benefit of hunting the comments from your code with cursor. It definitely would make it even more unlikely that comments aren't updated when code is changed.
I have always been a fan of syntax highlighting (as long as I have been aware of it - my Speccy lacked it). I am not aware of any negatives.