You are viewing a single thread.
View all comments View context
5 points

I think comments are good as a last resort when it’s difficult to communicate the intention of the code with other means.

If I find code that’s hard to understand, I’ll first try to find better variable or function names. Often this is enough.

If it’s still too difficult to understand, I try to restructure the code to better communicate the flow of the code.

If that doesn’t help (or is too difficult), then I might add a comment explaining key information that’s difficult to understand from the code.

permalink
report
parent
reply
3 points
*

I think comments are good in a first resort, along with the other points you mentioned. To me reading a single line summary and or explainer will always be faster than reading 15 lines of code even if it’s very well made and self documenting

permalink
report
parent
reply
2 points

As mentioned in my other comment, names will rarely explain the reasons why a given solution was chosen. These reasons are important from maintenance perspective and should be recorded next to the relevant code.

permalink
report
parent
reply
2 points

I agree, and I count that as “key information that’s difficult to understand from the code”.

IMO, comments should be used to provide value to the code. If they’re used too much, then readers of the code will more likely stop reading them altogether. They already got what they need from the code itself and the comments usually don’t add much value.

If they’re sparse, then that’s a good indication they’re important and shouldn’t be missed.

permalink
report
parent
reply
1 point

I definitely agree that too many comments is often a bad sign, esp. when large part of them is obviously generated.

permalink
report
parent
reply

Programmer Humor

!programmerhumor@lemmy.ml

Create post

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

  • Posts must be relevant to programming, programmers, or computer science.
  • No NSFW content.
  • Jokes must be in good taste. No hate speech, bigotry, etc.

Community stats

  • 6.4K

    Monthly active users

  • 786

    Posts

  • 7.1K

    Comments

Community moderators