Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Please comment your code, when it is is necessary.

I don't need "we need to loop here from 1 to 50", I need "we have to rate-limit this function to under 60 transactions per second due to hardware requirements", etc.

If you are putting "magic numbers" anywhere, COMMENT it as to what that number is, why you chose it, etc.

I'm 30 years into this game and I still come across code that takes way too long to reason about.



100% agreed.

A trend I've noticed the past couple years is that people make the excuse of "code should be self-documenting" so they don't need to add comments. But then very helpful contextual hints that are not otherwise documented anywhere, like the one you provided, are completely non-existent and the original dev has left the team, it's been 2-3 or more years since the last time someone touched it, etc.

Code should be self-documenting, don't get me wrong. But that's more in the vein of showing (what), not telling (why/how). There still needs some be some method of telling.


Not disagreeing because I think comments can help solve confusion immediately in clever code. But when I tell people the code should be self documenting it implies there will also be:

some form of git history to go along with it

some single page architecture type of doc. No technical info, just like a README saying what this thing does and some 101, maybe a diagram.

The "doc as part of the code" for me is really just a easy way to generate a "reference" for function signatures and specific code interfacing. And if you start off that way the reference is at least always up to date, because it's caught in review (ideally).

As for updating the README that's just on the dev team to remember.

I will say my opinion changes a bit with huge polyglot mono repositories. In those cases you need great docs and organization.


> If you are putting "magic numbers" anywhere, COMMENT it as to what that number is, why you chose it, etc.

Better yet, turn magic numbers into constant variables whose name becomes the comment. Of course, comments can also provide additional context :)


Agree. I also add to this - name your constants by meaning, not value. Too many times I see

    const ONE_HOUR_IN_MS = 3600000
Instead I would like to see

    const RESEND_DELAY = 3600000


I like to combine those:

    const RESEND_DELAY_MS = ONE_HOUR_IN_MS;
Having the unit in the name saved me more than once and having non-contextual constants for sizes increases readability imo.


I do this instead:

    const RESEND_DELAY_MS = 1 * 60 * 60 * 1000;  // one hour
Essentially the same, but the lack of extra variable spares one jump. Also you can change it without introducing a new variable (no dependency).


Better yet:

    const ONE_HOUR_IN_MS = 3600000
    const RESEND_DELAY = ONE_HOUR_IN_MS


I don't get why this is better. My approach:

const RESEND_DELAY_MS = 3600000; // because TTL in Agora


I would even go for

const RESEND_DELAY_MS = 3600*1000; // because TTL in Agora

Easier to check for the right number of zeros


Yes full agree


I've used stuff like this to do:

const ONE_HOUR = 3600000;

const RESEND_DELAY_MS = 2.5 * ONE_HOUR; // because TTL in Agora

IMO it makes it easier for successor to fiddle with.


As a general guide I comment the why, now the what. The what can be generally understood by reading the code. But the why is not so easy.


Agree completely.

Also, good commenting skills take years to master.


> good commenting skills take years to master

As a newer developer, it’s nice to hear you say that. I often find that I spend more time deliberating over comment wording and variable names than I spend writing the code itself!


You are on the right path! Most new devs don't even comment at all (and plenty of senior devs). As for variables names, I wouldn't fret about that. Yes, 'foo' isn't a good var name but the exact name doesn't matter as much as one would think.


Don't do it unless you are going to treat keeping comments up to date the same way you treat keeping the code up to date.

I've lost count of the number of times I've seen comments that were just wrong -- they applied to the code as it was written years ago, not today.

Write good code, and it should be obvious how it works. Use comments sparingly and only when things are not obvious. Update the comments when you change the code, and make sure your code review process doesn't let outdated comments slip by.

If you can't commit to the above policies, then don't comment your code at all.


Code comments that are wrong can be very useful. I've found many bugs that way. If I find a comment that states an assumption that does not hold in the code, that's the first place I analyze when looking for a bug. And very often it turns out the comment was right, but the code was wrong.


