Thoughts? It does feel like there’s a lot of things you can do in comments that would be impossible or impractical to do in names alone, even outside of using comments as documentation. There’s certainly much more information that you can comfortably fit into a comment compared to a name.
One of the comments in the Lobste.rs post that I got this from stuck out to me in particular:
Funny story: the other day I found an old zip among my backups that contained the source code of game that I wrote 23 years ago. I was just learning to code at the time. For some reason that I forgot, I decided to comment almost every single line of that game. There are comments everywhere, even for the most obvious things. Later on, I learned that an excess of comments is actually not considered a good practice. I learned that comments might be a code smell indicating that the code is not very clear. Good code should be so clear, that it doesn’t need comments. So I started to do my best to write clear code and I mostly stopped writing comments. Doing so only for the very few parts that were cryptic or hacky or had a very weird reason for being there.
But then I found this old code full of comments. And I thought it was wonderful. It was so easy to read, so easy to understand. Then I contrasted this with my current hobby project, which I write on an off. I had abandoned it for quite some months and I was struggling to understand my own code. I’ve done my best to write clear code, but I wish I had written more comments.
And this is even worse at work, where I have to spend a ton of time reading code that others wrote. I’m sure the authors did their best to write clear code, but I often find myself scratching my head. I cherish the moment when I find some piece of code with comments explaining things. Why they did certain things, how their high level algorithm works, what does this variable do, why I’m not supposed to make that change that looks like it will simplify things but it will break a corner case.
So, I’m starting to think that this idea that comments are not such a good practice is actually quite bad. I don’t think I can remember ever reading some code and thinking “argh so many comments! so noisy” But, on the other hand, I do find myself often in the situation where I don’t understand things and I wish there were some more comments. Now I’m trying to write comments more liberally, and I think you should do the same.
I guess that’s a generalization of the op’s idea.
Pointless or uninformative comments are not good, regardless of the quantity.
Useful and informative comments are always good, regardless of the quantity.
When I’m looking at someone else’s code, I want to see extensive, descriptive comments.
That hits me like something a teacher tells you in a coding class that turns out to be nonsense when you get to the real world.
I’m not sure how others do it.
As I’m coding, the comments form part of my plan. I write the comments before the code. As I discover I’ve made incorrect assumptions or poor decisions, I correct the comments with the new plan, then correct the code to match the updated comments.
As a final step in coding, when I feel it is complete, I’ll review comments to determine what should remain to help future me if I ever have to dig into it again.
Variable names should be reasonably memorable and make contextual sense, but that’s it. That’s what they exist for. Don’t overload the purpose of anything I’m the code.
In a company I work in, we have “no comments policy” for at least ~10 years now and we are not planning to change that. It’s not just theory, we work like this in practice and purpose of each part of code is perfectly understandable just from variable names, file names, namespaces, function names.
// Increment i i++;
Very info. Much useful.
Congratulations, you figured out to do comments the wrong way.
You also figured out how to use a bad, unclear variable name, so should we also stop naming variables with sensible words since it can be done wrong?
External documentation can also be done badly, so let’s stop doing that too.
Or what’s your point?
Something’s you leave out but let’s say instead you are using some enumerator like in Python over a list of some objects. Sure you can use “i” but what if it’s a list of apples then why not make the iterator “apple”
For Apple in apples
Simple example but the concept can go a long way