Don't waste your time commenting source code
riyadsthoughts.blogspot.com
Don't waste your time commenting source code
1–10 of 25 posts
Re: Don't waste your time commenting source code
#2Re: Don't waste your time commenting source code
#3An often stated rebuttal to the above is that the developer can comment the "complicated bits", and save time by skipping it the rest of the time. This is a flawed argument -- you don't know if you need to write a comment for a block of code until you've spent the time fully considering what needs to be commented ... which takes just as long as writing a comment.
This holds doubly true for APIs. Any time that your future API clients spend reading your source code instead of skimming your documentation is wasted time. Additionally, deriving guarantees and invariants from the source code does not make them true -- the invariants could be changed in the future, as there's nothing in the code to document what should be, instead of what currently is.
"Comments are unnecessary" is just an excuse for lazy developers to be lazy, and thus leverage externalities to reduce their upfront workload in exchange for increasing the workload and complexity for the programmers that follow them -- which may, in fact, be themselves.
Re: Don't waste your time commenting source code
#4I had some horrendously complex code to compute the two roots of a quadratic, and someone came along and "tidied it up." They replaced my code with:
delta = sqrt(b^2-4*a*c)
a2 = 2*a
x0 = (-b+delta)/a2
x1 = (-b-delta)/a2
They were then horribly confused over why the tests started failing, the check-in was broken, and all hell broke loose in the development branch.He put it back and all was well, then came to ask me about it. I explained about numerical stability, and all was well.
So with regards comments:
Pro: He would've known not to change the code, and it was complicated for a reason.
Con: He wouldn't've come to me to discuss why it was a problem, and hence wouldn't've learned about numerical stability.
Gripping hand: With a comment he would've known why, and the time wasted changing the code, testing, investigating and putting it all back would've been saved, and better spent learning about numerical stability, which he then had to do anyway.
I'm in favor of the correct use of comments, and YMWV as to what that means. Dogma is the enemy of true progress.
Re: Don't waste your time commenting source code
#5E.g., I was recently writing for a JPA entity on Google App Engine where I tried using a @PrePersist function to update a field. Turned out that GAE won't call the @PrePersist unless a field is updated first...i.e., catch 22.
So, say as a workaround you add an artificial update to the field to null just so your @PrePersist is called, what are the odds that the next guy reading the code without a comment won't just delete that assignment thinking it's unnecessary?
Re: Don't waste your time commenting source code
#6Comment the why, not the what.
I HATE code with no comments. And tests are not sufficient documentation, though that's an argument for a different time.
Re: Don't waste your time commenting source code
#7Re: Don't waste your time commenting source code
#8Re: Don't waste your time commenting source code
#9Ha ha ... also don't waste your time asking me for a job.
Re: Don't waste your time commenting source code
#10As I'm sure will be repeated over and over, comment the why, not the what or the how.