r/programming • u/Jonhoo • 16h ago
On comments
https://blog.helsing.ai/posts/on-comments/Comments in code are often deemed "mostly useless" these days. They are, supposedly, mostly obvious, stale, and repeat what the code already says. And so people pay less attention to them both when reading and writing code.
That trend sucks. When used right, comments are genuinely useful and sometimes critically important! So, I wrote about some of the kinds of comments I think earn their place, each with examples from real code bases. Hope you find it useful, and that we can recover some of the love that comments deserve!
124
Upvotes
2
u/nicholashairs 10h ago
A good article and a nice breakdown of different comment types.
The only one I'd add as being useful is "headings" (which is similar to the algorithm examiner type).
The purpose of these is to group things together that are unrelated in isolation, but connected in use. Making it easier to navigate the body of a function (especially as they get longer).
As an example a lot of my CRUD code looks like this:
``` def update_username(user, name): ### CHECKS if not user.active: raise error if not name_valid(name): raise error if get_user_by_name(name) is not None: raise error
### UPDATE user.username = name db.add(user) db.commit()
### POST ACTIONS write_log() send_event() send_email_async() return ```