Also; keep it clean. Don’t over-comment, but comment.
Also; keep it clean. Don’t over-comment, but comment.
Moreover, when a lot of people are asked to comment they write stuff like "this function does x to y" when the function is named "x_to_y". No shit sherlock comments I call them.
I'd always prefer to have good comments, but I've worked on a lot of code bases where no comments wouldn't really have been any worse.
https://github.com/python/cpython/blob/master/PC/python_uwp....
I'd say that redis is probably a better example of gold standard clean C code, though, and they follow the rule of "don't comment unless it's decidedly non-obvious" pretty assiduously:
https://github.com/antirez/redis/blob/unstable/src/ae_epoll....
Eg, to me this is well-commented and I wouldn't call it sparse.
https://github.com/antirez/redis/blob/unstable/src/latency.c
It's not always the intent that needs clarification.
"We would add support for this motherboard that looks a lot like the one we do support, but it doesn't offer feature X."
"We want to add more detailed logs here but we haven't figured out how to cope with the performance hit."
Closely related, comments are also useful for explaining why code that looks wrong/poorly optimized/obsolete isn't actually, which is something that the code itself can't explain.
Writing good readable code is hard, and you don't get it just by banning comments.
Much like how you don't lose weight by buying smaller sized clothes :)
If, in a brief sentence, written in plain English (or any other language of your choice), you can't explain the idea, you need to "go back to the drawing board." Resist the urge writing it in a programming language before you can explain it in English.
When you're done writing the function, go back to the docstring, revise it; in some cases, you can even remove it.
The purpose of a docstring is to help humans to quickly scan the code and discern the meaning, the idea of the function without having to read its source. Like a trailer for a movie - it should give you the general idea, but not give away the implementation details.
Think about docstrings as "type annotations" for humans. We use types and type hints "to help the compiler understand our code better" because computers do not understand plain English, but very often, we have no empathy for fellow programmers.
There have been a number of cases where, for instance, I had to call a bizarrely named API and do something unexpected in order to get an unintuitive outcome and in that case a "what" comment isn't such a bad idea.
The beginnings of functions, for instance, are great places to comment, perhaps even including a definition of some of the subroutines within it.
In the case of an exceptionally large subroutine, commenting within the subroutine may be advantageous - but at that point you may consider another function to replace that larger chunk.
Variable names that are perhaps obscure and are not practical to rename for whatever reason can also use comments.
Class headers are of course also a great place to comment, to explain the purpose of the class.
Good comments can lead to better code. :)
I don't think what you're asking for is possible. It's an inherently subjective standard.
If an objective definition were possible, then it'd be possible to write a static analysis tool that reads both your code and the comments and flags "over-commenting on line 23, under-commenting on line 457". That could be done with sufficiently advanced AI, but at that point it would just be the subjective opinion of the tool's author being enforced.
Literate programming comes to mind as an extreme counter example of not enough comments. https://en.wikipedia.org/wiki/Donald_Knuth#Literate_programm...
//increment i in for loop
For (i=0;i++....
Var x; //define variable x
X=4; //give value of 4 to x
Everyone knows what that code does. The comments aren't necessary.So. Weird. The other responses didn't properly implement the requirement specification.
I am so confused.
Nothing wrong with that!
I've seen it happen many times now. A nice, clean, easy-to-read code base turns into a forest of unreadable shit thanks to the introduction of parsed comment tags. Perl, Ruby, Python, JS. Doesn't matter what tool or language.
Please, people. I beg you. Stop putting this shit in code. Not everyone is using the same bloated IDE as you, nor do we care to maintain your silly block text and parameter text that is outdated the day you wrote it. If your function needs a block text to explain how to use it, you need to find a new job. I'm serious. You're not good at your job. If it's not self-evident what the file you're looking at does and the function within the file does, then refactor it. Cut it down. Make it make sense.