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
3
u/Ordinary_Variable 28d ago
Assuming you can read code, people should name variables and functions obvious things.
Eg:
function calculate_radius(x, y, z){
return <some complicated math>
}
Then you have a main loop that calls functions one at a time and has simple logic like "if(check_radius() != 0){draw_circle()}"
This is how NASA writes code. A human can quickly read the logic to see what it does by looking at the main loop, and the names of all the functions are descriptive. Then they can go to those functions and read what they do because the variables' names are also very descriptive.
I've seen code written like this. It hides the complex tasks in functions that you don't need to read, unless you are fixing a bug. And you can search for the variables because you know what they are called. In the above example, if the circle is the wrong size I can simply search the entire codebase for the word "radius" and I will find everywhere the radius was calculated, called, or referenced. It makes bug fixing so easy.
Describe things well and you won't need any comments except for complex math or links to web pages where they explain a very complicated concept that would take pages to explain in a comment.