|
|
|
|
|
by sureglymop
716 days ago
|
|
What I never understood about TDD is: why would one have to write the test first? Now, I get that maybe the reason is human psyche and that one may not have the same discipline to write the test after the code. But, stripping that away, more logically asking, why does the test need to exist first? I get that if you have a test, you have an exact "plan" of what your code needs to do/return and you then work on it until the test passes.. But isn't that the case already in any strongly typed language? You constrain the set of values your function can return to only the set of all valid values by defining a type (which is that set). (Okay I may have answered my own question, as we do define the expected type first). I think that at the end of the day it comes down to finding a balance between complete formal validation of correctness and productivity/usability i.e. actually getting something done. TDD strikes me as a practice that slows you down a fair amount yet still doesn't offer anything close to complete formal validation, so it doesn't seem that attractive to me. But I still also wonder what alternatives are out there. |
|
When you write the test first, you tend to think through the harder parts of “how do I make this code testable” which otherwise you might shrug your shoulders about and skip — since certain test scenarios may then require too big a refactor which is not worth it.
Just like it’s hard to bolt on security after you’ve built a product, for various system designs it’s hard to build testability if you don’t think about it up front.
The other part of this is psychological; TDD is a psychological trick for task decomposition. Certain people get paralyzed by unknown overwhelming tasks. A failing test case narrows that down to a smaller clearer solvable problem.
Also… cynically, if you write the test first, management (or your inner optimizer… you know you have one!) can’t— when they see you’ve delivered functionality— pressure/tell you to skimp on testing.
I confess I am not convinced the TDD juice is always worth the squeeze, but I have no regrets about the time I spent putting TDD in my toolbelt.