5 ms·
'Why' should be documented in your test suite. Leave a comment too, but don't think you can skip out on the former because you've done the latter.
by randomdata 4y ago
'Why' should be documented in your test suite. Leave a comment too, but don't think you can skip out on the former because you've done the latter.
- citrin_ru 4y agoTests can show what code is expected to do from the point of view of the tests author. They usually have no information why code works this way. Also I've seen more than a few times tests which ossify wrong behavior. Tests need comments too.
- randomdata 4y ago> They usually have no information why code works this way They need to if the the way it works is significant. If it is not significant then the value of a comment becomes somewhat dubious anyway as the code is expendable and doesn't really matter. It is not necessary to understand the program and if it is flawed you're going to rewrite it. Comments can add a lot of value, but there is a line where the value quickly diminishes.
- still_grokking 4y agoHow does a test inform you why a function was implemented using algorithm A and not algorithm B which would also do the same? And no matter how clear and clean you write algorithm A, the code will never answer the question why this algo and not another was used. That's the meaning of why here.
- randomdata 4y agoPerhaps a more concrete example would be useful? Nobody cares why you used a given algorithm over another in the general case. If the observation of a unit prompted a need for a specific algorithm – to reduce time complexity, for example – then you have something quite testable to document the choice, so it’s not clear what you are asserting.
- still_grokking 4y agoHow do you know when it's not "the general case"? Where is this documented? How does a unit test distinguish different algorithm (with of course same big-O characteristics, as obviously otherwise this example would not make any sense in the first place)?
- randomdata 4y agoAgain, I think we need a concrete example to really understand what you are trying to say. If two algorithms are functionally identical (same results, same space/time complexity, etc.), who cares why? What are you going to gain from knowing more about the choice? I'll give an example. Let's say you need to add 1 to each integer in an array. You could use a for loop, a map function, etc. We'll assume the compiler optimizes each choice to the exact same machine code. What would you like to have documented about the choice?
- wpietri 4y agoExactly my view, too. If a particular algorithm was chosen for, say, performance reasons, then those performance needs should be expressed in a test.
- citrin_ru 4y agoAn attempt to measure say 5% difference in a tests would likely produce a flaky test which frequent failures would waste team's time. I'm not even starting that to measure this difference in a test one would have to maintain two implementations - slow and faster one and run in a test suite both to have two times to compare. Looks like unnecessary work to me. Enough to run benchmark manually when a decision is being made.
- still_grokking 4y agoExactly. I wonder people don't recognize that upfront. I'm getting the impression, like it was stated by some people here, that this whole "no comments" / "everything should be a unit test" idea comes form inexperienced but idealistic developers mostly. People with more experience seem to know already OTOH that good comments can save your ass in the middle of the night.