Friday, September 23rd, 2005

Warning: geeky programmer post to follow.

Let me give you an example of where I use unit testing in VoodooPad, and where it has saved my ass.

The heart of VoodooPad, and the part that I'm the most proud of, is a class called "VPMarkup". As you might have guessed, it's the bit that takes what you are typing in realtime and applies the hypertext links to it and does the whole wiki thing. It doesn't only do "WikiWords" however- that would be too easy. VPMarkup recognizes emails, urls, page names with spaces in it, aliases to those pages, address book entries, and it does it wether or not you've got it typed in all uppercase or lowercase.

It's a complex little beast, and I needed it to be fast.

And for the longest time I was scared as shit to touch it. I had learned that just the slightest little modification would screw up somebody's page links, because of some silly little edge case. It was starting to turn into spaghetti code and I realized that I needed to reorganize it so that it would make more sense to me and also so I could fix some long standing bugs.

This is where unit testing came in and let me write the code without fear that I was breaking everything for everyone.

So I wrote a bunch of tests against the current implementation of VPMarkup, making sure to add in edge cases that I had discovered and fixed over the years. I then added some that I knew would fail- and then began the task of refactoring VPMarkup. I worked through the various bits, got original scenarios working and then started working on the bugs that were in the original version of VPMarkup.

Occasionally as I worked on the code, tests that were passing previously would fail. I would look at my code, see what was wrong and fix it. Eventually all the tests passed, and pizza was baked to celebrate. And if I ever come across another case where linking is broken I can add a test, dig into the code to fix it, and know with certainty that I haven't borked anything that was working before.

It's such a simple thing. But the amount of confidence that it gives you to work on those complex pieces of code is priceless. If you aren't using unit testing for at least some of your code, you're really doing yourself a disservice. If anything, it'll help you sleep better at night. And it doesn't matter how good a programmer you think you are- you write bugs.

Are there places where unit testing falls down? Certainly - I have yet to come across a way to test interfaces that works for me, retro-fitting unit tests sucks (start early!). And unit testing isn't a replacement for user testing either so it's not like you can just get rid of your beta testers. But dismissing unit testing altogether is just wrong, and you owe it to yourself to check it out if you haven't already.

comments (5)   # posted 12:49 am (uct-6)

Seattle Code Camp is coming- October 22 & 23, 2005.

Jason Mauer who is one of the guys helping put this on showed up at the Xcoder meeting last night to tell us all about it. Sounds like they are really trying not to make it just MS conference. I mentioned that they didn't have the mac as a platform to check off on the signup sheet, and it looks like this morning they've already got that fixed.

Joe was making some noises about doing a presentation, and I'm already signed up to go (but not present- too many people looking in my general direction gives me the frights). The whole thing sounds fun.

comments (0)   # posted 10:48 am (uct-6)

Michael McCracken writes about Backup 3.0's flickr-style text fields. I was going to say something but he beat me to it. Which is good because I'm lazy and it might not have happened. But I did already make a movie of this new widget you can watch. (can't let it go to waste...)

Beyond that, Backup 3.0 is #%$$@!*! nice. The history stuff is exactly what I've been looking for, and the spotlight integration- awesome. I just wish I didn't have to pay 99 bucks a year to keep this thing working. But I think I will.

With this and the collab stuff for developers they are throwing in, .mac is really starting to become worth it (at least for me).

comments (0)   # posted 10:34 am (uct-6)