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.

Comments on Comments

45 pointsby maxmusingover 1 year ago

17 comments

donatjover 1 year ago
In the “Why” vein, some of the most important comments are “Why I made the compromises I made” aka “Why this looks dumb but is actually for the best” comments.<p>They can prevent someone, including myself, from undergoing a timely rewrite of strange or bad looking code before inevitably hitting the same wall I hit previously.
评论 #37583583 未加载
评论 #37584143 未加载
评论 #37596771 未加载
评论 #37592696 未加载
kybernetikosover 1 year ago
The advice in &#x27;Replace What Comments With Names&#x27; section was just talked about recently here: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=37517329">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=37517329</a> in an article I agree with &#x27;Linear Code Is More Readable&#x27;.<p>If you&#x27;re going to reuse those little tiny functionettes, then sure, it might be worth doing, but to do it for readability is misguided. Comments are a perfectly reasonable way of indicating logical blocks within code.
评论 #37583400 未加载
评论 #37584036 未加载
hitchstoryover 1 year ago
100% agree with most of this, but I think the why can be dug into a little deeper. Under why comments I find they fit into two categories:<p>1) Why does the code exhibit behavior X? (~80%)<p>2) Why does the code do X this way? (~20%)<p>1 is usually a customer facing quirk which should be written down somewhere, but preferably not in the code directly. This stuff fits extremely well in tests which exercise the behavior (&quot;why does a user get an ID before the creation of a ticket?&quot;), in reference docs (&quot;why country appears 3 times in this API schema&quot;) or in a glossary (e.g. a quirks section under the &quot;admin user&quot; wiki page). These explanations will get more eyes on them this way, will be more likely to be kept up to date and will be available to non developers.<p>2 should not be non-existent but should be rare. In practice I find 2s are almost entirely absent from most code bases because to the person writing the code it is obvious.<p>For 2, because of this, I find it&#x27;s better to get somebody else to ask why and use answers to the whys on pull requests to write those comments: <a href="https:&#x2F;&#x2F;hitchdev.com&#x2F;approach&#x2F;communal-commenting&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;hitchdev.com&#x2F;approach&#x2F;communal-commenting&#x2F;</a>
评论 #37583248 未加载
评论 #37583956 未加载
bluGillover 1 year ago
Missing is one other style of comment: how to use this API. This is not targeted at someone understanding or maintaining the code, but someone outside who just knows (or suspects) they want to use your API without understanding it. While this need not be with the code it generally is better that way because tools can extract information from the code (the comment is before the function foo, therefore it must be about foo, foo takes some parameters with some types - we can link to the documentation for those types...).<p>This style of comment is only needed if you expect your code to have users who are external and thus don&#x27;t want to look into the details.
nonrandomstringover 1 year ago
Tangential, but see:<p><a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=37583258">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=37583258</a><p>Explaining &quot;why&quot; is often (revealing) more about how the programmer is thinking at that moment. It reveals hidden or ineffable knowledge about how the coder arrived at that point&#x2F;design, and may reveal intent not explicit in the code itself.<p>Mismatches between the declarative and imperative have often been where I&#x27;ve found bugs. Especially in my own code when trying to explain it to myself in a comment opens my eyes to an error.<p>Those &quot;why&quot; comments are our stories about our code.
k2xlover 1 year ago
I typically write comments for &quot;why&quot; rather than &quot;how&quot;. Or if I have to make what appears to be a weird hack or decision I put that too.<p>Another thing I really like to do is put github issues as comments if I am having to do some weird workaround in some API&#x2F;library that I found from a github issue - i&#x27;ll put the github issue URL so that way it is easy to see the latest update from that library later on to see if the workaround is no longer needed.
amadeuspagelover 1 year ago
The fundamental issue with comments in programming is that they&#x27;re part of the code, which is a ridiculous hack that somehow survives unquestioned. This is not how comments work in Google Docs, Microsoft Word, etc.. Maybe the idea of implementing comments as a greyed out part of the main text did not occur to the designers of these apps?
评论 #37583504 未加载
评论 #37584183 未加载
评论 #37583041 未加载
评论 #37583385 未加载
Supermanchoover 1 year ago
There is also the comments about why something commented out exists. Don&#x27;t make your fellow developers (including future self) to figure it out again.<p>&#x2F;&#x2F;&#x2F;&#x2F; These lines are a very simple way to enable feature X on your machine:<p>&#x2F;&#x2F; obscure.thing=false<p>&#x2F;&#x2F; someVar = &quot;probablyAthing&quot;;
gavinhowardover 1 year ago
You all would hate my code, and that&#x27;s okay. Why? Because I do everything that is suggested, save one: I leave <i>what</i> comments in.<p>For some reason, it is easier and quicker for me to understand English prose than code, even if the code is simple. That includes checking the actual code after reading the comment to see if it matches; having a target for the code makes it orders of magnitude easier to read for me.<p>That said, I still weirdly find value in <i>what</i> comments. Even some of the simplest ones communicate intent for me.<p>For example, I have many comments that take one of the following forms:<p><pre><code> &#x2F;&#x2F; Cache this. &#x2F;&#x2F; Get the &lt;item&gt;. </code></pre> These are the exact comments that people hate so much, but I like them because they communicate these things to me:<p>* The item is gotten for efficiency reasons. This means that I should check that it really is more efficient if something is slow.<p>* More importantly, the item is expected to <i>not change</i>, so if I&#x27;m digging aground for a bug, I should check that the item actually does not change.<p>So these &quot;useless&quot; comments actually help me.<p>In addition, the fear that they will go out-of-date is less of a problem for me because I have a strict habit of updating comments with code.<p>Now, I don&#x27;t suggest that everybody do what I do; I suggest the opposite, in fact. But I work alone, so I can do things that were ideal for myself.
评论 #37588695 未加载
ramses0over 1 year ago
An interesting go-ish thing to do regarding: &quot;&quot;&quot;Now, DownloadAndVerifyThing is shorter, contains less state, and is more obviously a composition of several tasks.&quot;&quot;&quot;<p>...I&#x27;ve experimented with &quot;convert a long sequential, independent function into several internal anonymous functions&quot; (ie: similar to IIFE in javascript).<p>This generally prevents (or contains) variable leakage and prevents (or contains) &quot;complexity leakage&quot;.<p>In their example: `DownloadAndVerifyThing(...) { ... }`, those functions could be defined internal to the `DownloadAndVerifyThing` function, which is kindof a further form of comment: &quot;thou shalt not be using these weird functions outside of this particular function which does the downloading and verifying...&quot;<p>Even if it&#x27;s done not in a function but in an anonymous block, &quot;trapping&quot; any state leakage or side-effects is nice for complexity reduction.<p>Example:<p><pre><code> func Foo() bar { { x := 1 y := 2 ...lots of math, etc... } abc := 123 } </code></pre> ...you&#x27;re not polluting the function internally with a lot of extra variables hanging around, which makes the inevitable refactoring &quot;more clean&quot; as you know at least the code block can be extracted independently w&#x2F;o impacting anything _after_ it (although you still have to be a little careful with the &quot;before&quot; and &quot;ordering&quot; portion of it).
adzmover 1 year ago
Tangential but I will say that c# is a great multi-paradigm language with terrible code comment culture that still uses xml-based comments, ugh it drives me crazy. I&#x27;ve even looked into figuring out how to get the compiler to auto transform javadoc-style or implicit-style comments into the structured format it expects, but it&#x27;s been tricky. I find this verbosity with the xml markup makes it more difficult to read as a human and discourages good comments.
svilen_dobrevover 1 year ago
i wonder... things like this are known (mostly from trial and error bit not only) for 50-60+ years.. why they are not studied?<p>and every new kid on the block has to find-out&#x2F;invent them ?
langsoul-comover 1 year ago
I like the beware comments, where weird code is done in a certain why that won&#x27;t make sense a few days after its released and not touched again.
forgotmypw17over 1 year ago
I&#x27;d like to add another comment type to this list, which is searchable #topics and alternative procedure names.<p>For example, if there&#x27;s a feature around replies, I may put #replies in the key places in the code. If some code takes part in generating replies.html, I might put &quot;#replies.html&quot; as a comment.<p>A lot of my procedures have comments with alternative names aliased underneath:<p><pre><code> sub SqliteQueryHashRef { # $query, @queryParams; calls sqlite with query, and returns result as array of hashrefs #sub SqliteGetHash { #sub SqliteGetHashRef { #sub SqliteGetQueryHashRef { #sub SqliteGetQuery { #sub GetQuery { #sub GetQueryAsHash { #sub GetQueryAsArray { #sub GetQueryAsArrayOfHashRefs { </code></pre> Each of these represents a time in the past when I went into the global find tool and searched for e.g. &quot;GetQuery &quot;, didn&#x27;t find anything, and then eventually tracked down this procedure.<p>BTW, if you only put a space after the procedure name in its definition, its much easier to search for.
ranting-mothover 1 year ago
Code is logic. Comments are wisdom.
评论 #37583482 未加载
jacquesmover 1 year ago
Literate programming is a nice middle ground but it doesn&#x27;t work for every project and it definitely doesn&#x27;t work for every person.
majikandyover 1 year ago
I like the idea of comments being impossible and one acts with that in mind. Along with the one about thinking the next person to view your code is a psychopath who knows where you live. Holding those 2 things in mind, you really can have your cake at eat it (too), you can have clean expressive code that covers a lot of the “why” without comments littering the screen.<p>How are you going to make the code itself scream of its “how and why”? Then failing that you can put a comment if you must.<p>I have worked with codebase with incredible comments. Changing the code was really hard and laborious and far from a joy to work with. When PRs become back and forth about how to change the wordings of the comments in the code, it is soul destroying, it becomes very time consuming. Like writing a joint novel at the same time as writing the code. Some people like it that way and each to their own, I know I can’t convince them.