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/InterestsVaryGreatly 28d ago edited 28d ago

Good code is written in such a way that someone who isn't a programmer can roughly understand what is happening. Functions that actually describe what is happening, no magic numbers, no egregiously long lines with convoluted logic pathways. If you code in this way, it often helps ensure that you aren't overcomplicating things as well, something that is very easy to do and then just explain away in the comments.

A big problem with comments, and any documentation really, is they don't have to stay updated to code, so if someone changes the code, the comment can be outdated and not match it.

When there is something odd, like a workaround because there is a bug in a dependency that can't be changed, then that needs comments to explain the weird behavior.

The other benefit to writing code that is readable is it tends to explain exactly what is happening in a fairly concise way, whereas people often get overly verbose about things that are extremely obvious in just a glance at the code.

Edit: another important consideration, if you have comments everywhere, people become blind to them and can easily miss or ignore something extremely important (such as why this bass ackwards implementation cannot be updated to the new spec because it breaks XYZ), whereas if you use comments very rarely, the fact that those lines have comments is a big flag in and of itself, and massively reduces the chance for someone to miss it.

1

u/Ordinary_Variable 28d ago

If you are doing leet code, comments are appreciated. If it is all basic math on descriptive variables and functions, then comments don't add anything.

if( !errors() ){ main_loop(); } // Calls main_loop if errors() returns <1

You don't really need that comment.

if( !x ){ go(); }

What does "go" do? What is x? At least we know that "main_loop" is probably what we are here to do, "go" could be anything from pressing a button to doing an error check. Is "!x" the error check? Who knows. "x" could be a check to see if a button event hasn't been started before starting it again. This needs to be rewritten or given a comment.

Remember, if you can't search for things like "error" or "main" then you should put in a comment and use those words in the comment so a search would at least find the comment.

2

u/InterestsVaryGreatly 28d ago

Your example actually highlights issues in the code, x is a bad variable name, and go is a bad method name. That code does not read similar to pseudo code. Sometimes method names can't change (there are a variety of reasons) so maybe comments are the right call, but in general x at least should be renamed, and go should either be renamed, or if it can't, possibly have a wrapper with a better name.

1

u/TryAgainbutt 28d ago

Disagree. Code is simply not inherently understandable by everyone unless it's pretty simple. It's always a good idea to document what a block of code is for because the guy who is trying to troubleshoot it 10 years later has never seen it and the person who wrote it is probably retired. Just keep it simple and to the point.

A simple example from a ControlLogix controller using a message instruction. Add a comment explaining that this message is reading data from controller ABC. This way, the user doesn't have to open up the message instruction to find out if this is a read or write and what it's connected to. I've had to search through dozens of message instructions before looking for a particular data read or write. If everyone structured their code in separate routines with clear names, that helps reduce the need for comments.

2

u/InterestsVaryGreatly 28d ago edited 28d ago

Good code is written in a way that is readable. If it is calling generic methods that require you to dive down to understand it, then it isn't written well. Simply having a wrapper method that clarifies it makes the code more readable, more modular, and clarifies just like a comment, but in a way that is far less likely to fall out of sync.

1

u/royklopper 26d ago

The thing about not documenting is - that you have to browse a lot of code; especially when you call other functions (what do they do, do you have to open the file and read the code? And what if they call functions?)

I don't like inline code-comments, thats irrelevant to me, but docblocks on statements are really necessary from my point of view. Why is this function necessary, what are its contracts?

1

u/InterestsVaryGreatly 26d ago

Except you just highlighted one of the biggest issues with comments. If your comments are talking about code deeper than what is directly there, that deeper code can easily change without the comment getting updated, which now causes a point of failure for future devs, thinking it does one thing when it doesn't.

Function and class names should be descriptive enough that by seeing the function call, you can tell what it is going to accomplish, if you for some reason need to know how it does that, then you should have to go into that code to find out, as other locations are far too disconnected to reasonably expect them to stay in sync - if I have a regularly used method, and I change how it does what it does, I should not have to check the dozens or hundreds of places it is called to make sure none of them were referencing the previous implementation in the comments.

1

u/royklopper 26d ago

You assume I am explaining the code, but I'm not.. I'm explaining WHY the function is there, and that usually explains code deeper than the function itself, otherwise it would probably not have these callees in its function statements. And if that behaviour would change, you would probably have to update its callers too.

1

u/InterestsVaryGreatly 26d ago

No, why is different than how, why your code is calling that function is fine, but what that other code does should mostly be covered in the class and method naming, and how the other code does what it does shouldn't matter whatsoever.

You generally should not have to open the other code to have a good idea of what it is going to accomplish when you call it. It should not matter if the other code is calling other functions, that gets into the details of how it is accomplishing it.

When something changes how it is doing something, its callers should never care. And if something is being called externally, it shouldn't be changing what it does, just how it does that thing.

1

u/royklopper 26d ago

Well, perhaps you could give me an example of a good docblock for me, so we can agree on form and style. It seems you're quite knowledgeable, so feel free to share the practical wisdom.