May 20, 2005The Art of CommentingMike Clark wrote an interesting piece on comments, and I have a few things to contribute as well. Here are some of the practices I follow with regards to comments:
These are all more or less personal preferences, but there is one rule that think should be applied at all times:
This is not easy. There is some kind of unsaid rule in the software development world that somehow makes it hard for people to follow this rule. Think about it: how would you feel if you see a commit log indicating that someone modified a file that you created and all they did was adding comments? Chances are you will take it a bit personally... "Is she trying to tell me my code isn't clean? Or that my comments are not good enough?". You need to get over it. There are many reasons why people other than you adding comments to your code is a Good Thing:
So next time it happens, don't take it personally, take a close look at the new comments and if they are not to your liking, work with the person to improve them. And of course, feel free to return the favor, and if everybody does their part, your code base will end up being easier to read and maintain. Posted by cedric at May 20, 2005 12:01 PM Comments
I like to add comments for closing braces: My first CS prof in college actually required this and would doc you if you didn't. I abhorred the practice at the time, but I do it now two if the block runs on for more than 20 lines (a screenish) or so. Posted by: cooper at May 20, 2005 01:18 PMCooper, you're seeing another smell there: overly long blocks. As Mike points out, commenting closing braces is nearly always the sign of a method that's too long. (Though complicated mathematical algorithims may well be a different kettle of fish). One thing I do with my Eclipse comment configuration is put the words "TODO" in - that way, it shows up in the various lists of tasks. Posted by: Robert Watkins at May 21, 2005 12:44 AMI think your comment is much better than the orginal article, that one just contain common practice with available everywhere. But your comment is really your idea and experience, I like this much more~ Posted by: Carfield Yim at May 21, 2005 11:18 AM>>Cooper, you're seeing another smell there: overly long blocks. As Mike points out, commenting closing braces is nearly always the sign of a method that's too long. (Though complicated mathematical algorithims may well be a different kettle of fish). You know, I think that is really an oversimplification, and not necessarily true. Frankly, if you have a 15 or 20 step proceedure, I would rather see it laid out in one method where it is clear what you are doing. Personally, I think there is a lot of "method propogation" that goes on in the name of this that makes code much harder to follow. Secondly, 20 lines is easy to hit when you are looking at code at my office in gnu style. public void foo() } Again, a pretty short and mostly control structures, but still long enough that by the end of the method, you might be wondering where the for() block ends. Posted by: cooper at May 22, 2005 05:57 PMHmmm you want some help with yours comments, just use Commentator: http://www.cenqua.com/commentator/ ;-) Posted by: Tugdual Grall at May 22, 2005 07:36 PMWell, I never know who uses UBB-ish stuff and who used HTML :P The line count in the above is all I really cared about though, so I think it still gets the point across. Posted by: cooper at May 23, 2005 08:04 AMAs a general rule I always try to stay away from closing-brace comments. Comments tend to fall out of sync pretty quickly, even more so when they are on a closing brace, and even more so when they are on a closing brace of a bigger code block. I have been tricked more than once by comments that told me I was looking at the closing brace of an "if (cond)" when really it was the end of a while loop with a different condition. DRY totally applies in this situation especially when even the tools can't help you catch the problem. Posted by: Vincent Mallet at May 25, 2005 07:45 AMCedric, The Eclipse template is a very good idea. I don't like to put comments in the closing brace because I think it's not necessary when I have only a few lines of code between braces and if I have a lot of code between the braces I do some refactoring to resolve the problem. I really appreciate to comment blocks of code, but in the last few months I have used methods with descriptive names despite block comments (//) and my experience have been very nice. Sample: public void foo() { The above code changes to: public void foo() { I agree with Mr. Watkins. If you need to comment your closing braces, then you have a problem with your code. I use to follow this practice in my C days but you shouldn't need it in an OO Java application except possibly in some unusual (read: rare) algorithms. Posted by: Stock at June 2, 2005 10:30 AMI think people comment too much. That's right, too much. Why do you comment? Because the code is too messy/complicated. Instead of wasting time writing comments to explain just make your code better. Unfortunately I think it's one of those things where if you comment people automatically assume you're doing a good thing. Posted by: Tinou Bao at June 2, 2005 10:47 PM"I like to add comments for closing braces" As many people already said, my experience says, that this is a pretty bad practice that does not live with time and does not support team development. I have seen this kind of practice with overly long and convoluted code..and after a year it was a mix of commented and uncommented brace. Such comments clutter the code a lot. With today's IDE you get brace highlighting for free you don't need to comment that, otherwise you may well invent a language with named blocks. I have listen to my first mentor that taught me to write methods with no more than 20-30 LOC Posted by: Someone at June 21, 2005 01:47 PMhttp://chase-home-finance.soignio.info chase home finance chase home finance [url=http://chase-home-finance.soignio.info ]chase home finance[/url] http://acid-reflux-diet.monyar.info acid reflux diet [url=http://acid-reflux-diet.monyar.info]acid reflux diet[/url] http://mobile-phone-games-download.eoe1o.info mobile phone games download [url=http://mobile-phone-games-download.eoe1o.info]mobile phone games download[/url] tfngkzey zmrwnftc http://ugbpouki.com jkhogpjb crtdcxlc [URL=http://fsskkcvk.com]hziaarai[/URL] Posted by: aelwogpe at June 12, 2007 11:42 PMprgnrbqj [URL=http://yoeumfkf.com]xjezemro[/URL] kiudebhu http://oyyklhxb.com ryfdskgt hoeygatc Posted by: ubrssivt at June 12, 2007 11:46 PMvyziva | [url=http://ninepa.com]zexava[/url] | [link=http://cusihe.com]nenuzo[/link] | http://xuxuxi.com | huzehe | [http://pirixe.com cuhiwi] Posted by: nthuijgjst at July 31, 2007 11:24 AMPost a comment
|