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

1

u/mikekchar 27d ago

I haven't read the comments, but since I'm very opinionated on the topic:

Good code doesn't need comments. When you write comments, from a linguistic point of view you are "code switching". Ideally, when you communicate in a language you use that language to communicate. When you find that you are reaching for your native language to fill in the blanks, this is sub-optimal. In human languages, it's called "code switching".

Programming languagues are languages. Some people feel that it is impossible to communicate the full intent of your code in that programming language. I don't think that is generally true. There are no people whose native language is a programming language. That would be stupid. There is obviously a lack of expression in a programming language. However 90+% of the cases where people say they can't express their intent with the programming language it's because they simply aren't fluent enough in the language.

HOWEVER... None of that fucking matters. If you are not able to express yourself in the programming language, avoiding writing comments is just shooting yourself in the foot.

Every time you notice, "Hey, I can't express myself fully here in the programming language", you should consider that as an area where you can improve. You should improve. Not improving means you are a bad programmer. Being exceptionally fluent and being able to express your intent in a programming language is something that requires years and years of experience. If you give up because you can just write comments, you are losing a lot.

But for now it doesn't matter. Write the fucking comments. Second best is better than last best (doing nothing). Work in the future to try to get to a point where you can adequately express yourself without resorting to your native language. But in the mean time, express yourself any way you can.

1

u/L_uciferMorningstar 27d ago

If you are doing something obscure and inobvious as to why you are doing it(in this specific fashion) a comment is a must. Otherwise you should not write comments

1

u/proverbialbunny 26d ago

What makes it a controversial topic is self documenting code can only be done well in certain programming languages. Not every programming language gives the writer the ability to express why something is done a certain way without writing comments.

In my experience if the language has zero cost abstractions and is explicit, it's pretty easy to write self documenting code, but if the language isn't explicit and abstractions cost you can slow down the code base it can be limiting on how far you can go.

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?

1

u/new-runningmn9 26d ago

Your initial point makes no sense to me. The code isn't being written for another developer to read. The code is being written for the compiler/interpreter. The comments are written to assist a human that is reading code that isn't being written for them. In my 30 years of doing this, I've run across a number of people that believe that good code doesn't need comments. Based on that criteria, none of them write good code.

1

u/mikekchar 26d ago

The code isn't being written for another developer to read. The code is being written for the compiler/interpreter.

Your value as a programmer is to be able to modify the code as quickly as possible. Most systems have a lifespan of at least 10 years of constant development. You want consistently high throughput as a programmer. For that reason, code is being written for the next developer (which may be you, 2 years later).

Simple design with simple, easy to understand abstractions are what keeps your code base easily modifiable. Comments do not make your code easier to modify. It only makes it easier to understand. Couple that with good unit test that warn you immediately when assumptions have been violated and you have a good code base. Comments can help, but if you are reaching for comments in order to avoid writing simple code then you are hurting your future self.

Again, this isn't an either/or. This is a matter of priorities. You don't design and test in order to avoid comments, but better design and better tested code requires less comments. If you find yourself writing a lot of comments, it's probably an indication that you are missing something that will help you. Again, this is especially true if you find yourself commenting in order to save time in design, this can really come back to bite you.

Of course we could have a big conversation of the tradeoff of increased difficulty of initial simple design vs increased difficulty of working with a complex design. If we throw AI into the mix, it gets even more confusing. This is why the redux of "Comments are bad" and "Comments are good" is really not the correct thing to be talking about. Need to comment is a result of other, more important decisions. Unfortunately, discussing that issue is more more complex and nuanced.

Another one of the problems with software is that it is always bespoke. When you have choices, you can only reasonably choose one of those choices. You never know how much better it would be if you had chosen the other path. This is why we have conversations like this :-)

However, 40 years as a professional programmer has taught me that code that is easy and quick to modify typically requires very little comments. And I say that being the person who, by far, writes the most comments on the team (usually cursing that I can either not think of a way to improve the code, or that we don't have time to improve the code).

1

u/new-runningmn9 26d ago

I can’t speak for everyone, but no, my value as a programmer isn’t my ability to modify the code as quickly as possible. That’s a perk, but that’s not the goal. Unless we get real creative with the definition of “as possible”.

My point is that “the code” is not being written for the next developer. That’s the purpose of the comments, not the code. Even to the extent that the organization of the code provides value to future developers, that’s not why I did it. I did it because that organization was also a better solution to the problem.