I have never cursed an author for having too many comments. There are many cursed developers out there after my long career.
On Comments in Code
41–50 of 238 posts
Re: On Comments in Code
#42Earlier quoted context omitted.
>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;
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.
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
#43Code is for computers to make them do exactly what you want them to do, comments are for your co-workers (and you) to make them understand what you actually meant to achieve.
All code is literature intended exclusively for humans. Computers read machine instructions; they're incapable of reading code. Know your audience.
Re: On Comments in Code
#44Documentation, in general, could use all the help it can get.
I wrote up a long piece on this[0]. No one will read it, because it's long. I've found that no one reads anything that is more than about a "7 minute read," these days. Part of my documentation problem, is that I can get too verbose. It's not a good thing.
[0] https://littlegreenviper.com/miscellany/leaving-a-legacy/
Re: On Comments in Code
#45Earlier quoted context omitted.
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.
You might as well be able to extract a method with a meaningful name then.
Re: On Comments in Code
#46I have a personal rule: If I need a comment, it goes at the beginning of the function. This neatly groups the comment with the code it applies to. Occasionally I have to make a function "just" for this grouping purpose, but that's fine.
This is also my practice. Any code that is complicated enough to require a special comment is also complicated enough to be its own function.
Re: On Comments in Code
#47Earlier quoted context omitted.
All code is literature intended exclusively for humans. Computers read machine instructions; they're incapable of reading code. Know your audience.
Humans wouldn't care so much about semicolons, but I do want the compiler to understand my code, too.
Re: On Comments in Code
#48On the contrary, I find standard JSDoc and its variants to be an excellent tool for internal documentation. With hovering support in modern editors, it allows context explanation in a very streamlined and human way. The author mentions for preconditions to just “read the code”. I consider this bad advice. If using an external library, would you rather hover over the method and see its conditions, or would you rather…
Take this real example:
/**
* change the temperature set point in use by the thermostat
*
* @param ctx the thermostat context
* @param sp the new set point in 0.1 celsius degrees
* @return 0 on success,
But we can change around the name of the parameters like this and use proper strict types: /**
* change the temperature set point in use by the thermostat
*
* @param thermostat_ctx the thermostat context
* @param set_point the new set point
* @return SUCCESS on success, otherwise an error code
*/
error_t update_temperature_set_point(thermostat_ctx_t *thermostat_ctx, celsius_degree_t set_point);
And thus the doc comment now it's useless and can be removed, leaving only: thermostat_error_t update_temperature_set_point(thermostat_ctx_t \*thermostat_ctx, celsius_degree_t set_point);
And in a codebase, more than 95% of the comments would be like that. There are the exception where you need to explain something in more details. In that case you first should ask yourself if there is really not a better way, and if not in that case the doc comment is fine. In all the other cases, it's probably not.Doc comments on the other hand really makes the code less readable, the increase the number of lines for most of the times not saying anything useful at all.
Re: On Comments in Code
#49http://jeremymikkola.com/posts/2021_03_21_useful_comments.ht...
Re: On Comments in Code
#50On the contrary, I find standard JSDoc and its variants to be an excellent tool for internal documentation. With hovering support in modern editors, it allows context explanation in a very streamlined and human way. The author mentions for preconditions to just “read the code”. I consider this bad advice. If using an external library, would you rather hover over the method and see its conditions, or would you rather…
If the name of the function and the parameters are clear, documenting the code seems useless. Take this real example: /** * change the temperature set point in use by the thermostat * * @param ctx the thermostat context * @param sp the new set point in 0.1 celsius degrees * @return 0 on success, But we can change around the name of the parameters like this and use proper strict types: /** * change the temperature set…