3 ms·
I think if you have an assert() for the invalid input, that makes sense. I don't think documentation is enough in practice. In my experience, you can write "d
by chubot 6y ago
I think if you have an assert() for the invalid input, that makes sense. I don't think documentation is enough in practice.
In my experience, you can write "don't call this with 0" in the comments, or you can write it in the documentation -- it doesn't matter. Someone will call it in an unexpected way. They will make it work for the happy path and not consider the other cases, because that takes time. (And that includes myself in many instances.)
So the only way to be sure is to blow up and make it a hard bug.
----
I'll also note that very few functions are public APIs with documentation. I'm thinking about the bash codebase, which has 140K lines of C written over 30 years. There are a lot of informal internal APIs, and few documented APIs.
They are kinda stable but also subject to change. So I think assert() is the best way of "documenting" the expected invariants in these cases, which comprise the vast majority of coding situations.