3 ms·
> But ideally the comments should be executable, as unit tests For the kinds of comments that answer the "why", if we could do that, we wouldn't need the actua
by TeMPOraL 4y ago
> But ideally the comments should be executable, as unit tests
For the kinds of comments that answer the "why", if we could do that, we wouldn't need the actual code in the first place.
> making you read them if and only if you break them.
A good "why" comment is supposed to inform you beforehand, so you can make changes effectively and without introducing extra bugs in the process. Unit tests are more of a safety net.
- dhosek 4y agoI’m imagining the poster is thinking about things like rust doctests where code in the comments will be executed as tests when you run cargo test on the project. It’s a nice way of being able to ensure that (at least part of) the documentation will correspond to the behavior of the code.
- danuker 4y agoI am thinking of any fast unit test system. Going in seconds from red to green is a thrill.
- TeMPOraL 4y agoUnit tests suck at being documentation, and are not a good substitute for the "why" comment. They can catch some mistakes you make with the code under test, but they can't tell you why it is the way it is. At best, they can help you guess, at the cost of having to study the test code itself (which is usually much bigger than the code it tests, and often more complicated). But the thing is, the knowledge of "why" is most valuable to have before you start making changes and break some tests.
- dhosek 4y agoThis is true, test coverage, especially in code that has to interact with other systems in particular ways will often have ten lines of setup that only matters in the test for every line of actual verification.