News for hackers. Stuff that matters.
Re:On Comments in Code by Nicksil on Tuesday June 15, 2021 @02:54PM (#27519866)
>Codes needs "why" comments, not "what" comments.

Indeed. While there are exceptions, "what" comments will usually only add noise thus making things more difficult.

This isn't necessary:

    // Total count
    int total_count = 0;
Re:On Comments in Code by nicoburns on Tuesday June 15, 2021 @03:13PM (#27520116)
I find it's often useful to put a 'what commment' on a block of a few (2-10) lines of code. Then you can skim through the function without reading every individual line of code.
Re:On Comments in Code by isbvhodnvemrwvn on Tuesday June 15, 2021 @03:23PM (#27520243)
You might as well be able to extract a method with a meaningful name then.
Re:On Comments in Code by ziml77 on Tuesday June 15, 2021 @03:52PM (#27520627)
Please no. Don't extract methods methods just to avoid using a comment. You end up making code more difficult to follow when you do that since you're now jumping somewhere else in the file and you're adding a lot of noise by having to pass the function's current state along as arguments to the new function.
Re:On Comments in Code by hackinthebochs on Tuesday June 15, 2021 @11:00PM (#27524404)
>by having to pass the function's current state along as arguments to the new function.

But then the state being read and mutated by a block of code is explicit. The alternative is having to inspect the code to determine if and what state is being manipulated. The more code in a functional unit, the harder this gets.

Re:On Comments in Code by nicoburns on Wednesday June 16, 2021 @02:19PM (#27531721)
If you're mutating variables then I'd agree that it might be best to encapsulate that in a function. But I'd argue that except for performance sensitive code, that's bad practice. The vast majority of code I write has 95% immutable variables. So even in a large function they'll typically be at most one or two mutable variables to keep track of (and very often zero).
Re:On Comments in Code by nicoburns on Tuesday June 15, 2021 @05:35PM (#27521824)
That then makes the code non-linear, which IMO makes it a lot less readable in most circumstances.
Re:On Comments in Code by isbvhodnvemrwvn on Tuesday June 15, 2021 @05:45PM (#27521909)
It depends, if it needed a comment in the first place it likely was at a different (likely lower) level of abstraction than the rest of the code in that method.
Re:On Comments in Code by nicoburns on Tuesday June 15, 2021 @06:20PM (#27522298)
Yes, but that doesn't mean it doesn't benefit from being inline. I love being able to skim over the paragraph-comments to get the big picture and then dip into the details without having to jump to another file / part of the file and lose my context.
Re:On Comments in Code by firebaze on Tuesday June 15, 2021 @03:50PM (#27520599)
To me it's actually faster and easier to read code instead. This is because i generally don't trust "what" comments - either I know the code already or I don't, and if I don't, then I don't trust the "what" comment fully, so I both have to parse "what" and code.

A "why" comment usually ages well, so I generally trust it and can use it to better parse the code and its intentions.

So I belong in the "what only for API methods" camp.

Re:On Comments in Code by nicoburns on Tuesday June 15, 2021 @05:36PM (#27521831)
Do you also read through every function call (at least the first time you encounter a given function)?
Re:On Comments in Code by majewsky on Wednesday June 16, 2021 @07:01AM (#27527079)
This is Damian Conway's "coding in paragraphs" advice, and in my experience, it's the single best advice for more readable code. When you have a large function, split the code into blocks with a heading comment on each section. That way, you can skim through the function body quickly by just reading the headings, then dive deeper into whatever block you're interested in. Like section headings on an article.
Re:On Comments in Code by bostonsre on Tuesday June 15, 2021 @06:36PM (#27522487)
I do something like this, but I write them as debug log statements so that you get the additional benefits from logging.
Re:On Comments in Code by nicoburns on Wednesday June 16, 2021 @02:20PM (#27531738)
Yes, I've also done this in places. It works pretty well, although I wish I could get my syntax highlighter to de-emphasise these lines like it does with comments.
Re:On Comments in Code by jbay808 on Tuesday June 15, 2021 @03:25PM (#27520255)
What would be your opinion of this comment, though?

  // Reset the counter for the next run
  total_count = 0;
Re:On Comments in Code by Normal_gaussian on Tuesday June 15, 2021 @06:05PM (#27522134)
Ignoring the pedantry elsewhere, then sure this comment explains what you are doing.

The contention is whether it is necessary or common enough to be assumed. In most places this would be self evident and should be avoided to reduce mental load, however I have seen and done exactly this many times - in distrubuted or embedded systems where the control is neither synchronous or co-located (ie, coming in from interrupts / messages). In such places there may even be call for more description, however presuming the context is appropriate this could be correct.

I would imagine a game with a react redux style state engine might result in a comment like this as well.

Re:On Comments in Code by rrose on Tuesday June 15, 2021 @03:28PM (#27520301)
it feels like this suggests a design flaw. You should probably be creating a new local `total_count` variable for each run that goes out of scope at the end of the run, rather than reusing the same variable over and over again in different contexts
Re:On Comments in Code by wongarsu on Tuesday June 15, 2021 @04:01PM (#27520739)

  // reuse counter for performance, this brings 2% speedup
  total_count = 0;
would be a quite useful why comment, if not creating a new variable each time is intentional
Re:On Comments in Code by rrose on Tuesday June 15, 2021 @05:00PM (#27521435)
sure, but reusing an integer variable is never going to bring a 2% speedup. allocating a new variable of almost any type is not going to cause any performance issues, at least if it is allocated on the stack, and if the construction of the variable is particularly expensive, resetting it probably will be too.

I would need to see a realistic example to believe that something like this would ever be a useful comment.

Re:On Comments in Code by hnlmorg on Tuesday June 15, 2021 @06:05PM (#27522129)
I feel like you're getting bogged down on the literal code and missing the point of a hypothetical example.
Re:On Comments in Code by _old_dude_ on Tuesday June 15, 2021 @06:02PM (#27522095)
In a compiled language, this is true for a local variable in general, it costs nothing because the compiler uses a register for it (or at worst spill it on stack but the stack frame size is reserved once for all stack allocated variables when the function is called).

The story is different, if the variable is captured by a closure/lambda, the current stack frame or part of it may be allocated on heap, leading to perf issues.

Re:On Comments in Code by slavik81 on Wednesday June 16, 2021 @03:16AM (#27525781)
> the stack frame size is reserved once for all stack allocated variables when the function is called

Is there somewhere that's documented for LLVM? That's my expectation of what the final compiler output should be, but I read that the IR allocates every variable dynamically, and then they optimize away whatever they can. I haven't been able to figure out how or where it's guaranteed that all allocations will be coalesced.

I wouldn't normally worry about it, but I've talked to folks who don't believe me when I say it doesn't matter whether you declare an integer inside or outside of a loop. It would be nice to be able to explain exactly why it can never matter.

Re:On Comments in Code by _old_dude_ on Wednesday June 16, 2021 @04:47AM (#27526331)
I don't know LLVM well enough, but you can play with godbolt

By example, with https://godbolt.org/z/9Kv7oo9oK you can see that values goes into registers (and that thank to 'lea' there is not many registers used).

And if you remove the option -O2, values are spilled on stack.

Re:On Comments in Code by latchkey on Tuesday June 15, 2021 @03:28PM (#27520291)
why are you resetting the counter?
Re:On Comments in Code by dlp211 on Tuesday June 15, 2021 @04:54PM (#27521363)
For the next run. Why is in the comment, it's likely a design flaw though and no comment is going to help here. See comment about using proper scoping.