3 ms·
> Comments cost essentially nothing to add to your code and can save it from an early death and complete refactoring by the next guy who comes along. Whatever
by seanmcdirmid 12y ago
> Comments cost essentially nothing to add to your code and can save it from an early death and complete refactoring by the next guy who comes along.
Whatever their benefits are, this is not true at all. Comments are expensive to write and maintain. They are VERY VERY expensive to maintain because there is no automated way to test them.
- anthracis417 12y agoForgive my ignorance, but why and how would you test a comment? They don't do anything, there is no instruction for the machine to understand or run.
- ttty 12y agoThen you will read the comment that says to do one thing but the program will do a completely other thing. I don't see any problem, do you?
- mike_hearn 12y agoThat's why good comments are about explaining WHY you're doing something, or HOW to use the code, and possibly making the code itself clearer but only if there's no way to do that by introducing better variable and method naming. Comments are highly valuable but like any tool they can be abused, or done in such a way that they don't make things better.
- wtetzner 12y agoThe point is that comments can become out of date, and there's no automated way to enforce they stay up to date.
- knowaveragejoe 12y agoHe's referring to the problem of comments becoming out of sync with the code they're referring to. Not everyone updates comments as they update code.
- jdbernard 12y agoAnd those people deserve a special place of torment all their own. How hard is it really to update, or at least delete comments that are no longer relevant when you modify the code?
- knowaveragejoe 12y agoIt's not that it's hard, it just may not be apparent that you need to do so.
- seanmcdirmid 12y agoThe comments are meant to be read and used to understand code. So you must test them like any other artifact to ensure that they are a net positive and not a net negative. Programmers make mistakes, which hopefully fail a test or at least cause a crash. Because comments can't be executed, neither of those will happen; they have to be verified manually
- AlisdairO 12y agoIsn't that fairly easy to do during a code review?
- seanmcdirmid 12y agoSure, code review is a very manual approach.
- AlisdairO 12y agoYes, what I mean is, if you're doing code review then there's virtually no additional cost - you're looking over the changes anyway. It doesn't matter that the process is manual if you're already doing it. If you're not doing code review, then you perhaps either have some organisational issues or you have coworkers who are sufficiently responsible that you can trust them to do basic code hygiene work like keeping comments up to date.
- seanmcdirmid 12y agoI think comments would be more difficult to review than code, since you'd have to carefully make sure they are meaningful by examining the code, without the context of the programmers involved. Alternatively, you could add comments during code review to document the review and basically redo the comments on each review...maybe. Comments also break flow and get in the way of code reading, but could probably just be hidden during review.
- dragonwriter 12y ago
- peaton 12y agoBrings up an interesting point about perhaps using natural language processing to determine correctness of a comment. That would be prettty cool.
- kedean 12y agoIf you could use NLP to verify the comment, then what would the point of the comment be? Comments are for those aspects of a program that are invisible to the compiler and IDE, the intent.
- peaton 12y agoMaybe my view of NLP is more sci-fi than reality, but I think you're missing what I meant. If you can use NLP to prove that your comment - ie the intent - which IS invisible to the compiler and IDE, then that might be cool. Practical, probably not. But I'd still be very impressed at the implications if this was possible within some error margin.
- nirvdrum 12y agoI've certainly come across situations where the code and the comment don't line up, but that seems to arise from laziness more than anything else. If the comment is directly attached to a region of code, most developers I've come across will process both at the same time. The area where I see it fall apart most commonly is with javadoc-style comments. Most IDEs will even flag these issues, but they're often ignored. A comment on its own isn't inherently harder to maintain any other piece of code or documentation. Sure, it's not zero cost, but I find more often than not they exceed the cost associated with not commenting.
- reitanqild 12y agoDisagree. Reason: In Software Archeology (a.k.a. maintaining legacy code) you are often happy to get any kind of clue as to what went on inside someones head. Maybe they saw an edge case? Of course you should write self-documenting code with variables and function names that explains most of it but when you either have to 1. factor out a new function do_this_to_fix_that_weird_thing(weird state) or 2. have to add a line state == weird ? fix = fix+1:continue // weird is a weird state that sometimes occurs even though vendors api docs says otherwise. please think twice. Outdated comments are a problem but very often a problem I'll happily deal with compared to not having them at all. If a comment doesn't make sense you can also try checking VC history.
- otikik 12y agoI will take 3 3. fix_bug_that_occurs_even_when_api_docs_say_otherwise(weird)
- remon 12y agoYou're not wrong but you're isolating the cost of commenting a certain piece of code. It's really about the net time investment. If you create comments the idea is that code maintenance in the future is easier and/or introduces less risk of introducing new bugs (and you'll agree those are even more expensive to resolve). That said it remains a bit of a subjective discussion.