I've never written anything before that was meant to be large. Recently, though, I've started a hobby Perl project that is fairly large in scope, so I've been thinking about design.
My usual design process goes like this:
- Fix a small problem with a small Perl script.
- Somebody wants more functionality, kludge it in.
- Is the code utterly terrible now? Maybe refactor something.
- Goto 2.
Code gets really terrible, really quick this way. I know you all probably know that, but it has surprised me very quickly how crappy things can get if you aren't planning ahead. The only reason it stays sane at all is the set of automated testing tools on CPAN, which have saved me probably thousands of hours of bug hunting. (The "looking for what my last change broke" kind of hunting).
Back to now: I was trying to spec out what I wanted to do, and I had a thought: What if I wrote the tests first? Possible pros:
- Get thinking about potential problems/sticky spots/corner cases earlier.
- Write code that uses the module before you write the module. I often write a module only to find out that it is awkward to actually use later.
- Be thinking about the big picture, and how different components will work together in usage.
- xdg mentions: If you've made a test bug where a test trivially succeeds without checking anything, you'll find it this way instantly.
Does anybody else start with the tests and go from there? Heck, for all I know, this is what everybody does already, and I'm just behind the times. If people do go the "tests first" route, are there any pitfalls? I can't think of any cons to going this way, and that sort of worries me.