Most active commenters
  • marginalia_nu(4)

←back to thread

1457 points nromiun | 18 comments | | HN request time: 0.222s | source | bottom
1. Buttons840 ◴[] No.45075079[source]
It's been said: "Document the why, not the what."

I have a hard time separating the why and the what so I document both.

The biggest offender of "documenting the what" is:

    x = 4  // assign 4 to x
Yeah, don't do that. Don't mix a lot of comments into the code. It makes it ugly to read, and the context switching between code and comments is hard.

Instead do something like:

    // I'm going to do
    // a thing. The code
    // does the thing.
    // We need to do the
    // thing, because the
    // business needs a
    // widget and stuff.
    
    setup();
    t = setupThing();
    t.useThing(42);
    t.theWidget(need=true);
    t.alsoOtherStuff();
    etc();
    etc();
Keep the code and comments separate, but stating the what is better than no comments at all, and it does help reduce cognitive load.
replies(6): >>45075259 #>>45075314 #>>45075395 #>>45076149 #>>45079235 #>>45080058 #
2. marginalia_nu ◴[] No.45075259[source]
I generally don't mind documenting both when it's merited. Sometimes you need to clarify the why, occasionally you need to clarify the what.

I think comments in general are underrated. You don't need to annotate every line like a freshman programming assignment, but on the other hand most supposed self-documenting code just isn't.

replies(1): >>45075299 #
3. mastermage ◴[] No.45075299[source]
sometimes you do some wack magic in just one line of code, sometimes thats necessary for performance or because what you are trying todo is inherently wack magic. Example the fast inverse square from quake. Insane magic and if you just document does inverse square approximately people would freak out. So sometimes when wack magic is used explain the wack magic (as concise as reasonable)
replies(1): >>45075386 #
4. mastermage ◴[] No.45075314[source]
I am trying to shift more to this style of commenting. Because I am not a programmer by education. I am a physicist and most physicists are like I want comments in my code so do it like this.. and then you as a student do what you need to for a good grade.
5. marginalia_nu ◴[] No.45075386{3}[source]
Yup.

I've got a function the gist of which is

  if (!cond())
    return val;

  do {
    // logic
  } while (cond());

  return val;

This looks like it could be simplified as

  while (cond())
    // logic
  }
  return val;
But if you do you lose out on 20% of performance due to branch mispredictions, and this is a very hot function. It looks like a mistake, like the two are equivalent, but they are actually not. So it gets a comment that explains what's happening.
replies(1): >>45075647 #
6. dsego ◴[] No.45075395[source]
> x = 4 // assign 4 to x

Ah, the chat gpt style of comments.

> Instead do something like:

The only negative is that there is a chance the comment becomes stale and not in sync with the code. Other coders should be careful to update the comment if they touch the code.

replies(1): >>45075474 #
7. Buttons840 ◴[] No.45075474[source]
If the what becomes stale, you can tell. If the why becomes stale (and it can become stale), you'll never know, unless the what is also included.
replies(2): >>45075786 #>>45076785 #
8. brokencode ◴[] No.45075647{4}[source]
That feels like.. something the compiler should be optimizing for you? I would certainly be among those questioning this code.
replies(1): >>45075671 #
9. marginalia_nu ◴[] No.45075671{5}[source]
The compiler can't know from the code alone which branch is more likely. This is a property of the input data and not the code. Really advanced JIT compilers can sometimes do those types of optimizations, but this is a fairly rare scenario.
replies(2): >>45076830 #>>45076966 #
10. marcosdumay ◴[] No.45075786{3}[source]
The reason you want people to document the "why" is because you can easily check if one reason has become stale, but you can never check if every single possible reason is still valid.
11. Waterluvian ◴[] No.45076149[source]
The challenge I have these days is deciding what belongs in code and what goes into design documentation and technical manuals.

I generally find that comments in code should explain why the code is doing non-obvious things. “This gets memoized because it’s actually important to maintain referential identity for reason X.”

replies(1): >>45076928 #
12. fcantournet ◴[] No.45076785{3}[source]
The why becoming stale is a feature, that's when you know there is a VALID reason you thought this looked weird and convoluted, instead of you completely missing the inherent complexity of the problem.
13. jijijijij ◴[] No.45076830{6}[source]
Isn't branch prediction mostly a CPU thing? Do you have an example with corresponding assembly?

It's not that I don't believe you about the performance impact, as I have observed the same with e.g. Rust in some cases, but I don't think it has a lot to do with the compiler judging what's more likely, but rather more or less "random" optimization differences/bugs. At least in my case, the ordering had nothing to do with likelihood, or even had a reverse correlation.

I think in your example a compiler may or may not realize the code is semantically equivalent and all bets are off about what's going to happen optimization-wise.

I mean, in the end it doesn't matter for the commenting issue, as you are realistically not going to fix the compiler to have slightly more readable code.

replies(1): >>45076924 #
14. marginalia_nu ◴[] No.45076924{7}[source]
I don't have an assembly output for this particular case, but how I understand it is that the re-write basically turns it into two separate conditions, which means the branch predictor is free to model their outcomes separately.

In this case, the data is bimodal, depending on the chosen input, two likely outcomes exists. Either no looping is needed, or much looping is needed. This seemingly confuses the branch predictor when it's the same branch dealing with both scenarios.

15. jijijijij ◴[] No.45076928[source]
The problem is, a year later the obvious things are not obvious anymore :D
16. whitehexagon ◴[] No.45076966{6}[source]
The other day I spotted Zig has @branchHint, not tried it yet, my code isnt that hot!
17. ivanjermakov ◴[] No.45079235[source]
Such comments are never maintained. Implementation will change 5 times this year and the comment will get stale and confusing.

At my current projects we drop all code comments except for some really tricky logic and very high level docs.

18. jmpeax ◴[] No.45080058[source]
> It makes it ugly to read, and the context switching between code and comments is hard.

That's a symptom of bad syntax highlighting, fix it and you're good to go.