4 ms·
The problem with creating unit tests before the code is that it simply doesn't work for exploratory programming. I often don't know exactly what I intend to do
by nradov 15y ago
The problem with creating unit tests before the code is that it simply doesn't work for exploratory programming. I often don't know exactly what I intend to do or what the tools are capable of until I'm fairly far into coding the features. Writing unit tests at that early stage would be a complete waste of time.
If you know ahead of time exactly what the inputs and outputs should be then sure, writing unit tests first is a good practice.
- ams6110 15y agoI don't understand what you are saying. How can you start coding if you don't even know what you intend to do or what your expected inputs and outputs are? What are you exploring, and how do you know when you've found what you're looking for?
- thorstadt 15y agoAs an example of exploratory development that's very difficult to do test-first, I've been doing a lot of image processing work lately with the objective of improving the legibility of photographs containing text. Trying to find a way to automatically test the output before putting together a solution is a unique problem unto itself, since any given set of input parameters could potentially "solve" the problem for some images but not others.
- nradov 15y agoSometimes you have to explore the problem domain and available tools for a while before you figure out where you want to go and how to get there. It's sort of like an iterative prototyping methodology. http://paulgraham.com/arc0.html http://paulgraham.com/arc0.html I don't use Arc (or any kind of Lisp) but I understand what Paul Graham was trying to achieve.
- colomon 15y agoAnother simple example from my work of the moment: my goal is to import points from a file (that I'm assured does have points) using a 3rd party file import library. I don't know in advance how the 3rd party library imports points, so the best test I could possibly write ahead of time is a fairly high-level integration test, "Did we manage to import a point?" All my actual programming work here consists of adding printfs at strategic points to tease out exactly where I should be looking for the point data. Unit tests are completely useless for this process. Come to think of it, even once I've got it figured out, it's very hard to see how to usefully use unit tests here...
- ollysb 15y agoKent Beck talks about this in episode 1 of http://pragprog.com/screencasts/v-kbtdd/test-driven-development http://pragprog.com/screencasts/v-kbtdd/test-driven-developm... The example he works with is writing a driver for Tokyo Tyrant in java. He begins by writing a high level test of what he eventually wants to be able to do. Instead of implementing it though he comments it out and then starts to play around with potential implementation code inside the body of the test. Once he's finished playing he then gradually refactors code from the test body into the implementation until he's got everything he needs and uncomments the test. It may well be the case that by the time you've finished exploring that you've realised the test you've written isn't very relevant. This is fine, the purpose of the writing the initial test is to guide your exploration in a productive direction. Once you have some implementation code you can always go back and reframe the test according to your new understanding.