I happened to notice that an SUnit I added today was number 1,000. Perhaps I should get free groceries for that milestone, but it reminded me just how much and why we take xUnits and test-driven-development (TDD) for granted. I was a bit more aware, having spent yesterday working on a customer’s C++ code which had no xUnits or test scaffolding.
It really was a study in contrasts.
For the customer’s C++ code, I found myself fighting in the Visual Studio debugger mostly with overzealous constructors and destructors (folks, just instantiate and delete objects in constructors/destructors, don’t use them for wild stuff like reading registries, calling decryption libraries, and connecting and disconnecting from databases). But the real hassle was having to run the code-compile-link-bounce-deploy gauntlet far too many times. Often this isn’t bad (yes, incremental compile and auto build all help), but in this case, it took a lot set-up time getting data in the state it needed to be in before calling this code, and every code change required repeating that. That’s usually true even of hot swap JVMs.
Compare that to my SUnit-driven VA Smalltalk code. I wrote the test case and my first stub method before I wrote the code. I ran the test, and of course, it failed (failed big, too: red, not yellow, due to an expected message not understood exception). I re-ran the SUnit with Debug, and started filling in the code – in the debugger. I added methods and even an entire class that didn’t exist before I started debugging. I inspected objects and ivars and even changed a few to guide me through crafting and whittling at the code. I ran it again and got to yellow. One more “code it while debugging it” pass and the test went green.
Red, then yellow, then green. My favorite development process.
Why haven’t we learned?