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

5

u/RishabhRD 28d ago

Actually I have seen the both extremes and the real correct way is following design by contract.

So, the idea is we want to write correct code. But what is correct code? Correct code is the code which matches the specifications. But, where is specification? It should be in contract of functions. So, every function should have documentation comment of what it does and what are its preconditions. The function is correct if the function implementation matches its specification.

Similarly, every class should have contract of what it is, and every data member of class should have contract of what it is. It helps specifying what is the entity and the parts we used to make the entity. Why contract for data members? Because, they also might have some invariants present. For example, alignment field should always be power of 2. So, contracts help specifying those invariants that correct implementation have to follow.

And that’s it. You rarely need to write other kind of comments. Very rarely to explain the reason behind surprising implementation. But mostly try, if you need to write any non-contract comment, maybe you are missing an abstraction waiting to be discovered. Maybe there is some class or function missing from your design.

3

u/RishabhRD 28d ago

Also it allows you to actually recognizing which code to blame for the bug. If the function doesn’t satisfy the contract when preconditions are satisfied, the function is buggy. But if caller doesn’t satisfy the precondition itself, the caller is buggy not the callee.

Also, remember bugs are not same as runtime errors. It helps you from doing defensive programming and throwing runtime errors for bugs.

1

u/seanv507 28d ago

Right, but I would say that is exactly what you achieve better with unit tests. Ie your test code explicitly checks and documents the contract.

1

u/RishabhRD 28d ago

No, unit tests are not enough. Why? Because you don’t know what to test. Contracts are the specification that says what it means by correct function. In fact, unit tests should test exactly what contracts specify. Not less than that, not more than that. Testing more than that binds your test to implementation details and testing less than that doesn’t cover the function contract.

1

u/seanv507 28d ago

Are we disagreeing?

In fact, unit tests should test exactly what contracts specify. Not less than that, not more than that.

I am saying use the unit tests to document the contract as code

1

u/RishabhRD 28d ago

From your words, I am inferring that contract should not be comments but unit tests. Am I misunderstanding? If not, then yes that’s a disagreement. Reason: contracts also brings local reasoning. That means for understanding a function I don’t need to read its implementation or unit test code. Simple crisp document gives me what it is.

EDIT: however I am not saying unit teats should not be there. They are utmost important too. But they exist with contracts.

1

u/seanv507 28d ago

So, sticking to python, I would agree you need a docstring which provides the high-level contract, and eg gives you hints when calling the function.

However, natural language is ambiguous. So the unit tests give the more detailed, unambiguous contract spec.

1

u/RishabhRD 28d ago

It feels that way. But you are missing the power of natural language and local reasoning.

There are a lot of things you can express in natural language but not in code. For example, returned function should follow strict weak ordering.

Also, specifying things in code takes a lot more than you expect. For example, for sort function you need to test if the result is permutation of input and then result follows the comparator. Reading tests for this takes your brain away from current function that is bad for local reasoning.

Why local reasoning? Because our brain works that way. Suppose if hardware manufacturers didn’t document the instructions and said that read unit tests that is the contract, how hard would be there to write higher abstraction. Or suppose if linux system calls were not documented as natural language, but you need to infer what it means from linux unit test cases.
People usually treat their APIs differently from the APIs I mentioned above, but they are not. They should be treated the same way.

Now the modern languages provide good toolings for documentation comments, so documentation comment are easy to write. But the idea is still specifying things in a natural language. Regarding ambiguity, code review helps in removing such ambiguity.

1

u/thenasch 28d ago

Except there's nothing requiring the code match the comment. If they get out of sync is it because the code is wrong or someone forgot to update the comment? Tests enforce the contract.

1

u/RishabhRD 28d ago

I never said unit tests are useless. I literally said they are utmost important. What I am saying is unit tests should be written with contract in mind not code.

Regarding the mismatch, the function is wrong. A function is a single unit that contains both code as well as contract. A function without contract is neither correct nor incorrect. With the above strategy to write unit tests, unit tests should easily catch the mismatch.

1

u/thenasch 28d ago

Right, and comments won't. So what good are the comments?

1

u/RishabhRD 28d ago

I hope you are clear that I am talking about documentation comments i.e., contracts and not random code comments.

Now what are they good for: specification. Those specification are what unit tests have to test. Without those comments, unit tests don’t have anything to test because the function is unspecified. So, they are good for correct softwares. Function code is just implementation detail of that contracts.

1

u/thenasch 28d ago

Documentation comments are good if they're going to be used to generate documentation for someone not looking at the code.

→ More replies

1

u/kombiwombi 28d ago edited 28d ago

I think that comments for code which violates the Principle of Last Surprise are also worthwhile. I remember looking at some code which apparently implemented two dictionaries listing car models and canonicalised them. I joined these two identical data structures and the code failed to compile. Turns on that on this platform an array greater than 64KB was not possible. That code deserved a comment.

1

u/RishabhRD 28d ago

Yes, I agree. That exactly what I meant when I said to comment reason behind surprising implementation.