3 ms·
Idea: every comment should have an assert attached to it. If after a code change assert becomes false, your IDE warns you and you must rewrite the attached comm
by Grue3 8y ago
Idea: every comment should have an assert attached to it. If after a code change assert becomes false, your IDE warns you and you must rewrite the attached comment.
- anoncake 8y agoYou can't express every comment as an assert statement. And when you can, then what's the point of the comment? I don't think // x should never be 0 assert x != 0 is all that different from // set x to 1 x = 1
- Grue3 8y agoThe point is not to reduce the comment to the assert, but to detect when the comment is outdated. If a comment is referring to another function in the code, then assert would check that the function exists. If you refactor the code and remove the function, you'll have to refactor this comment as well. A docstring describing arguments to a function would check that the list of arguments to a function is exactly as described in the comment. If you add a new argument, you have to edit the docstring. And so on.
- anoncake 8y agoThat actually sounds interesting, but I dont see why youd need an assert for that. If you markup the comment appropriately so the compiler or a linter sees when you mention another function and identify where you describe the arguments in a docstring, it could do these checks statically. In fact, Im pretty sure Javadoc and other tools already have that markup.