A coworker of mine believes that any use of in-code comments (ie, not javadoc style method or class comments) is a code smell. What do you think?
|
locked by Yannis Rizos♦ Nov 27 '12 at 20:12
This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: FAQ.
closed as not constructive by Yannis Rizos♦ Nov 27 '12 at 20:11
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or specific expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, see the FAQ for guidance.
|
Only if the comment describes what the code is doing. If I wanted to know what was happening in a method or block, I would read the code. I would hope, anyway, that any developers working on a given project were at least familiar enough with the development language to read what is written and understand what it is doing. In some cases of extreme optimization, you might be using techniques that makes it difficult for someone to follow what your code is doing. In these cases, comments can and should be used to not only explain why you have such optimizations, but what the code is doing. A good rule of thumb would be to have someone else (or multiple other people) familiar with the implementation language and project look at your code - if they can't understand both the why and the how, then you should comment both the why and the how. However, what's not clear in the code is why you have done something. If you take an approach that might not be obvious to others, you should have a comment that explains why you made the decisions that you did. I would suspect that you might not even realize that a comment is needed until after something like a code review, where people want to know why you did X instead of Y - you can capture your answer in the code for everyone else who looks at it in the future. The most important thing, though, is to change your comments when you change your code. If you change an algorithm, be sure to update the comments with why you went with algorithm X over Y. Stale comments are an even bigger code smell. |
|||||||||||||||||||||
|
|
This is particularly irritating to hear at the moment, I spent some time this weekend looking at very well-named, very clean, uncommented code implementing a research algorithm (one that isn't actually published). I'm high-level familiar with it, the guy sitting next to me was the inventor, and the code was written a few years ago by someone else. We could barely follow it. Your co-worker is not experienced enough, obviously. |
|||||||||||||||||||||
|
|
Comments should explain why, not how.
Before:
after:
|
|||||||||||||||||||||
|
|
I declare your co-worker a heretic! Where are my heretic-burnin' boots? Obsessive commenting is bad and a maintenance headache, and comments are no replacement for well-named methods, classes, variables, etc. But sometimes putting why something is the way it is can be immensely valuable for the poor idiot who has to maintain the code in six months -- particularly when that poor idiot winds up being you. Some actual comments from the code I'm working on:
|
|||||||||||||||||
|
|
Ideally, code should be so well coded that it should be auto explicative. In the real world, we know that also very high quality code needs sometimes commenting. What you should absolutely avoid is "comment-code redundancy" (comments that don't add anything to code):
Then, if there's a good (and maintained/aligned) code design and documentation, commenting is even less useful. But in some circumstances comments can be a good aid in code readability:
Don't forget that you have to maintain and keep in sync also comments... outdated or wrong comments can be a terrible pain! And, as a general rule, commenting too much can be a symptom of bad programming. |
|||||||||||||||||||||
|
|
Categorically defining a method or process as a "code smell" is a "zealotry smell". The term is becoming the new "considered harmful". Please remember that all of these sort of things are supposed to be guidelines. Many of the other answers give good advice as to when comments are warranted. Personally I use very few comments. Explain the purpose of non-obvious processes and leave the occasional death-threat to anyone that might be considering altering things on their own that required weeks of tuning. Refactoring everything until a kindergartner can understand it is likely not an efficient use of your time, and probably will not perform as well as a more concise version. Comments don't affect run time,so the only negative issue to consider is the maintenance. |
|||||||||||||
|
|
In some cases, no amount of good naming, refactoring etc. can replace a comment. Just look at this real-world example (language is Groovy):
Looks strange, doesn't it? Probably a copy-paste-error? Cries for a bugfix? Now the same with comments:
|
|||||
|
|
The primary issue here is the meaning of the term "code smell". Many people (including you, I think) understands a code smell to be something close to an error or at least something that needs to be fixed. Perhaps you think of it as a synonym to "anti-pattern". This is not the meaning of the term! The code smell metaphor originates from Wards Wiki, and they stress:
So what does it mean that comments are a code-smell: it means that when you see a comment, you should pause and think: "Hmmm, I sense a hint that something could be improved". Perhaps you can rename a variable, perform the "extract method"-refactoring -- or perhaps the comment is actually the best solution. That is what it means for comments to be code smells.EDIT: I just stumpled upon these two articles, which explains it better than me: |
|||||||||
|
|
I think the rule is quite simple: imagine a complete stranger seeing your code. You probably will be a stranger to your own code in 5 years. Try to minimize the mental effort to understand your code for this stranger. |
|||||||||||||
|
|
A good idea to have the right comments is to start with writing comments.
This allows you to extract methods easily to even get rid of the comments,
For things that can't be separated just don't extract methods and type the code under the comments. This is what I see as an useful way to keep commenting to a minimum, it's really useless to go commenting each line... Only document a single line if it's about a magic value initialization or where it makes sense. If parameters are used too much, then they should be private members in your class. |
|||||
|
|
I think the answer is the usual "It depends" one. Commenting code just to comment code is a smell. Commenting code because you're using an obscure algorithm that's an order of magnitude faster saves the maintenance programmer (usually me 6 months after I wrote it) half a day of poking through the code to determine what it's doing. |
||||
|
|
or
|
||||
|
|
|
Code comments are definitely not a "code smell". This belief typically comes from the fact that comments can become stale (out of date) and can be difficult to maintain. However, having good comments which explain why the code is doing something a certain way can (and usually is) important for maintenance.
|
|||||||||
|
|
If the code has been written in a particular way to avoid a problem present in a library (both a third-party library, or a library that comes with the compiler), then it makes sense to comment it. |
||||
|
|
|
Even the most well-written book still likely has an introduction and chapter titles. Comments in well-documented code are still useful to describe high-level concepts, and explain how the code is organized. |
||||
|
|
|
Of honorable mention is the anti-pattern: It's my impression that sometimes FLOSS license preamples are frequently used in lieu of file documentation. The GPL/BSDL makes a nice filler text, and after that you seldomly see any other comment block. |
||||
|
|
|
I disagree with the idea that writing comments to explain the code are bad. This completely ignores the fact that code has bugs. It might be clear what the code does without comments. It's less likely to be clear what the code is supposed to do. Without comments how do you know if the results are wrong, or they're being used incorrectly? The comments should explain the intent of the code, so that if there is a mistake, someone reading the comments+code has a chance of finding it. I generally find myself writing inline comments before I write the code. This way it's clear what I'm trying to write code to do, and reduces getting lost in an algorithm without really knowing what you're trying to do. |
|||||
|
|
Comments that are put in because someone thinks it's ok to have 700 lines in one method are a smell. Comments that are there because you know if you don't put in a comment, someone will make the same mistake yet again are a smell. Comments put in because some code analysis tool demands it are also a smell. People that won't ever put in a comment, or write even a little help for other developers are also a smell. I'm amazed at how many people won't write stuff down, but will then turn around and acknowledge they can't remember what they did 3 months ago. I don't like to write docs, but I like to have to tell people the same thing over and over again even less. |
||||
|
|
|
I'll answer with a question of my own. Can you find the bug in the uncommented code below? tl;dr: The next person to maintain your code might not be as godlike as you are.
|
|||||||||
|
|
Reading this I am reminded on something that I first read (from a longer list, preserved by taking photocopies) some decades back:
A rather older smell methinks. |
||||
|
|
|
I think code commenting gets a very poor start to life. I don't know about these days, but when I was first being taught programming at school I got assignments of the nature of "Write a program that prints the numbers one to ten on separate lines. Make sure you comment your code." You'd get marked down if you didn't add comments because commenting your code is a GOOD THING. But what is there to say about such a trivial process? So you end up writing the classic
just to get a decent grade and, if you've any nous at all, instantly forming a very low opinion of code comments. Code commenting is not a GOOD THING. It's a SOMETIMES NECESSARY THING, and Thomas Owens in the top answer provides an excellent explanation of the situations in which it's necessary. However, these situations rarely crop up in homework-type assignments. In many ways, adding a comment should be considered a last-resort choice, when what needs to be said cannot be said clearly in the active parts of the programming language. Although object naming can go stale, various human and computer lack-of-feedback mechanisms make it easy to forget to maintain comments and consequently comments go stale much more quickly than active code. For that reason, where a choice is possible, changing code to make it clearer should always be preferred to annotating unclear code with comments. |
||||
|
|
|
It's all summed up in this blog posting: http://stevesmithblog.com/blog/when-to-comment-your-code/ |
||||
|
|
|
You have to keep a balance between code and comments... Usually I try to add some comments that resume a block of code. Not because I won't be able to understand the code (well, that also), but because I can read faster my own code and locate specific sections where the important stuff it's happening. Anyway, my own personal criteria is "when in doubt, comment". I prefer to have a redundant line than a completely cryptic line that I'm not going to be able to understand. I can always remove comments on a code review, after a while (and I usually do) Also, comments are quite helpful adding "caveats" like "Be careful! If the format of the input is not ASCII, this code will have to change!" |
||||
|
|
|
I have to agree with your coworker. I always say that if I comment my code, it means that I'm worried that I won't be able to figure out my own code in the future. This is a bad sign. The only other reason I sprinkle comments into the code is to call out something that doesn't seem to make sense. Those comments usually take the form of something like:
or
|
|||||
|
Of course comments are a code smell...every programmer knows we all eventually turn insane due to the amount of work, debugging, or just plain madness we run into. "Do this!" your project manager says. You respond, "It can't be done." They say, "Then we will find someone else to do it." You say, "OK, well maybe it can be done." And then spend the next X number of days.. weeks.. months.. trying to figure it out. Throughout the process, you will try and fail, and try and fail. We all do this. The true answer is there are two types of programmers, those that comment and those that don't. 1) Those that do are either making their own job easier by documenting for future reference, commenting out failed routines that didn't work (the smell is not deleting them after finding the one that works.), or breaking up the code with comment formatting to hopefully make it a bit easier to read or understand. Seriously, I can't blame them. But in the end, they snap and then you have this:
2) Those that don't are either pretending to be a superhero, or are living in a cave. They simply have reckless disregard for others, themselves, and could care less about the code, or what meaning it could possibly have for later. Now don't get me wrong.. self-documenting variables and functions can avoid this entirely.. and trust me you can never do enough code-cleanup. But the simple truth is that as long as you keep backups, you can ALWAYS delete comments. |
|||||
|
|
I'd argue that not using some comments in your code is a code smell. While I agree that code should be self documenting as much as possible, you hit a certain point where you are going to see code that makes no sense regardless of how well the code is written. I've seen some code in business applications where the comments are pretty much mandatory because:
Also, company style guides might tell you to do something a certain way - if they say that your could should have comments outlining what blocks of code in a function is doing, then include the comments. |
||||
|
|
|
There's a big fundamental difference between comments and code: comments are a way for people to communicate ideas to other people, whereas code is primarily meant for the computer. There are many aspects in "code" that's also only for humans, like naming and indentation. But comments are written strictly for humans, by humans. Therefore, writing comments is every bit as difficult as any written human communication! The writer should have a clear conception of who the audience is, and what kind of text they will need. How can you know who will read your comments in ten, twenty years? What if the person is from a completely different culture? Etc. I hope everybody understands this. Even inside the small homogeneous culture I live in, it's just so difficult to communicate ideas to other people. Human communication usually fails, except by accident. |
||||
|
|
|
Code comments giving, where applicable, units of function arguments and returns, structure fields, even local variables can be very handy. Remember the Mars Orbiter! |
||||
|
|
|
Here's my rule-of-thumb:
|
||||
|
|
|
Educate your co-worker about the Literate Programming technique. |
||||
|
|