>> Don't do it unless you are going to treat keeping comments up to date the same way you treat keeping the code up to date.

This should be easy enough to verify during code review, no?


Only if the comment is very local, and not e.g. two calls up the stack.


Agreed, despite the elephant in the room that when the company is in growth stage, comments rot, documentation rots, things change rather quickly and everything including comments become a liability. I'm not saying they shouldn't be written but they also need maintaining


You can just name the variable to explain what the number is, and if you need more info, there should be a "Why?" doc somewhere explaining the context generally, WITHOUT tying itself directly to the current choice (otherwise you'll have to update the "Why?" doc every time you update the number in the code, which is almost certain to go out of sync).

No need to interweave documentation and code, in most cases. Sometimes, when we fail to write good code, sure. But let's try to write good code!


>No need to interweave documentation and code, in most cases.

Locality is the reason, and it's a good reason. Of course, I don't want to see paragraphs of explanation inline, but a one-line comment giving context for some choice that might be confusing is much better and faster to work with than a separate WHY doc.


It's neither faster nor better, in practice.

It's not faster because you can just name the variable whatever it is the value represents, so by adding a second descriptor (the comment) you're introducing needless complexity.

It's not better because you really shouldn't be changing a value you don't understand the context around, which means reading much more than a one-line comment.


I think the GP is referring to the very common case where you can’t possibly say what needs to be said in a single variable name, but one or two lines of comments is sufficient.


I am saying things like this:

https://github.com/golang/go/blob/master/src/cmd/cgo/ast.go#...

belong in the source code, not a Why doc. No variable naming can give that context.


If you think variable naming is the only tool in the “self documenting code” toolbox, I recommend learning substantially more before dismissing the concept.


This would not work based on my experience

What actually would happen is the next person comes along and changes the code and doesn’t update the external doc because it’s probably buried among 100 other pages on confluence and they don’t even know it exists

If the comment is in the code they will see it and update accordingly


I think you missed what I said; there's no need to update the external doc just because you updated the value.

The external doc is an explanation of what the value does, and what happens when that value is updated, not why it's set to a specific value.


That sounds wonderful in theory but it doesn’t seem realistic in general to have this doc explaining a particular constant and why/how it was chosen that doesn’t need updating if someone comes along and chooses a new value. I appreciate that you think you’ve discovered the answer here, but experience tells me it ain’t that simple.


Care to elaborate on why your experience tells you this?

I’ve put this into practice numerous times with positive effect, so my more relevant experience tells me this is a winning strategy, compared to in line docs, which are objectively worse in nearly every way.


I think I gave one example: if the doc includes the rationale for the value, it needs to be updated.

But your more relevant experience tells you that’s wrong. I really don’t understand such aversion to a one or two line comment, but let’s just not work together in the future. ;)


I explicitly said not to write “why” docs to explain specific value choices, but rather document the context around the setting, and what happens when the value is raised or lowered, and why you might want to do that, so I’m thinking this isn’t a topic you’re grasping very well.

You’ll be fairly lonely if you reject working with everyone who’s read Clean Code, and truly alone if you won’t work with anyone who understands something better than you.


And I explicitly said sometimes you want to write about the rationale. I didn’t actually realize you were attempting to dictate that that wasn’t allowed. That seems pretty nuts.

And I think people who read Clean Code and aren’t dogmatic and condescending about trivial choices like this are pretty fine.


What you want and what is the best way to communicate are not always aligned.

And I'm not dictating anything, or being dogmatic and condescending, but I guess it's easier to think I am if you're trying to have an Internet Argument Moment.


> I'm not dictating anything, or being dogmatic

You literally dictated that explanatory code docs shouldn’t talk about rationale for constants. And your evidence for this is that apparently Clean Code says so.

> condescending

Well you can claim not to be condescending all you want but that’s how I interpreted your explanation that the only reason I disagree is that you “understand it better,” and I’d imagine that’s how most people would read it.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: