DSimon comments on Coding Rationally - Test Driven Development - Less Wrong
You are viewing a comment permalink. View the original post to see all comments and the full post content.
You are viewing a comment permalink. View the original post to see all comments and the full post content.
Comments (82)
Sure. Suppose you've just coded up a method. Your favored hypothesis is that your code is correct. Thus, you will find it harder to think of inputs likely to disconfirm this hypothesis than to think of inputs likely to confirm it.
Wason's 2-4-6 test provides a good illustration of this. You can almost directly map the hypothesis most people come up with to a method that returns "true" if the numbers provided conform to a certain rule. A unit test is a sample input that should cause the method to return false, which you could then check against the actual output of the Wason "oracle" (the actual criterion for being an acceptable triple).
Most people think more readily of confirmatory tests, that is, input data which conforms to their favored hypothesis. This will apply if you have already written the method.
This was noticed in the late 70's by testing authority Glenford Myers who derived from it the (misleading and harmful) edict "developers should never test their own code".
However, if you have to write the test first, you will avoid confirmation bias. You are less likely to harbor preconceptions as to the rule to be implemented, in fact you are actively thinking about test inputs that will invalidate the implementation, whatever that happens to be.
Does that help?
Okay, I think I see where you're going, but let me double-check:
You're saying that false positive tests are weeded out in TDD because the implementation isn't allowed to have any code to raise errors or return negative states until there's first a test checking for those errors/states.
So, if an everythingWorkingOkay() function always returns true, it wouldn't pass the test that breaks things and then makes sure it return false. We know that test exists because ideally, for TDD, that test must be written before any code can be added to the function that is intended to return false at all.
Whereas, if the programmer writes the code first and the test second, they might well forget to test for negative output, since that possibility won't come to mind as readily.
See this other reply for more detail.