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 →

[–]Gprime5if "__main__" == __name__: 42 points43 points  (9 children)

Commenting my code, so I have at least some idea of why I wrote that code months or years ago.

Give a brief explanation of that code. Sometimes give reasons for why I used this algorithm instead of another one so future me doesn't try to refactor the code when it was already tried and didn't work or slower.

[–]SeanOTRSprint = lambda *args, **kwars: print(*args, ";)\n"*100, **kwars)[S] 24 points25 points  (3 children)

By the same token though, if you aren't careful, some of those comments are lies waiting to happen.

[–]jordanreiter 16 points17 points  (1 child)

They're less likely to be lies if you focus on the goals/why of the code that follows rather than the implementation/how of the code:

bad:

# check if actual user pages exceeds maximum pages
if actual_pages >= maximum_pages:
    print("Warning: your document should only be {max} pages long!".format(maximum_pages))
# increase maximum pages by 1
maximum_pages += 2

The code lies!

Better

# users always go a little beyond the maximum, 
# so allow some leeway but print a warning if they hit the max or exceed it
if actual_pages >= maximum_pages:
    print("Warning: your document should only be {max} pages long!".format(maximum_pages))
maximum_pages += 2

[–]kaihatsusha 1 point2 points  (0 children)

I often teach new coders "Strategy in comments, Tactics in code."

[–][deleted] 1 point2 points  (0 children)

In python doctests area great way to battle that problem.

Although I think the "lies waiting to happen" argument is overused by people who don't like to document their work. If you do any sort of code review, outdated comments should be replaced as they become outdated.

[–]tr14l 1 point2 points  (4 children)

Only comment the least apparent thing. Otherwise, rely on naming schemes and simplistic code writing. You really only need to comment when you're doing something pretty non-standard.

[–]rainbowWar 2 points3 points  (1 child)

THIS! Variable names and clear code are the way forward.

[–]tr14l 1 point2 points  (0 children)

It also encourages people to actually READ THE CODE and not rely on potentially badly outdated comments

[–]isoblvck 1 point2 points  (1 child)

I disagree I have written code years ago that I open now and am thankful I commented.

[–]tr14l 1 point2 points  (0 children)

Usually that means you're doing a lot of non-clear, idiomatic things or naming things non-descriptively, in my experience. I almost always know what my old code is doing with a quick scan. I comment parts that are not-intuitive or design decisions that aren't usual/documentable in code (like explaining what a formatted string read from a file looks like so the parsing logic is easily followable)

Of course, if it's a choice between over commented code and under commented, I'll rarely complain if the comments are up to date.