I disagree about comments should never be about what is being done. If what is being done is not obvious then they’re important. Take assembly code as an example. Or complicated bit operations. I agree the why is more important to document than the what but saying the what is never important seems misguided.
Also, this may be a semantics thing, but oftentimes the code’s specification is in doc comments. I don’t believe you’re claiming code shouldn’t ever have specifications, this isn’t meant as a gotcha lol.
A function name can be misleading just like a comment can, in the same scenarios and for the same reasons, plus it’s harder to update because you have to change it in at least two places.
And yet, outdated comments are far, far more common than outdated function names.
Because people don’t try to squeeze a complete description of what a function does into a single identifier, which is what you you would have to do if you want function names to take the place of comments. I for one don’t want to strip all the spaces and punctuation out of my comments so I can use them as function names, and I really didn’t want to read someone else’s code written in that style.
I think it’s good to document why things are done, but extracting things out into another function is just documenting what is being done with extra steps. This also comes with a number of problems:
Not all languages are readable. Documenting what is being done is important in some C, or when working with some libraries that have confusing usage syntax.
Not all people reading the code know the language or libraries well. Those people need guidance to understand what the code is trying to do. Function names can of course do this, but…
Not all types can be named in all languages. Some languages have a concept of “opaque types”, which explicitly have no name. If parameter and return types must be specified in that language, working around that restriction may result in unnecessarily complicated code.
Longer files (the result of having dozens of single-use functions) are less readable. Related logic is now detached into pointers that go all over the file all because of an allergic reaction to code comments, where a simple // or # would have made the code just as readable.
Function names can be just as outdated as code comments. Both require upkeep. Speaking from personal experience, I’ve seen some truly misleading/incorrect function names.
Comments should never be about what is being done. They should only ever be about why it is being done.
If you write your code like suggested in the book, you won’t need to rely on possibly outdated comments to tell you what’s going on.
Any comment about “what is being done” can be replaced with extracting the code in question to a separate, well-named method.
I disagree about comments should never be about what is being done. If what is being done is not obvious then they’re important. Take assembly code as an example. Or complicated bit operations. I agree the why is more important to document than the what but saying the what is never important seems misguided.
Also, this may be a semantics thing, but oftentimes the code’s specification is in doc comments. I don’t believe you’re claiming code shouldn’t ever have specifications, this isn’t meant as a gotcha lol.
You’re talking about assembly in a thread about OOP…
I think commenting what can be important in OOP too though.
A function name can be misleading just like a comment can, in the same scenarios and for the same reasons, plus it’s harder to update because you have to change it in at least two places.
And yet, outdated comments are far, far more common than outdated function names.
Also, if you’re changing a comment which explains the “what”, you should likely change the method name, as well.
It’s important for the client to know what the method does by looking at the name, so why would you duplicate your effort?
Because people don’t try to squeeze a complete description of what a function does into a single identifier, which is what you you would have to do if you want function names to take the place of comments. I for one don’t want to strip all the spaces and punctuation out of my comments so I can use them as function names, and I really didn’t want to read someone else’s code written in that style.
I think it’s good to document why things are done, but extracting things out into another function is just documenting what is being done with extra steps. This also comes with a number of problems:
//
or#
would have made the code just as readable.