Comment your damn code
21–30 of 68 posts
Re: Comment your damn code
#22Note that the following does not count as commenting your damn code: // Get the account holder currency $currency = $accountHolder->getCurrency(); Imagine several 10s of KLOCs where 99% of the comments are as worthless as this and most people are content with their efforts because they believe they are commenting thoroughly.
As with all things, ROI is key. Your commenting effort is a finite resource. Expend it in the way that gets you the most value.
Re: Comment your damn code
#23Comments can't guarantee to represent what the codes does always. "Don't be lazy" is a fine argument, but it takes one time that you are under pressure to ship right away and you don't comment. Tests, on the other hand will keep failing until they describe the updated version of the code.
We should strive to make code obvious, when it's not obvious, then you should comment, or better: simplify your code.
Re: Comment your damn code
#24I think there's the issue. In applications I write, every 3-7 lines of code is refactored into a method that is named exactly what it does, and every few hundred lines of code (usually much less) is in a separate file and class named on what he does. Broken out like that, there's not many places to put a comment that would be of any use.
But not broken out like that, of course you're going to need comments. But I think you'll find that comments aren't even enough.
"I know what the code says. But tell me the intent."
I think unit tests and expressive code work much better to achieve this goal.
Re: Comment your damn code
#25 # First we iterate over the directory and get a list of files to process
...
# Then we process them
This "pseudo-code" would then be filled in by the actual source code ei intended to write. And once ei was done the comments would stay in and be checked in for code review.Nothing was more tedious to read. It's like having a narrator tell you exactly what each character is going to say right before they say it. It is quite maddening to this reader and I had to constantly review this sort of code.
I had to politely approach the person writing this code and explain this to them. Comments are not for describing what can already be read. One's code should be written in such a way as to inform a programmer maintaining the code to its purpose and utility. That means short, single-purpose functions, conventions, idioms, and all of that. But it also means that comments should only be used when you're going against convention or doing something hack-ish on purpose.
I saw fewer lines of comments until a fresh, new young team member joined...
Re: Comment your damn code
#26Why comments are important: they represent intention . A reasonable complex method can have a wide variety of inputs which the author can't be expected to have tested across the entire range of values. For example, if you have six boolean inputs, you have 2^6=64 possible combinations. If you have six integer inputs, you have roughly (4e9)^6=4e57 combinations, which is more than the number of atoms in the Earth. Expre…
A comment, on the other hand, cannot be trusted. Ignoring the fact that it could simply be out of date and wrong, your interpretation of intent might not the same as the person writing it. A lot of this might be because you simply don't share the same context. After all, when this comment was written has a context beyond the location of the code.
Re: Comment your damn code
#27I'm against commenting code, with two exceptions: - You're implementing something complex (like an algorithm) - You're implementing something stupid (typically a workaround for something that could not be done in a more elegant way, and you want to explain why it can't be refactored) If the code is well written it is also self explanatory. This can be done by structuring / formatting the code well, into methods, clas…
Re: Comment your damn code
#28Well, I disagree. Test-drive your damn code. The difference in positive impact will be a few orders of magnitude.
But I'm with Toshiro - test your damn code! Testing first brings all the benefits of commenting and more:
Tests help document and clarify your intent. They convey your assumptions, expectations, and show you exactly how methods are expected to behave when you run the test.
3 months later when you're adding new features, a failing test tells you that you introduced a bug before you ever run the new code.
That said, comments have a place in the tests themselves. Much better than scattering comments all around the code base.
The other place for comments is in the version control. Make small, frequent commits and set up your version control so that it forces you to comment each time.
When you're doing these things, comments in other parts of the code are just redundant and noisy.
Re: Comment your damn code
#29Note that the following does not count as commenting your damn code: // Get the account holder currency $currency = $accountHolder->getCurrency(); Imagine several 10s of KLOCs where 99% of the comments are as worthless as this and most people are content with their efforts because they believe they are commenting thoroughly.
Those aren't comments, that's someone being lazy. A comment should describe the why or the goal, not the how.
Re: Comment your damn code
#30I'm against commenting code, with two exceptions: - You're implementing something complex (like an algorithm) - You're implementing something stupid (typically a workaround for something that could not be done in a more elegant way, and you want to explain why it can't be refactored) If the code is well written it is also self explanatory. This can be done by structuring / formatting the code well, into methods, clas…
C# 11 years of experience