I got access to an early draft of this discussion, so I've been chewing on it for a while.I just published: 'Writing better test assertions' https://t.co/cgRceGwnaE #tdd #js #cleanCode #testing— Thomas Mayrhofer (@webPapaya) March 11, 2019
The basic outline is this: given a list of employees, produce a report of which employees are eligible to work on Sunday.
What makes this example interesting to me is that the requirements of the report evolve. The sample employee roster is the same, the name of the function under test is the same, the shape of the report is the same, but the actual values change over time.
This is very different from exercises like the bowling game, or roman numerals, or Fibonacci, where once a given input is producing the correct result, that behavior gets locked in until the end of the exercise where we live happily ever after.
If we look at Mayrhofer's demonstration, and use a little bit of imagination, the implementation goes through roughly this lifecycle
- It begins as an identity function
- The identity function is refactored into a list filter using a predicate that accepts all entries
- The predicate is replaced
- The result is refactored to include a sort using the stable comparator
- The comparator is replaced
- The result is refactored to include a map using an identity function
- The map function is replaced
- The comparator is reversed
It also doesn't feel like investing more up front on the tests helps matters very much. One can imagine, for instance, breaking up the unsatisfactory checks that are coupled directly to the report with collections of coarse grained constraints that can be evaluated independently. But unless that language flows out of you naturally, that's extra work to amortize once again.
It's not clear that TDD is leading to a testing strategy that has costs of change commiserate with the value of the change, nor is it clear to me that the testing strategy is significantly reducing the risk. Maybe we're supposed to make it up on volume? Lots of reports, each cheap because they share the same core orchestration logic?
This starts to feel like one of the cases where Coplien has the right idea: that there are more effective ways to address the risk than TDD -- for example, introducing and reviewing assertions?
No comments:
Post a Comment