You are viewing a single thread.
View all comments
73 points

Comments should explain “why”, the code already explains “what”.

permalink
report
reply
63 points
*

The allowable exception is when the what is a what the fuck, as in you had to use a hack so horrible that it requires an apology comment

permalink
report
parent
reply
15 points

Absolutely, although I see that as part of why

Why is there a horrible hack here? Because stupid reason…

permalink
report
parent
reply
5 points

Or if the what is so cryptic and esoteric that it would require the reader a couple hours of research to understand it.

Also, I find it useful to summarise the what before code blocks if that can’t be summarised in a function name

permalink
report
parent
reply
4 points

Describing the what also helps when you dabble in a new technology or little-used technology. It helps to explain to yourself what you’re doing and it helps in onboarding. “Hey, newbie, there’s a function in XYZ module that’s extensively documented. Look there for guidance.”

permalink
report
parent
reply
28 points

Inline comments yes.

Function/Class/Module doc comments should absolutely explain “what”.

permalink
report
parent
reply
8 points

You are absolutely right. It was inline comments I had in mind.

permalink
report
parent
reply
6 points

I don’t code, at best I script. I’m a sysadmin, not a dev, so I play around in PowerShell mostly.

I just started to naturally do all of this. Not because I was taught to, but because I’ve written too many scripts that I later looked at, and thought, WTF is going on here… Who tf wrote this? (Of course it was me)…

So instead of confusing my future self, I started putting in comments. One at the beginning to describe what the file name can’t, and inline comments to step me through what’s happening, and more importantly why I did what I did.

The sheer number of comments can sometimes double the number of lines in my script, but later when I’m staring into the abyss of what I wrote, I appreciate me.

permalink
report
parent
reply
1 point

I agree.

I usually think of that as documentation, not comments.

But even so, the code should say what it does, with a good name. The documentation adds details.

permalink
report
parent
reply
5 points

Unless you’re working with people who are too smart, then sometimes the code only explains the how. Why did the log processor have thousands of lines about Hilbert Curves? I never could figure it out even after talking with the person that wrote it.

permalink
report
parent
reply
3 points

“Smart”

permalink
report
parent
reply
1 point
*

If you know how the code does something, you also know what it does.

permalink
report
parent
reply
1 point
Deleted by creator
permalink
report
parent
reply

Programmer Humor

!programmer_humor@programming.dev

Create post

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

  • Keep content in english
  • No advertisements
  • Posts must be related to programming or programmer topics

Community stats

  • 2.5K

    Monthly active users

  • 866

    Posts

  • 14K

    Comments