Never put comments at the end of lines. It suggests that either the code is bad and isn't evident what it's doing, either that you don't know that comments should highlight why something is fine and not what is done, which would make them longer and unreadable when coupled with code. Also code lines are often long enough instead, without comments afterwards.
If it doesn't work for every case, use something that does - put comments before the statement, not on the same line. This allows more room to write proper comments
Personal choice is personal. Nobody else has to deal with it. The fact that I set my tabs to 20 for example will be unknown to anybody so it's no concern.
So why is it funny? Because the comments are NOT a personal choice, since a lot of people will be editing the same code and will be scratching their head because it will say
something += 1 # Increment something
and the reader will be "well thank you captain obvious!", and curse at the screen because he has no clue WHY that thing was necessary there.
First - the example was not about the content of the comment. It was about the format, as in, spaces vs. tabs. By criticizing the example based on your opinions about the content of the comment, you have failed (deliberately or not) to address the issue for which it was presented.
But to humor you and to address your proposition that comments are only legitimate if they are lengthy, here is a revised example:
def some_function(self, request):
self.requests += 1 # see bug report 1,840
if self.response is not None:
self.responses += 1 # see bug report 1,919
Are you really going to take the position that those comments are necessarily superfluous because they are brief?
I'm going to predict your next argument: "They should be on their own line, because otherwise they might not be aligned due to variable-width tabs" - which would, of course, be solipsistic: your personal preferences for tabs does not conflict with any legitimate examples, and this example is de facto illegitimate because it conflicts with your personal preference.
Finally, since PEP8 has been cited as an authority, I'll just point out this portion about proper comments:
First - the example was not about the content of the comment. It was about the format, as in, spaces vs. tabs
Which in normal code isn't an issue. Usually you need to align words, numbers, whatever, that are all in the same nesting level. Doing it across levels doesn't make sense. And if this was an example of why it would make sense, it's a bad one.
self.requests += 1 # see bug report 1,840
Again a bad example. There's git blame for this. This type of comment is bad because it still doesn't explain why, but rather leads you to some weird place. Bug report 1840 where? Flyspray? Jira? We switched 3 years ago and lost the Flyspray DB, now what?
Are you really going to take the position that those comments are necessarily superfluous because they are brief?
Kinda yeah... Most of the times comments that are necessary are much longer. And in the rare cases where they're short, there are zero benefits to writing them at the end of the line (inconsistent with the rest of the comments), and definitely zero benefit in having them aligned.
I'm going to predict your next argument
Well you kinda failed.
Finally, since PEP8 has been cited as an authority
Argument from authority. Just because an authority says so, doesn't mean that it is better. An authority dictated that this is the standard. One has the choice of following or not, however because of the nature of standards, that choice is practically nil. This is a prime example of how some of the worst standards get to live, just because they become standardized.
Luckily there are also other authorities, not just pep8. For makefiles you don't even have the option of space indentation, because the creators realized that it's kinda stupid and didn't even support it.
Sometimes, this is useful: x = x + 1 # Compensate for border
-2
u/[deleted] Apr 30 '20
Never put comments at the end of lines. It suggests that either the code is bad and isn't evident what it's doing, either that you don't know that comments should highlight why something is fine and not what is done, which would make them longer and unreadable when coupled with code. Also code lines are often long enough instead, without comments afterwards.
If it doesn't work for every case, use something that does - put comments before the statement, not on the same line. This allows more room to write proper comments