by randomdrake · 13y ago · 0 comments
So many times I have had to repair code in languages I wasn't quite familiar in or that was simply using methods I hadn't learned yet. Those comments were life saving.
Comments describing intentions of code also allow for someone to come by later and quickly and concisely understand what the code was doing and where it was going.
I cannot think of a single instance where parsing 65 lines of a function definition was ever quicker than reading:
This method takes a foo object, applies HTML encoding, and returns an array of the original and the clean code.
In the same vein, saying that Unit Tests are a replacement for comments just brings me back to my original assertion: I already am looking for help understanding the code base, so I should look at more code elsewhere and parse another long list of assertions?
Bad comments are bad like bad code is bad, or like bad pie is bad. But comments, code and pie aren't bad.
Please don't stop commenting your code because you feel like you can be more clever with the language instead. Someone may be much more familiar with the language the comments are written in. They may may need to do something with what you wrote.
Whenever I read some new reason why comments aren't cool or why some programmer has decided they[1] can just be clever with the language, I can't help but think they just don't have enough experience to know why and how comments can be absolutely necessary and fantastic.
[1] not saying this is necessarily true in this case, it's just the impression I get.
def clean(foo)
return [foo, foo.apply_html_encoding]
end
The encoding logic should be contained in apply_html_encoding. The point is that most of the functions/methods that are 65 lines long are badly coded.I would consider that comment just as bad as the ones in my examples, but maybe not quite as obviously so.
It also has the side effect of being in a different file to the one you're trying to read.
But as you say, they need to be good comments, and comments that either describe intention or explain something in the language that might be rather obscure can be very useful.