News for hackers. Stuff that matters.
Re:Ask HN: Do you ever go back and admire a piece of code yo... by Rainymood on Friday May 08, 2020 @10:39AM (#23115278)
Me writing docstrings: God this is so much work for nothing... my code should speak for itself.

Me 6 months later: Thank god I wrote docstrings, what is this garbled mess.

Re:Ask HN: Do you ever go back and admire a piece of code yo... by jjice on Friday May 08, 2020 @10:51AM (#23115401)
100%. It's amazing how many GitHub repos have little to no comments, not even file or function overview comments. The amount of time save by potential contributors reading and having to interpret the code is way longer than it would take for the author to write the comments in the first place.

To each their own, but comments are generally a positive addition to code.

Re:Ask HN: Do you ever go back and admire a piece of code yo... by CmdrKrool on Friday May 08, 2020 @11:32AM (#23115892)
Yes, the greatest value add of comments for me is the potential time-saving, in the ideal case (re)acquainting you with a large work of code by guiding your attention from the top-down - firstly to orient you in the broad structural aspects, then to fly you smoothly down towards the minutae of reading individual lines of code.

This does sometimes entail a few 'what'-style comments, so I'm disappointed to regularly read of programmers that have apparently taken up arms against them.

Re:Ask HN: Do you ever go back and admire a piece of code yo... by t-writescode on Friday May 08, 2020 @10:07PM (#23121571)
Are they ‘what?’ or ‘wat?’ comments? Maybe that’s the difference?
Re:Ask HN: Do you ever go back and admire a piece of code yo... by whynaut on Friday May 08, 2020 @10:29PM (#23121693)
what, opposed to ‘why’ or ‘how’
Re:Ask HN: Do you ever go back and admire a piece of code yo... by t-writescode on Saturday May 09, 2020 @03:21AM (#23122909)
No, I understand that part; but, I think of a "what" comment as "incrementing value of X"

a "wat" comment, like the meme, is more of a "this is a really weird blob of code that might be obvious when you break the whole thing apart; but, what it's doing is _this_ this is the implementation of it because we have a complicated data structure that's connecting all these pieces"

Re:Ask HN: Do you ever go back and admire a piece of code yo... by BurningFrog on Friday May 08, 2020 @04:17PM (#23118959)
I think there are levels:

Level 1: Garbage code with no or bad comment.

Level 2: Garbage code with good comments.

Level 3: Good code with comments

Level 4: Code good enough that it doesn't need comments, with rare exceptions.

Each level is better than the previous. You can't level up directly from 1 to 4.

Still, 4 is the best level. I know it sounds weird if you haven't seen it.

Re:Ask HN: Do you ever go back and admire a piece of code yo... by Sevaris on Friday May 08, 2020 @05:56PM (#23119968)
I don't think level 4 exists. There is no code for problems of sufficient complexity that are self-explanatory. There are too many hidden assumptions and foreknowledge and tradeoffs and decisions baked into a block of code that, unless it's trivial, there's no way code itself can reflect it adequately.
Re:Ask HN: Do you ever go back and admire a piece of code yo... by BurningFrog on Saturday May 09, 2020 @11:41AM (#23125636)
I have three things to say about that:

1. I did allow for "rare exceptions". Some things do need to be explained outside of the code. Maybe we're not so different after all :)

2. There are a number of techniques to write such code. Before I learned them, I had NO IDEA. One is to take what would have been comments and use them as variable or function names. This includes (and I had real resistance before I accepted this) breaking out a variable of function only in order to give it an informative name. "Level 4" doesn't just happen. You work at it for a long time and sharpen your skills in that area.

3. To me, much of the art of writing software is to find ways to divide a complex problem into simple pieces. If my code is real complex, I look for simpler way to write it. And I look hard.

Re:Ask HN: Do you ever go back and admire a piece of code yo... by jbotz on Saturday May 09, 2020 @08:26AM (#23124166)
It does exist, but only for snippets, sections of code, or at best whole individual functions, especially in languages that are concise and expressive (which are most languages that are not Java ;-). But even really good code still needs comments about how things fit together, what to find where, etc., because those things are generally not expressible in programming languages, although a good module system can help a lot with that.
Re:Ask HN: Do you ever go back and admire a piece of code yo... by zadler on Friday May 08, 2020 @04:28PM (#23119059)
I still think a file should have at least some small comment at the top to help orient the reader and set expectations.
Re:Ask HN: Do you ever go back and admire a piece of code yo... by woah on Friday May 08, 2020 @01:21PM (#23117163)
I find it's not useful to have comments in code that you are actively working on since they quickly get out of date. They are best added when one finishes work on a block of code.
Re:Ask HN: Do you ever go back and admire a piece of code yo... by scarface74 on Friday May 08, 2020 @03:56PM (#23118788)
I’ll do comments first and then code. It’s a lot faster for me to keep a flow and pseudocode and then write the code in between. If the comments are two pedestrian afterwards (increment i), I will erase the comments.

I started this technique early in my career back in the late 90s after reading “Code Complete”.

It also helps me to pick back up faster if I get interrupted.

Re:Ask HN: Do you ever go back and admire a piece of code yo... by dmckeon on Friday May 08, 2020 @04:23PM (#23119008)
I’ve heard that styled as “do project the document, don’t document the project” but I don’t know a source to attribute this to.

The idea is to write a rough draft that covers what the project is, what it does, the API structure, and how to use the project, then write the code, “projecting the document”, and finally sort out the documentation - comments in the source, everything else in a manual page, article, or research paper.

Re:Ask HN: Do you ever go back and admire a piece of code yo... by pvorb on Friday May 08, 2020 @05:47PM (#23119873)
This sounds a lot like TDD, but with comments instead of tests. It's an interesting technique I never heard of.

Do you ever get back to code you wrote and find a lot of misleading comments that you simply forgot to delete/update? Or are you always updating the comments before you try a new approach if the first one didn't work out?

Re:Ask HN: Do you ever go back and admire a piece of code yo... by scarface74 on Friday May 08, 2020 @09:37PM (#23121398)
The pseudocode comments are way too detailed and too much noise to keep in the code. I’m not going to keep a comment in the code like.

   //increment i 
But also if the comment is something like:

  //Call the API to get a list of customers. 
I’m going to replace it with

  List<Customer> customers= GetCustomers(); 

Use the IDE to create a valid stub function and delete the comments. The method names become self documenting. At any point I have code that compiles.
Re:Ask HN: Do you ever go back and admire a piece of code yo... by hermitdev on Friday May 08, 2020 @02:45PM (#23118051)
At a previous job, when I transitioned off a few projects that I had solely worked on for 7 years, I warned the dev taking over from me: I don't usually comment my code, but when I do, you'd better pay attention.

I think in one of them, only a single source file (C++) had comments, and there were more comments than code. I was explaining an intricate locking pattern around a data cache.

Like you suggested, I added the comments after I'd done the work because it was a work in progress. I added the comments because it was a very fragile piece of code and sensitive to changes (e.g. really easy to cause a deadlock or race condition if changed).

Re:Ask HN: Do you ever go back and admire a piece of code yo... by jannotti on Friday May 08, 2020 @01:48PM (#23117469)
I get the feeling, but I rarely think "I'm completely done now, time to comment!" Better to admit that and document a bit earlier than when it's "finished".
Re:Ask HN: Do you ever go back and admire a piece of code yo... by lanstin on Friday May 08, 2020 @03:03PM (#23118229)
More accurate comments that way, isn't it. Nothing sadder than the optimistic comment from before implementation, which took the regular twisty path towards correctness in the corner cases. More of a risk for the higher level methods than the little "DRY" functions that do something relatively functional.
Re:Ask HN: Do you ever go back and admire a piece of code yo... by miketuritzin on Friday May 08, 2020 @05:00PM (#23119408)
I frequently think just before committing/pushing, "Time to add some comments!" That doesn't mean the code is fully finished, but it does provide a clear point in time to add them (when needed).
Re:Ask HN: Do you ever go back and admire a piece of code yo... by ekimekim on Friday May 08, 2020 @03:13PM (#23118328)
I think this is a matter of coding style.

When I'm writing something I'm generally "top down", breaking the problem into sub-tasks. In this mindset I'll often start a sub-task with a comment describing what it needs to do, and then set out to do it. If it turns out I was wrong about what I needed to do then I update the comment, but that's not that common.

Re:Ask HN: Do you ever go back and admire a piece of code yo... by 6510 on Friday May 08, 2020 @03:06PM (#23118251)
While working on it my comment explains what the block should be doing. Extra points for lists of harebrained ideas that will likely never happen.
Re:Ask HN: Do you ever go back and admire a piece of code yo... by mywittyname on Friday May 08, 2020 @11:32AM (#23115882)
Especially when it comes to data structures. It's so helpful to see a comment with an example of the structure of a variable, especially because you won't have the same context when you come back to it in a few months.

I know some people don't feel that way, because I've been dinged for it in code reviews as something that should be covered in unit tests. But I think digging through unit tests to glean the structure of an object adds a lot of overhead.

Re:Ask HN: Do you ever go back and admire a piece of code yo... by cpascal on Friday May 08, 2020 @01:31PM (#23117277)
On a bit of a tangent here, but good commit messages are another thing I really appreciate 6 months later.
Re:Ask HN: Do you ever go back and admire a piece of code yo... by delusional on Friday May 08, 2020 @01:42PM (#23117402)
I love good commit messages as well, but it can be hard to convince people to write them. I always hear "Why? i don't use them" which is self propelling. You don't have good messages, so you don't read them when you want to know something, so you don't write good commit messages.
Re:Ask HN: Do you ever go back and admire a piece of code yo... by saagarjha on Friday May 08, 2020 @04:58PM (#23119374)
I don't really do comments all that often (generally, only for "Chesterton's fence-esque" things) but I do really rely on good commit messages. They are really helpful when going back in history and trying to get a general idea why something changed.
Re:Ask HN: Do you ever go back and admire a piece of code yo... by hermitdev on Friday May 08, 2020 @02:56PM (#23118158)
Another thing I like to put in commit messages is the ticket# (of what ever bug tracker you're using) of the feature/bug task that drove the change.
Re:Ask HN: Do you ever go back and admire a piece of code yo... by Beltiras on Friday May 08, 2020 @11:16AM (#23115713)
I always advise juniors to document their code because they never know, next maintenance programmer to touch it might be themselves. I can't remember why I wrote code a week ago, let alone 6 months.