There are many valid reasons for MethodCommenting. Making unclear code more clear should not be one of them. The ExtremeWay is to TreatCommentsWithSuspicion: consider the ''code'' guilty until proven innocent, if a comment seems to be needed to make it clear. When a comment is needed ''for clarity'', the ExtremeWay is to improve the code until, ideally, it no longer needs a comment. Understand what it is ToNeedComments. ''Note that if, when, where, and how to comment is a controversial topic on this wiki. You are invited to consider multiple viewpoints.'' ---- From Kernighan And Plauger, ElementsOfProgrammingStyle, 1978 : Do not comment bad code, rewrite it. -- AamodSane ---- It's not always possible to rewrite unclear code. Examples: * Some steps need to be executed in a nonobvious order, because the obvious order is significantly slower. (If you don't comment such code, often you'll be the first to shake your head about it and change it back to the slower way :/ ) * You must deal with an unusual interface, e.g. an iterating function starting (conceptually) at -1 or 2. See Microsoft's FindFirstFile/FindNextFile functions and the C''''''File''''''Find class. ''Try creating an enum for First''''''XXX rather than hard coding the value and then commenting it. There are lots of coding solutions that can be applied before falling back onto comments.'' Now you're going to need to put the same comment for the enum; the comment still says something you can't say in code (without being absurd). ''Try the enum approach and see if it is even possible to write a comment without duplicating what is written in the code.'' * You are using a new, unfamiliar idiom/pattern. You yourself might recognize it, but what about the unfortunate fellow programmer seeing this way-to-do-it the first time in his or her life? ''Why should the reader of the class care where you got the pattern for the code? If the pattern is making the code complex, don't use it!'' More general: What is clear as glass used for fiber channels to you, may puzzle the programmer using/extending your code. So 'unclear' depends on what someone knows and is used to. ''Don't assume everybody is a crack.'' -- FreddyTheCat ---- Sure, there may be exceptions. The point remains - more comments can be removed than most people realize. Making the code good is better than explaining it. Focus on getting that benefit. When you just can't get rid of the comment, and your partner can't either ... then write us and ask for help. If we can't help, then put in a comment. ;-> Explaining the programming language isn't one of the exceptions. It's OK to assume competence but not wizard status. On the other hand, using the most obscure form of the language isn't OK either. If the idiom is important and not uncommon, use it. Give the method a decent name, sort of like (someone correct my code, I'm just typing this in) void copy''''''To''''''Null (char* In, char* Out) { while( *Out++ = *In++ ); } Similarly, let the code say that it's optimized: export''''''Employees employees do: [ :each | each export''''''Optimized ] Employee>>export''''''Optimized self export''''''Personal''''''Buffered. self export''''''Dollars''''''Buffered. Employee>>export''''''Personal''''''Buffered | buffered''''''Person''''''Data | buffered''''''Person := self get''''''Personal''''''Data. buffered''''''Person export''''''Name. buffered''''''Person export''''''Address. buffered''''''Person export''''''Phone. Employee>>export''''''Dollars''''''Buffered etc ... I'd suggest that something like this makes it clear you don't want to screw with the code. -- RonJeffries ---- Bjarne Stroustrup says in "The C++ Programming Language" (3rd Ed., Chapter 6.4): : ''If something can be described in the language itself, one should do that, and not just mention it in a comment.'' This is where I fervently agree with you. Still there ''are'' things that can't be clearly expressed in the programming language, but need to be said. This is where comments can't be spared. To pick up your code example, the "optimized" in export''''''Optimized is ambiguous. It can refer to the action done, or to the way the code is written. The latter usage smells, like HungarianNotation, and is exposing implementation details. If I see a name like export''''''Optimized, I assume the function exports something in an optimized form (''e.g.'' a database without deleted records), I ''don't'' recognize it as a warning not to meddle with the code. See also '''Clear Warning''' in MethodCommenting. I don't want to duplicate MethodCommenting and ToNeedComments here. Many caveats about TreatCommentsWithSuspicion that are in my mind now are expressed there, in the patterns at the beginning. Just now I stumbled over a possible reason as to ''why'' I oppose TreatCommentsWithSuspicion and "Do not comment bad code, rewrite it.": Both statements are negative. How about '''"If you can turn a comment into source code, do it."'''? This guideline is based on the Stroustrup quote, but goes farther, as the quote doesn't say anything about removing the comment. With my proposed guideline, there is less danger of losing information when removing comments, a danger I see with the more urgent statements above. But maybe the sense of urgency lies within me, because I am one of those rule-obeying Krauts :). A good partner for '''"If you can turn a comment into source code, do it."''' would be: '''"If you can make code clearer, do it. Even if you just add a comment."'''. This also goes with NanoRefactorization. -- FreddyTheCat I like the positive form. The partner rule I cannot support. It's easier to add a comment than it is to fix the code, but fixing the code is the right thing to do whenever possible. I would choose not to recommend the easy but wrong fix. Prefer telling people they can't comment unless all else fails ... because it rarely does. -- RonJeffries ------ I usually use a multi-phase approach. Every time there's some code that's unclear, I comment it. Then, every time I run across a comment, I see if there's an ExtractMethod or renaming I could do to make the code say exactly the same thing as the comment. Then once I get code that looks like this // Load register state for current process currentProcess.loadRegisterState(); I remove the duplication by deleting the comment (unless I have a professor that grades based on number of comments...yes, I've had some. ;)). I tend to get better naming and more SelfDocumentingCode this way, because I'm writing first for the human and then turning the human code into something the machine can read. It's the same reasoning behind MakeItWorkMakeItRightMakeItFast. Focus on achieving the goal (clarity) first, and then refactor so there's no duplication. -- JonathanTang ------ ''Almost'' every sort of collection in Java is 0-based. The notable exceptions are the JDBC API and Record stores. Every time I have cause to iterate or select by index in one of its one-based collections, I comment it. Mostly to remind myself that these are the exceptional cases and I can't be sloppy. -- StevenNewton ''(and KarlKnechtel)'' ------ See also: SelfDocumentingCode, TreatNamesWithSuspicion, TreatCodeWithSuspicion, DocumentationBeyondTheSourceCode, CommentingChallenge ---- Are comments a SignOfInsecurityInCode?