TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

I hate comments

29 pointsby commandaover 12 years ago

22 comments

randomdrakeover 12 years ago
Hopefully no one even moderately unfamiliar with the language the code is written in needs to find out how to fix something that is broken. Citing breaking code by using some global that makes the comment no longer true? Sounds like an abuse of globals or someone not cleaning up the comments, not the comments themselves.<p>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.<p>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.<p>I cannot think of a single instance where parsing 65 lines of a function definition was ever quicker than reading:<p><i>This method takes a foo object, applies HTML encoding, and returns an array of the original and the clean code.</i><p>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?<p>Bad comments are bad like bad code is bad, or like bad pie is bad. But comments, code and pie aren't bad.<p>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.<p>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.<p>[1] not saying this is necessarily true in this case, it's just the impression I get.
评论 #4934857 未加载
评论 #4934769 未加载
评论 #4935046 未加载
评论 #4934849 未加载
Pewpewarrowsover 12 years ago
Sounds like the developers who wrote that code didn't know how to write comments. Here's some guidelines:<p>* Anything that duplicates the line they describe in prose should absolutely be removed. They're worthless and become out of date immediately.<p>* If you have logical "paragraphs" of code separated by newlines and comments summarizing the paragraph, that's a great indication that your parent method/function/whatever is in charge of doing too much. Turn the comments into names of new methods/functions and refactor accordingly.<p>* If the algorithm is sufficiently complicated, it definitely deserves a documentation block describing the "how"s and "what"s. But more often than not those comments are just a duplication, as specified in the first bullet point above. If you don't think any code ever deserves these sorts of comments: congratulations, you've never worked on anything non-trivial in your career. I suggest that you find more of a challenging project.<p>* Definitely leave a comment behind explaining the "why"s behind a particular block of code if its intentions are not immediately obvious.<p>* Definitely have headers of documenting comments for functions/classes/methods/parameters/etc. Is your first reply when someone asks "How do I learn to use Rails?" to say "Read the entire source code to Rails"? No, of course not. For the same reason, your reaction to "How do I use this block of code" should not be "Read the block of code." Abstractions and proper APIs people, come on. I should be able to glance at the docs above it along with the definition and know how to use it, what to pass to it, and what I might expect in terms of returns / side effects. Bonus points if you have a tool to auto-extract these into a static site that you host for your team.
kjackson2012over 12 years ago
What a terrible and self-righteous post.<p>How is a test case supposed to explain <i>why</i> MAX_COUNTER is set to 5 or 20? Comments are the single-most important thing when writing code, in my opinion. When feature specs are lost, and documentation goes out of the date, you only have code. And if you're supposed to jump through hoops like test case code to try to divine WHY things are as they are, then you're dealing with really hard-to-maintain code.<p>I strongly suggest all developers to ignore this blog post, and to comment as best as you can.
评论 #4935225 未加载
commandaover 12 years ago
I helped the author edit this post for clarity, but it took all I had not to argue with him because I strongly disagree with his thesis. My main language (Objective-C) is relatively less readable (compared to his main language of Python), and I would absolutely not be able to function in a team, or even with code I'd written myself over a week ago, without heavy commenting. I do believe he's right though with respect to test coverage being effectively a runnable comment or explanation of how a particular module works. However, to me at least, tests are no more readable than the source code itself, so I still rely (and implore my teammates to rely) on heavily commenting their code. Especially when dealing with inscrutable things like OpenGL code.
评论 #4935300 未加载
sankyoover 12 years ago
The comments that he shows are pretty worthless. However, comments are often necessary to explain WHY we are doing something.<p>oven.setTemp(350) // 400 will burn them, 300 they will be mushy<p>fluxCapacitor.setOption(THRESHOLD, 1.21E9) // lower thresholds will prevent return from time travel. Please see <a href="http://en.wikipedia.org/wiki/DeLorean_time_machine" rel="nofollow">http://en.wikipedia.org/wiki/DeLorean_time_machine</a> to review the physics
评论 #4934921 未加载
B-Conover 12 years ago
I personally have four reasons for writing comments. If a comment does not fall squarely into one of them, I try to omit it:<p>1) Why the code is doing what it's doing. What's the motivation? Why is a check necessary? What's the context?<p>2) High-level overview. 20 lines of code may speak for themselves, but a quick sentence can easily summarize it. I love well-summarized code. "Do X with the Y unless it's Z" is a really fast read. (Function names obviously provide this sort of explanation, but sometimes a name is not obviously not always sufficient.)<p>3) Stubs / future notes. Sometimes you need to leave something stubbed out. If you have thoughts on what they code will need to be, leave a note about it. This is easily removed later.<p>4) Quirks. If the code does something that is not obvious, note that. Non-obvious side-effects, bugs, etc. Don't discover something painful, then leave the next person (possibly a future you) to re-learn that same painful lesson.<p>Code does stuff. Comments describe the code. Comments should not re-describe what the code does, they should be about the code and the code's context itself.
评论 #4934937 未加载
theevocaterover 12 years ago
Comments are for why, code is for how.<p>Why is the counter &#60; 5 or the balance &#62;=0? Comments explain why your assertions are reality. Not every assertion is going to be as obvious as "balances have to be positive". If I had a dollar for every opaque assertion I blew up or ran into in code that had no explanation as to <i>why</i> something had to not be 0 or &#62;1mil or null or whatever I would be a rich man.
elibenover 12 years ago
I don't mean to sound patronizing, but if you're implementing counters or banging out numbingly-boilerplate business logic for websites, comments are indeed useless.<p>But when you're reading an obtuse 50-line function that implements some weird algorithm acting on some weird data structure, full of magic-like assumptions, you'd wish no one had deleted comments from it, or... had written them in the first place.
评论 #4934758 未加载
saidajigumiover 12 years ago
I'll agree that <i>good</i> comments don't reiterate what is plain from the code. Truly clear, self-explanatory code is a joy to read. But the process of coding usually boils away any notion of why, leaving only the brain-compiled machine that implements the how. It's the original use-case, high-level policies, implicit contracts and so on that comments should generally focus on. Especially, as the author notes, since TDD infrastructure and culture has gotten so much better at covering the "how" in recent years.<p>Also, sometimes the code is sufficiently brain-compiled that it's utterly inscrutable. Early in my career I wrote a fast interrupt driver for the company's main product that had become quite heavily optimized, and practically every line depended on nuances of the related hardware's functionality. This code was written in two-column code/comment form, and had more lines of comments than lines of code. This was necessary for it to be at all maintainable. In theory, a later refactoring might have simplified this state of affairs... but that opportunity never happened.
acc00over 12 years ago
the comments in the gc example are not there because the language this is implemented in is less readable than python; they are there because the problem the code solves is non-trivial and requires documenting. however clean, readable and trivial your code is, you can't express <i>your thought process</i> with it. and when you're doing sophisticated things, that is very important to convey.
fotbrover 12 years ago
Take it up with programming 101 instructors around the world, who beat it into their students' heads that "every line" / "every function" / "every class" etc <i>needs</i> comments, no matter how redundant or bad the comment was, because "good code is commented". No joke, fairly well respected science &#38; engineering university, every line of every program through the first three semesters of a CS program was required to be commented. After a while, slapping in useless and bad comments became habit, required for the assignment to even be considered for grading. It takes a while to break that habit.
评论 #4935068 未加载
alexchamberlainover 12 years ago
I've recently read Clean Code by Robert Martin and now totally agree with the OP; everyone should go and read it, though it's full of annoying Java, it does apply equally to proper languages.
评论 #4935375 未加载
hvsover 12 years ago
A conversation I once had with a junior developer:<p><pre><code> Him: I don't write comments. My code is self-documenting. Me: Apparently, you have never attempted to read those documents. </code></pre> Comments should include the why's of code's existence (e.g. business rules) as well as any bigger picture information about the code for usage (e.g. "This function is for X, if you are looking for the function for Y, you are probably looking for Z").
moocow01over 12 years ago
This doesn't address the practical real-world problem...<p>Most developers are under at least moderate pressure to deliver and usually their priority is this...<p>1) Make thing appear to work<p>2) Make thing actually work<p>3) Clean up code to pass a code review or not be yelled at by a coworker<p>4) Comment where things are necessary<p>In many cases we barely get past step 1 and a half and usually its not even our fault - the business case has been mostly satisfied and according to the mucky mucks its best to move onto the next feature. Yes most of us dream of places where we can craft our code to oblivion but its probably not the average reality unfortunately.<p>I think its great when somebody takes the time to comment - commenting is not fun and has very little upfront reward but can be a lifesaver when traversing complex logic. If you have the ability to leave a profuse amount of comments I can hide them when I dont need them and use them when I have no idea whats going on - comment as much as you like.
katbyteover 12 years ago
Without comments how do you produce documentation for your code?<p>I put XML comments on every function, object, property etc giving them at the very least a short summary. These are then compiled into documentation xml that integrates with the IDE providing pop-up documentation for objects and functions without needing to see the source code. This xml is then compiled into actual html documentation is also used for APIs and libraries that other developers use without any access to the code.<p>Maybe documentation falls outside of the purview of source code comments... but i don't think so and i find it really handy to have the source code self documented with comments keeping docs and code in the same place.
epsover 12 years ago
Ah, to be young and wild, and have most of my limbs again...<p>Try expressing a relatively complex state machine with asserts. Then come back in a month and try converting all those asserts back into few English sentences.
kondroover 12 years ago
Comments prove that naming is hard.<p>If we as developers spent more time on using better names for our objects/methods, we would need less comments.
jamesmcnover 12 years ago
and also vi &#62; emacs<p>But seriously. Comments are useful. Don't over use them, don't under use them. If you are being dogmatic about your comments, you are probably doing it wrong. Autodoc comments can be nice, when done properly.
slajaxover 12 years ago
Comments can be super fun. Especially if you are into fabels and can liken the purpose of your code to the journey of a hobbit or similar creature from the shire.
halayliover 12 years ago
It's more accurate to say: I hate bad comments.
Peakerover 12 years ago
You can start to use your commit log as the comments.
kaonashiover 12 years ago
tl;dr<p>Comments suck, documentation rocks.