3 ms·
When I'm not writing a library (which will have a well-defined API), for perhaps the first third of a project, my "refactoring steps are too big" - except they
by barrkel 15y ago
When I'm not writing a library (which will have a well-defined API), for perhaps the first third of a project, my "refactoring steps are too big" - except they don't "turn out to be an adventure [I] regret every moment of getting into". I'm usually writing or rewriting something that I haven't written before, so the approach may change quite drastically as I find out the previous approach was wrong. Hence the rewrite, and hence why it doesn't turn out to be a misadventure.
But if I'm closing in on the home straight, I definitely want lots of unit tests so that I can fix bugs and make minor course corrections without breaking the rest of the functionality.
Writing tests first, in advance of any functionality, has not been profitable for the kinds of programs I write. Unless I already know how the whole thing will be composed at an architectural level, I don't know what to test, because I don't have any unit boundaries in mind.
The old Ron Jeffries TDD Sudoku attempt contrasted with Peter Norvig's approach, only on a larger scale problem, comes to mind.
For example, one project I have in mind was adjusting the Delphi compiler to support anonymous methods, including supporting variable capture. At a facile level, the unit boundary is simple: an expression tree before, an expression tree after. But that's usually not a good way to test a compiler, because the creating the trees correctly is a challenge in itself, and one that is already solved by the parser; and similarly, figuring out if the end result represents the semantics correctly is more easily done by actually executing it. So you tend to test in a black-box fashion, but with a unit-testing idea in mind, where the syntax is just a way of describing the parameters to a function. So you write some minimal anonymous method syntax, with expected output, and of course the compiler barfs. It's going to be some time, quite a bit of code, before that minimal test is going to pass, violating the core tenets of TDD (the very simplest thing, skipping over the syntax, avoiding a syntax error and passing the test, would be asinine). And the first approach chosen probably isn't right, for one reason or another that only becomes clear after a few days or a week. What might work well on paper or in a Lisp prototype falls over when faced with the limitation of how the existing codebase represents things. So you throw it out and start again. Testing isn't buying you much here.
Ironing the bugs out, making sure all the corner cases are covered: tests galore! No problem there. But the early stages, that's a lot more problematic.