Lost in Code.

On discovering the value of testing.

You don’t have to wander very far into the Ruby/Rails community before you stumble upon post after post on testing — how you’re not a real programmer if you don’t test your code, we’ve recently discovered the joys of TDD, we don’t know how we functioned without it, it saved our careers, our app’s got 3:1 test coverage, BDD is the new pink, we use RSpec and Cucumber, blah blah blah.

This past week a new version of the journaling service I’ve been co-developing got pushed which actually ended up breaking a lot of things. Fortunately we have a small userbase, so as far as I know everyone was okay with it. But the fault’s all mine here; the bugs we found stemmed from one changeset in which I converted some of the admin controllers to use resources. It turns out that there were parts of the code that were actually more complicated that I thought, and that didn’t fit the resource model as cleanly as I’d imagined. So I was shocked to find out that there was just a bunch of stuff I’d completely missed.

Now, I’ve been programming in some capacity since I was 12, so I’m quite used to surrounding myself with code. But the first time I really read anything about TDD or BDD or XP in general was a year ago, when I was also picking up Rails. So I’m no Martin Fowler or Jay Fields or David Chelimsky or Jeremy Voorhis or Luke Redpath… or whoever. I don’t have a background in this stuff.

So far, on every project I’ve worked on, I’ve been the only programmer. At home, for myself, I’ve coded alone. At work, occasionally my coworker will commit something, but I pretty much go solo. So that’s probably why I’d never heard about testing till now, and why I’ve never really done it. Oh, I’ve flirted with it, but I always end up quitting at some point, I completely stop maintaining the tests. Because when you’re the only one, you know the code, you know what everything does, why everything is there — why would you need to test? It’s a royal pain in the neck anyway, and it just takes time — time which you could spend on real development.

As I’ve just learned, though, add another developer into the mix and the ballgame changes entirely. Now there’s code that you didn’t write and you don’t understand as well as your own; on the flip side, you’ve got code that your partner can’t understand as well as his own. Now, you can do what we did and start out by saying, “Okay, you maintain what you write, and I’ll maintain what I write.” At some point, however, this falls apart and your domains start to mix together. And one change (like, say, to routes) affects a bajillion things.

Wouldn’t it be nice if, I don’t know, every time one of us committed or something, there was a way of running through the entire application to make sure that everything still works? I mean, that would be pretty rad. Too bad there isn’t any way to do that….. ;)

So, even if I would probably rather poke my eyes out than write tests, if it puts some sort of check in place to where I don’t look reckless and we don’t have to waste a week squashing runaway bugs… I’m all for it.

Now if only I could figure out which testing library to use…