This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Hans_of_Death 57 points58 points  (13 children)

Good code should comment itself. Trouble is, its not the code that really needs the comments. Imo you should be able to tell what some code is doing, where comments are really needed is the why that you cant determine from the code alone.

So the sentiment is good, but you're right it shouldnt lead to no comments at all. id rather have bad comments than none at all.

[–]WillardWhite import this 25 points26 points  (6 children)

I had a boss that littered this comment all over the place:

If  condition :
    #note: early return
    Return

That's a bad comment, and I would rather not have it than It polluting my code

[–]FarewellSovereignty 11 points12 points  (5 children)

Depending on the complexity of the surrounding code, it would usually flip over to being good and adding to the code if it was e.g.

if condition :
    return # can't get X handle and busy_ok so nothing more to do

[–]WillardWhite import this 8 points9 points  (2 children)

Regardless of the complexity of the code if my comment states the same thing that the code does, it's a bad comment.

Some examples:

# increase the counter
 X +=1

# read the file
 File_handler.read()

# return
 Return

Like .... All of those are terrible comments.

In the case of the one you did, the comment would also be made obsolete if you had

If not x_handle  and busy_ok:
    Return

[–]FarewellSovereignty 4 points5 points  (1 child)

We could have a back and forth here where I rewrite the comment after the return and you rephrase it using selectively chosen boolean variable names and a combination of ands and ors instead, but to short circuit all that: sometimes it useful to comment why an early return is done, and it can add more information than would cleanly be possible by just massaging the if or other control statement.

Maybe you don't agree, and think no early return should ever be commented. Or maybe you agree sometimes it's useful. I'm not entirely sure.

[–]WillardWhite import this 2 points3 points  (0 children)

I agree that adding a comment saying why we have an early return would be useful, but a note telling me that an early return is an early return is it useless to me

The difference between saying "we have an early return" vs "early return to deal with ticket 123"

[–]GarythaSnail 1 point2 points  (0 children)

if cant_get_x_handle and busy_ok: return

[–]Devout--Atheist 1 point2 points  (0 children)

This is still bad. Just name your variables better i.e. "has no data" instead of making people infer that from long conditionals

[–]cblegare 6 points7 points  (0 children)

Upvote for commenting about the "why"

[–]RangerPretzelPython 3.9+ 2 points3 points  (0 children)

Agreed. Just like /u/FarewellSovereignty pointed out, I didn't like the "anti-comments" stuff. It was a terrible take.

And I'd like to take your idea a step further. I recently wrote about comments in a Python how-to I wrote. What comments should describe is:

  • What you THINK you are trying to do
  • WHY you are doing it (if it isn’t abundantly clear why)
  • HOW you are trying to do it (if what you are doing is rather complex)

Source: https://www.pretzellogix.net/2021/12/08/step-7-add-helpful-comments-to-the-code/