this post was submitted on 10 Aug 2024
145 points (95.0% liked)
Programming
17432 readers
153 users here now
Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!
Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.
Hope you enjoy the instance!
Rules
Rules
- Follow the programming.dev instance rules
- Keep content related to programming in some way
- If you're posting long videos try to add in some form of tldr for those who don't want to watch videos
Wormhole
Follow the wormhole through a path of communities [email protected]
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
If those functions are huge units of work or pretty complex, I can agree. For most cases though, a simple code comment should do to explain what's going on?
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.
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.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.