Why you shouldn't write comments

Posted by vegan_antitheist@reddit | learnprogramming | View on Reddit | 5 comments

The Java channel just posted this short:
https://www.youtube.com/shorts/q5EuaH5n65g

I think the reason why beginners write such useless comments is because tutorials use them to describe what the code does. But you should never do that in your own code. I rare situations you might use a commend to explain why it is like that.

Sometimes you describe a technical detail that is irrelevant for javadoc because you could just as well do something else to produce the correct result. You try to make it obvious but a quick note to why it was solved like this might be useful.

Sometimes it's just about something not being obvious.
For example if you do getFirst() on a stream and it's not obvious why the first element is the right one because sorting was done somewhere else. In that case you might want to explain that. And the method the generates the sequence must have a javadoc explaining the order that can be expected. But even better would be an assertions, where you check that it's really the expected item or that the order of the list if correct. Assertions are even more misunderstood than comments. They don't replace unit tests. They replace comments.

Whenever you want to write a comment, do this: