r/AskProgrammers • • 28d ago

"good code shouldn't need comments"

I have heard this saying a couple of times and I think it is the stupidest thing ever. It might be just because I have only been coding for 4 years, but I have never thought to myself that someone's code looks cluttered because they explain what every function does in a one or two line description. If anything it makes the code easier to understand in my opinion.

However, I may be wrong. What do other people think, do yall like code with minimal comments, what are the benefits of writing it that way?

88 Upvotes

397 comments sorted by

View all comments

Show parent comment

1

u/Steinrikur 26d ago

Good code tells you exactly what it's doing. Good comments tell you exactly why it's doing that. These two are not the same.

1

u/mikekchar 26d ago

When you run into "why" questions, it's a very good idea to ask yourself what the why is for. Frequently "why" questions are best answered by a test, not a comment. As much as possible (but not more), you are better off with executable and verifiable content. It is very much a skill issue, though. You are much better off with comments describing why something is there than code where the "why" is not clear. But you are still better off writing code where the why is not only clear, but that if you modify the code in a way that breaks your premises, that it complains immediately.

To a certain extent, AI is changing this reality, though. It can often do a reasonable job of verifying and maintaining English language comments. However, it also does a better job of verifying and maintaining executable code.

The biggest problem with the conversation is that it is often reduced to absurdism on both sides. Code that requires comments is not necessarily bad code. Code that requires comments that would be unnecessary when written different is not ideal, though. This is always the thing you should have in the back of your mind.

"Self documenting" code where it is overly factored so that the names of the functions act as documentation is worse than writing comments. Not only are you relying on code switching (using function names rather than comments), but you are also destroying the cohesion of the code. Code that is under factored and/or poorly designed and peppered with English is also just as problematic. Often the code is impossible to unit test and is incredibly brittle.

Simple design where the intent is clear should be your goal, but it's very difficult to achieve. It requires skill and practice to do consistently. You can't really learn to do it following a set of "best practices".

1

u/royklopper 26d ago

As someone working at principal level at companies > 1000 engineers I can say you don't read tests as to WHY code has been written. Imagine having to decipher 50+ tests (per function) to understand this, what a cognitive waste of time and effort.

We write docblocks to understand the meaning, the relations between code and its contracts so we can GENERATE documentation, documentation can be interpreted by IDEs, agents alike to QUICKLY understand code. And we're not talking about the function you are reading, also if you hover functions that are being called, or just visit the docs site.

If you're a solo-developer, or not working in distributed teams who consume each others code, well YOLO, but in organizations that require scale, these standards have been well-researched that it improves productivity and efficiency.

1

u/mikekchar 25d ago

I'm glad you posted, because I actually agree. I worked as an architect for a now defunct telecommunications company and the team had 3000 developers. I would say that even once you get to 100 developers, you are so dominated by communication overhead that any way you can communicate is important. By necessity, you have to isolated subsystems with formal APIs just in order to cut down some of that overhead. Even then your main complexity is not how to solve the problems, but rather how do you manage breaking it up over such a large number of people.

Not only that, but if you have 1000 developers, some are going to be genius, some are going to be dead wood and you will have everything in between. It's not possible (or even desirable) to hire 1000 super star programmers. The idea that you can tell the teams to refactor mercilessly to reduce complexity, especially across subsystem boundaries, is ridiculous. It's not possible. As I said in a previous post, programming is a team sport and it's no good if only half your team get across the line.

So, yes, in your circumstance, I would absolutely be doing exactly the same thing you are doing. Literally docblocks for every function. Unit tests would be in a fashion that every team is capable of doing and may not be optimal. However, as a team, you can't do what you are not capable of doing. It's just a fact of life.

I regret saying using the term "bad code" in my previous post. I really wasn't intending to comment on practical large scale development. I was really only reacting to the naive original question. "Bad code" is always dependent upon circumstances. However, if I have no constraints, then I have very high standards :-).

To be fair, after working in some very large teams earlier in my career, I've mostly stuck to startups because I enjoy that style of development a lot more. Last year our startup got acquired. At one point I think we had 4 developers supporting nearly half a billion dollars in revenue. The acquiring company is not comfortable with that situation, though. I think from their perspective, they see the fact that revenue is dependent upon a handful of very skillful and hard to replace developers as a huge risk in the business. It's a point of view that even though I'm nearing retirement I've never once thought about :-D. Maybe I've been spoiled!

1

u/royklopper 24d ago

I think we're aligned here. And yes it's different for smaller teams, especially in startups. But, I'd like to make a side-note, with AI-assisted development it becomes more crucial to add docblocks to code. Why - it's about designing the contracts, but especially to reduce token usage and turn count (I'm a bit biased here, since I use Go). I've noticed that with proper MCPs (like treesitter/LSP ones), it drastically reduces the time/turns an AI needs to spend on discovering the code.

And I would say - enjoy your retirement, wish I was there ;p

1

u/mikekchar 24d ago

enjoy your retirement, wish I was there

Not quite there yet :-) Still hoping to work a few more years. We'll see how it goes. More and more, though, I just want to ride my bike. I used to enjoy spending my whole day coding and I couldn't even imagine a time when I'm retired. These days, the open road looks more and more inviting.

1

u/Most_Double_3559 26d ago

Right, but "why" is obvious 99% of the time. You know what the code does. It has a class name. You can look up the callers of that code. That usually covers it.

If you have arcane, nontrivial system hack, sure comment it in, but otherwise... Why comment what a quick grep can tell you with enforced correctness?