TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Stop commenting your code just to say you did

57 点作者 bradt大约 11 年前

21 条评论

carsongross大约 11 年前
God forbid someone gets their thoughts together with comments outlining what a function is going to do and then fills in the code...<p>Or, while deep in thought, someone taps out a bit of redundant commentary on a line or two of their code...<p>Or someone needs a bit of natural language to provide an anchor in their code.<p>The important thing here, I think we can all agree, is that we are smarter than that person.
评论 #7509783 未加载
评论 #7510818 未加载
评论 #7509756 未加载
评论 #7509721 未加载
millstone大约 11 年前
Don&#x27;t listen to this guy. Copious comments are great.<p>1. They act as a easier-to-skim narrative of the code. You can often get an outline of a well-commented component by reading the comments alone, rather than by picking apart a language designed for machine consumption. Syntax highlighting makes this especially easy.<p>2. They&#x27;re helpful for other programmers who may not be as comfortable in the language as you are. For example, my PHP is about ten years old; I had no idea that `__construct` is the constructor, and not just another function. But thanks to the &quot;useless&quot; comment, I do know that.<p>3. They&#x27;re helpful for programmers who aren&#x27;t as comfortable in the problem domain. A line in a FFT implementation may be &quot;obvious&quot; to those skilled in harmonic analysis, but not the poor client programmer who is trying to figure out why the function is returning garbage output.<p>4. They can be useful as a thing to search for when you need to navigate around.<p><i>When deciding if I should write a comment or not, I ask myself a simple question: if I come back to this bit of code, will it be obvious why it’s been written this particular way? If my future self will be very thankful for an explanation, then it’s a no brainer, I write the comment.</i><p>Note the assumption that the code will only be consumed by &quot;my future self&quot;.<p>Ever maintained someone else&#x27;s under-commented or no-commented code? I&#x27;m sure the original programmer did not have any trouble with it, and will never know what he inflicted on his successors. Spend some time working in a comment desert, and you&#x27;ll never complain about the rain.
评论 #7509942 未加载
评论 #7510041 未加载
Danieru大约 11 年前
I suspect part of this is enforced by TA markers in university.<p>When marking a TA isn&#x27;t trying to understand the code, they just do not have time for that. So when evaluating the documentation or comments portion of the mark they just scroll through and see how much gray is on their screen. No marks are ever deducted for bad comments.<p>The situation can get so silly I&#x27;ve had TAs complement my code and the design then deduct full documentation marks.<p>This has led some people I know to go over their code before submission and add worthless comments. To them &quot;documentation&quot; is a separate step which occurs after you&#x27;ve written and tested everything. The whole situation is a shame.
评论 #7510227 未加载
评论 #7509653 未加载
评论 #7509757 未加载
ef4大约 11 年前
This is one of the reasons that programmers who haven&#x27;t read much of other people&#x27;s code are a liability. They&#x27;re as lost as novelists who don&#x27;t read books.<p>The difference comes down to empathy: if you can get inside the head of your future readers, you&#x27;ll know when to comment, and what to name that function, and how long the functions should be, etc...
评论 #7509726 未加载
SomeCallMeTim大约 11 年前
The example that he gives that I take issue with is the &quot;Constructor&quot; one in a JavaDoc comment.<p>When using documentation tools, every undocumented function spews a warning. And that <i>is as it should be.</i> But some simple constructors don&#x27;t have any functionality worth documenting.<p>I code as if all warnings should be addressed, and that&#x27;s true of my documentation generation. If a public function has no documentation, that should be considered an error that needs fixing. If the documentation is obvious, then <i>so be it.</i><p>That is the price you pay for reliably good documentation; without enabling those warnings you don&#x27;t know when you&#x27;ve missed documenting an important function. And even sometimes the &quot;obvious&quot; is useful; I don&#x27;t know how many ambiguous Boolean parameters I&#x27;ve seen where it wasn&#x27;t clear what value &quot;true&quot; represented.
评论 #7509907 未加载
评论 #7509895 未加载
nghst大约 11 年前
I think the occasional &quot;header&quot; comment is useful, even in cases where the code is obvious, i.e.<p><pre><code> &#x2F;&#x2F; do blah blah obvious_thing; obvious_thing; obvious_thing; obvious_thing; &#x2F;&#x2F; do something else obvious_thing; obvious_thing; obvious_thing; obvious_thing; </code></pre> Even though none of the lines really need documentation, having the comment ensures that you can quickly jump in when you come back later.<p>This is really useful in something like drawing code.
评论 #7509547 未加载
methodover大约 11 年前
I cannot recall a time in my career as a programmer where the inclusion of a comment has caused me serious problems.<p>However, I can recall many, many times where I&#x27;ve read code that does something odd, and wished that there was some documentation explaining it.<p>I&#x27;ve met many programmers who rarely comment and make points similar to those made in this article. It&#x27;s an easy argument: code should be self-documenting. But I&#x27;ve seen too many programmers jump from &quot;code should be self-documenting&quot; to &quot;I shall never use comments&#x2F;docstrings.&quot;<p>I feel like the problems outlined in the article are overstated. It really isn&#x27;t a big deal when you read a comment that doesn&#x27;t make sense, or is empty of content. It&#x27;s a much bigger deal when you read strange code which really deserves documentation but there is no documentation.
yk大约 11 年前
Thing is, just like with code, you write good comments by throwing away bad ones. The writing of the bad ones is not optional.
raverbashing大约 11 年前
Yes<p>If you write this: x = 1; &#x2F;&#x2F; sets x to 1 I HATE YOU<p>Explain the WHY not the WHAT<p>It&#x27;s the same crap with the Hungarian notation, where half MS didn&#x27;t get how to do it properly. It&#x27;s not the computing type, it&#x27;s <i>what the variable represents</i>. Thank you, I know this is an int, the compiler knows it&#x27;s an int. What I need to know is what this is counting&#x2F;representing&#x2F;etc
err4nt大约 11 年前
I&#x27;m beginning to believe that if code is simply a machine-interpretable codification of human expertise, then we should be able to express precisely what the code should be doing using written language as well.<p>I&#x27;ve been turning this over in my head to figure out how it should look in practice, but I&#x27;m wholeheartedly believing the following statement is true:<p>&quot;All code should be composed in two languages: first in whatever programming language the task requires, and second in whatever language the programmer speaks natively.&quot;
评论 #7510051 未加载
dmckeon大约 11 年前
Comment first: project the document instead of documenting the project. In other words, write the comments first, then write the code, then review and tidy up both the code and comments.<p>By writing the text first, you clarify the <i>why</i> before you write the how. You also capture the high-level view before you get down to the gnarly details, after which the high-level view can become a vague ghost of a memory.<p>Once that high-level view is left behind, many people become so immersed in the coding details that &quot;introductory&quot; material written after coding often begins with the esoteric details of the implementation of the central algorithm, and fails to mention what the tool&#x2F;packages&#x2F;code&#x2F;app is <i>for</i> in the first place.<p>To put it another way, documenting is to code as foreplay is to sex. Both code and sex can be effective without comments or foreplay, for some values of effective, but they are more likely to be more pleasant and repeatable over time if people put in a little time and effort before rushing ahead to what they see as the most interesting parts.
whyme大约 11 年前
I&#x27;ll suggest there&#x27;s a missing ingredient here...I found I was able to eliminate many of my comments by asking myself if that someone whom might read my code would be better served with a unit test instead. I also found, however, that this needs to lead to an understanding that unit test are in fact a form of documentation and should be treated as such.
评论 #7510379 未加载
kephra大约 11 年前
I&#x27;ve learned coding in Forth, so my code is often creating a domain specific language to solve a class of problems, and Forth shadow pages caused the documentation to be roughly same size as the code itself. But I normally comment in manual style. So the comments in my code do not tell how it works internally, but how to use it, and they are extracted from the code to create a standard Unix roff manual page. I think the Perl community did a good job in constraining a documentation style that is useful for those who want to use a module.<p>There are seldom exceptions, where I actually comment the code itself. Those are clear warnings that this code is not trivial to understand for average programmers. E.g. my w3dig search engine implements a recursive descendent parser for the site description language, that is not always executed linear, but sometimes pushes a parser class instance together with a closure to a queue, for parallel execution of the spider.
beshrkayali大约 11 年前
Not that I don&#x27;t agree, sometimes unnecessary comments kind of make it a bit complicated to understand what&#x27;s going on. BUT, an important point to mention is that coders write code in different styles and manners, there is no right and wrong when it comes to comments. Sometimes just seeing that comments exist (even if some lines are pointing the obvious) makes a difference to the inexperienced or to the new-comers. Not to mention that &quot;obvious&quot; here depends heavily on the experience of the developer with language, framework, or whatever they&#x27;re using. I guess the person writing the code should at least get to write comments the way the like. You&#x27;re a programmer, reading a couple of extra comments won&#x27;t ruin your day, just skip it if it&#x27;s obvious.
quackerhacker大约 11 年前
Comments in production code have always been a pet peeve of mine. I understand the OP&#x27;s frustration on useless comments where the function clearly states enough in it&#x27;s naming convention.<p>I brought this up to one of my instructors before and he said that comment overhead is negligeble on bandwidth (I was referring to css and js commenting at the time), but I don&#x27;t think that&#x27;s true when you factor in mobile networks and when some teams comment every freaking line when naming convention and common sense should suffice.
评论 #7510395 未加载
icehawk219大约 11 年前
One of my guiding principles for code comments has always been &quot;say why not what&quot;. If I can&#x27;t figure out that what on my own then something is probably very wrong with the code. What I want to know is <i>why</i> you used a for() instead of a while() to iterate over an array or things like that. Comments that explain what you&#x27;re doing I find to be cruft. I don&#x27;t necessarily hate on them but I do find them unnecessary and unhelpful.
logfromblammo大约 11 年前
My current employer makes the useless code comments mandatory. They do not need to make sense or be useful; the requirement is that they exist. Fortunately, they pay me enough that I am not looking for other jobs over it.<p>No, I&#x27;m looking elsewhere due to the many other aggravations that are not worth the salary.<p>Not only are businesses training younger developers to do this, but they are training older developers to expect it. Of course it is going to bleed over into the real world.
评论 #7509750 未加载
apdinin大约 11 年前
I can agree with the seeming intention of the OP, but the analogy of adding labels to wires in an electrical box is problematic. How does having blank labels on wires equate to poor commenting?<p>Good or bad, a comment says something about the code it&#x27;s commenting (even if the code is self explanatory). A blank label says nothing about the wire.<p>The better analogy might be having labels on wires that state the color of the wires -- red, blue, green, etc.
korzun大约 11 年前
To author, the constructor is not a function it&#x27;s a method.<p>I agree with useless comments but don&#x27;t agree with blank &#x27;constructor&#x27; method doc blocks that just say constructor as a filler.<p>Since most of the methods will have params&#x2F;returns and better description than a simple constructor, leaving constructor without any doc-block is kind of counter productive and silly.
jgrahamc大约 11 年前
Leaving comments in real life: <a href="http://blog.jgc.org/2011/11/leaving-comments-in-real-life.html" rel="nofollow">http:&#x2F;&#x2F;blog.jgc.org&#x2F;2011&#x2F;11&#x2F;leaving-comments-in-real-life.ht...</a>
评论 #7510367 未加载
shearnie大约 11 年前
Equally frustrating are revision comments (usually for a big end-of-day commit) that go: &quot;Committed changes&quot;.