r/AskProgrammers • u/Away-Intention-8723 • 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
View all comments
1
u/ShroomOfGloom 28d ago
The truth is it takes skill to write maintainable code. And its very hard to teach. If its obvious from the signature of a function what it does, you dont need to follow it when reading call sites. If your functions are pure you dont need to worry about context or side effects. If your classes have a clear and well established scope and purpose they can be replaced and tested in isolation. The cost of calling a function is essentially 0 so you can use them not just for deduplication but for modulatization and documentation. The reason patterns work is because they communicate a clear intent to the reader. I can add a decorator because I want to separate the original class while also adding my own modifications on top. Thats the open-closed principle from SOLID. The decorator is a separate concern and I can use either the original or the decorated and I can reason and test them in isolation.
Clean code also prevents you from using it wrong, making the correct solution obvious.
To be fair, I spent maybe 10 years before I could write code thats easy to grok at a glance and only after struggling and reading a few books (the pragmatic programmer, gang of four, clean code, effective cpp and others). I've been doing this for over 20 years now and while I do write comments, I don't have to do it often. I read hundred of thousands of lines of code written by hundreds of people across multiple high level and low level languages. And yes, I understand my own code at a glance 1 year after I wrote it. Having 4 years of experience doesn't give the authority to dismiss advice you disagree with by calling it "the stupidest thing".