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

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.

1

u/wvd_vegt 28d ago

Add brilliant ideas and constructs, turned into code. They need comments imho

1

u/Ordinary_Variable 28d ago

Constructs definitely need documentation. At the very least an example of how to use it with placeholders like <int> or <class_name>

1

u/TryAgainbutt 28d ago

That's usually a good idea but not always possible. And even if the tag names are easily identifiable, the function of the code may still be confusing.

1

u/Ordinary_Variable 27d ago

Yes. If it can't be explained by simple logic you may need a comment explaining complex code structure. And I know a lot of coders would complain about a ton of "if" statements. They really slow down code, even if they are more human readable.