916
Always write comments
(lemmy.world)
Welcome to Programmer Humor!
This is a place where you can post jokes, memes, humor, etc. related to programming!
For sharing awful code theres also Programming Horror.
Unit tests should be the parity check for other code. Those don't get outdated with the next refactoring where someone didn't remember to also adjust all the comments.
Why would people be better at judging if something needs a comment than at judging if something needs a better name or refactoring? Asking people to skip that judgement step and comment everything just gives you a bunch of useless boilerplate comments. That trains everyone reading that codebase to ignore comments because they are almost always useless anyway.
At least this we can 100 % agree on. Documentation should be as close as possible to the code. (I just think most of the time that place is in the name of things, not in an actual comment.)
You can't be too obvious when writing code. Good names, good code structure, unit tests and comments are not mutually exclusive. And comments are the cheapest part of all of that. Commenting what a method does takes a fraction of the time that good code structure or unit tests take, so adding a comment is rarely not worth the effort.
Again, who is asking them to skip judgement? I am saying, do comments additionally.
And forcing programmers to do anything they don't understand or are against will always result in useless results. I've seen enough unit tests that don't actually test what the tested code does. I've seen nonsense comments. I've seen naming that "follows" the guideline but is totally useless or even wrong. I have even seen code that was implemented in spite and where the code superficially checks all the boxes but is totally terrible once you peek beyond that. Same goes for the whole process. If a programmer doesn't care for code reviews, you get nothing by forcing the programmer to do them.
Does this mean all of these things are useless, just because programmers can purpously sabotage them and find loopholes that fit the guidelines? I don't think so.
If you can fit all the information about what something does plus all the specifics, edge cases, non-obvious things and the reason why this specific approach was taken in maximum 4 words, then yes. Otherwise comments are the way to go.
Having no documentation is fine for a small project with one dev that will get tossed in a year. But if you are making something that will actually be used for longer, than it's not ok.
If you're writing an explanation of what your code does then, ding ding, you're writing code. If your code has so many side effects and garbage in it that it's incomprehensible without an explanation then it's shit code and I doubt you'd be able to write a comment that explains it that is not equally as shit as the code. Commenting on how shit code works cannot be trusted because the commenter has already proved they're shit at the job by creating that shit code.
Best you can hope for is the comment contains a reason as to why the code is shit.
Found the junior dev.