Live data from Hacker News

Writing Well-Documented Code – Learn from Examples

codecatalog.org

31–40 of 153 posts

Re: Writing Well-Documented Code – Learn from Examples

#32
post #11

I have a simple rule to go by. Comments should describe “why” and the code should describe “what”.

I agree and first example in the article is all comments describing what the code is doing. There is not a single one explaining why.

Well I stopped reading further.

Re: Writing Well-Documented Code – Learn from Examples

#33
The code should explain what it's doing (self documenting code) and tests should explain why it's doing it.

Comments tend to just become a place for misinformation or get disconnected from the actual logic.

Adding more comments sometimes doesn't clarify the situation, it just acts as a second source of truth.

Re: Writing Well-Documented Code – Learn from Examples

#34
post #22

Earlier quoted context omitted.

Maybe it's a buggy HTTPS redirect in GP's browser? Due to HSTS or something? The HTTP version of your URL works fine. The HTTPS version has an expired cert. If I click past that, it redirects to redis.io, then gives a 404. I know antirez is a smart guy, but if this is some kind of "I'm taking a bold stance against HTTPS" then I'd rather read the archive.org link with functioning HTTPS. My threat model prefers trustin…

Let's hear more about your threat model that causes you to wory about nefarious content being injected into a blog post about comments in code

A bit snarky but I agree, it doesn’t seem like a huge threat. I’d love to be proven wrong though.

Re: Writing Well-Documented Code – Learn from Examples

#35

Speaking of source code comments, antirez (of Redis fame) wrote a fantastic article[0] about that topic some time ago and I still recommend it to colleagues whenever they make the hollow statement that "code should and can be intelligible on its own, without any comments". [0]: https://web.archive.org/web/20210226004600/http://antirez.co... (I still don't understand why he deleted his blog)

I agree with you. Code should be intelligible on its own without comments. It should also be well documented. One does not preclude the other.

Re: Writing Well-Documented Code – Learn from Examples

#36
Personally I found most of those examples to be not great. I think they would be improved better names overall, more well named private methods and most importantly well structured companion test classes proving the behavior.

I really dont want to read another developers comments on their mental state while I am feverishly debugging their code during a 3am page.

The number of times I have been misled by an outdated comment. I generally open git blame to see if the comments predate any code now. Tests on the other hand almost never lie.

Readmes or comments explaining why something exists vs what it allegedly does are generally much better. If you can’t write expressive code why would you assume your English is much better?

Re: Writing Well-Documented Code – Learn from Examples

#37
post #33

The code should explain what it's doing (self documenting code) and tests should explain why it's doing it. Comments tend to just become a place for misinformation or get disconnected from the actual logic. Adding more comments sometimes doesn't clarify the situation, it just acts as a second source of truth.

> The code should explain what it's doing (self documenting code) and tests should explain why it's doing it.

I agree at a surface level, however:

  - this makes us assume that there are tests in the first place
  - this makes us assume that someone will read these tests instead of asking the developer
  - this makes us assume that these tests will be readable enough on their own to replace free form text
  - this makes us assume that these tests will even be able to contain a representation of why the code exists
  - personally, i have never seen a test that explains a business requirement with enough context not to miss out on details
  - i have also never seen code that encapsulates these requirements 1:1, without getting too mixed up in implementation details
I think that code definitely should describe what it's doing, tests should explain how it's doing it as far as possible (though this requires discipline to have loosely coupled and testable code, which isn't always the case) and that there still should be comments that explain the realities of needing technical solutions for people problems and all of the aspects that are carried with this.

Doing anything less feels like giving ourselves constraints that will lead to suboptimal results because the tooling and testing solutions aren't quite there yet. Maybe when frameworks and languages will become more expressive, we'll be able to live without comments. However, that day may not come anytime soon.

Nor will companies be interested in the overhead that writing extensive and good tests entail, nor will they want to wait for their engineers to figure out how to convey everything in computer code, as opposed to just dropping some comments into the codebase, right next to the actual code that they concern. Maybe when companies are ready for 50% of the development time to be spent on testing software, that won't be an issue. That day may also not come anytime soon.

As a possible counterpoint, look at RSpec, i think it's headed in the right direction: https://rspec.info/

Re: Writing Well-Documented Code – Learn from Examples

#38
Well documented code is respectful of the maintainer's time. Nobody wants to read a monologue full of noise to understand what is happening.

Comments are not monologues. Explain what the line you are commenting does, and perhaps elaborate why. But always acknoweledge: more words = more patience required = more maintenance cost.

Also, nobody will care about what your name is in 2 years. Do not make the code about you.

Re: Writing Well-Documented Code – Learn from Examples

#39
post #5

some types of comments are redundant. These days I write my code as comments first and then write the code. But for example this one // If either token bucket capacity or refill time is 0, disable limiting. if size == 0 || complete_refill_time_ms == 0 { return None; } The comment is the same as the code. It is pointless. Instead I might say under what circumstances I expect them to be zero or why Im disabling limitin…

I don't think the comment is the same as the code. If "size" was called "token_bucket_capacity" I would agree but since it's not, if you remove the comment you lose some context.

Re: Writing Well-Documented Code – Learn from Examples

#40

Well documented code is respectful of the maintainer's time. Nobody wants to read a monologue full of noise to understand what is happening. Comments are not monologues. Explain what the line you are commenting does, and perhaps elaborate why. But always acknoweledge: more words = more patience required = more maintenance cost. Also, nobody will care about what your name is in 2 years. Do not make the code about you.

On the other hand, if something is missing in the comments, it could take days to find out exactly what you need. Maybe a "level of comments" like log levels could help here?
Post reply on HN