1. incomplete
2. wrong
3. missing
My goal is to write code that is so clear it doesn't need documentation.
1. incomplete
2. wrong
3. missing
My goal is to write code that is so clear it doesn't need documentation.
I don't want to read the code of your function to figure out what its doing. I want to read the function signature, and if that's not clear enough a comment explaining its purpose and parameters. Code explains how it does something, but often does not clearly explain what that something is.
For example, having the parameter to a function declared `const` means the function cannot alter it, and this is checked by the compiler. It won't be necessary to mention that in the function documentation.
P.S. This doesn't work in C and C++, despite them having a `const` type qualifier. This is because the `const` is not transitive, and can be legitimately cast away. Therefore, it's useless as a guarantee. D's `const` is transitive, and although you can cast it away in @system code, you're on your own with that.
Actually you should mention it in the documentation. There's 2 types of documentation, so it's easier to just do the one: documentation for users, documentation for developers. Unless you work absolutely alone on all your projects and you don't open source them, someone else is going to be reading your code.
>> someone else is going to be reading your code.
Someone else is going to {read,edit,write,maintain} your code. Which means __anything__ the code does should be explained. Before you were suggesting documentation is only for the user. Documentation is also important for the developer. Whoever takes over your code later or works on it with you.
i += 2; // add 2 to i
const(int)* p; // p will not change what it points to
No value is added by such comments. Hence, the more expressive a language is, the fewer comments are needed.Of course, a precondition is that the person reading the code knows the language reasonably well, and I'm not writing a language tutorial.
But you should write summarize your abstractions. So that's your classes and functions. I agree with the other commenter that what is of most concern is the method's signature. Also, a naming might be obvious, but I assure you it is not. What is obvious to some is not obvious to others. In addition to this, the code changes over time. What once might have been obvious no longer is later down the line. Expect this to happen because you want to handle failure in your "system" (in this case, the style in which you write code). The more complexity in a method, the more important it is to document.
If you need an example I think both the main article AND the comments here are quite enlightening[0]. In fact, I'd say the comments perfectly prove the author's point. The author writes very clearly how when playing the game you should operate under purely the letter of the law but top comment claims crystal clarity yet mentions "intent." A point explicitly mentioned that one should ignore by the author.
Just think of documentation as information entropy. By adding a few words you increase the information gain for someone who has never before looked at the code. I've also given you reasons as to why you are likely to also benefit, so if you need a purely selfish reason to document, this too exists. But we must differentiate long term utility vs short term to see the reasoning.
I hear this so often but I think it is an excuse. Those 3 points are also true about your code.
> There are 2 hard problems in computer science: cache invalidation, naming things, and off-by-1 errors
The difference is there are things the compiler can verify to be correct.
The compiler does not tell you that code is correct. It has no such capability and even LLMs are far away from doing that. The compiler can only tell you that the code is compilable. That means the syntax is correct, but it does not mean the logic is. The logic is much more abstract in terms of correctness.
There are a number of constructs in C that require documentation because they are not expressible in code. More expressivity in the language reduces the documentation required. An ownership/borrowing capability means one doesn't have to document who the "owner" is. And so on.
A compiler only tells you about semantic errors. It has no understanding though. This is true for any language. We can say/write things that are linguistically correct but have no actual meaning and tells us nothing about the "correctness" or efficiency of our usage.
Comments are for the logic, not grammar. The logic is abstract.