Unit Testing is for the Weak

I know it’s bad for me and the project, but every time I start programming something new, I jump into it without creating any unit tests. Even when I know that the code I’m updating could really benefit from being tested and in the long run, it would save me a lot of time and brainpower, I still start plowing through with my bulldozer of coding prowess.

It’s always the same order of events. I spend a half day hammering away at code before I start realizing the anti-pattern. I keep building, hitting F5 in my browser, do a congratulatory fist-pump after my awesome new code works, then lower my fist as I realize I may have broken something else. I figure, oh, it’s just this once, that I can adjust some query string variables to test for the other few variations, then go onto the next minor detail. The whole scene is repeated, each new thing being repeatedly tested again and again by changing the url until I realize that dammit, I knew I should be unit testing this crap.

It happened again today. I was extending some custom paging code so we could inject some ads into a list of products. The original custom paging code wasn’t wrapped in tests, so I figured, why waste my time with unit tests when I have this handy F5 button? It took all day before my dammit moment, at which point I shelved everything and wrote a few basic tests around the existing functionality as a starting point.

And of course, it worked like a charm; way easier than I assumed up front. I can stop monkeying with the url for every freaking variation of page sizes and indexes, and instead focus on one individual problem at a time. It boils down to that single unit of focus I’m capable of. With these unit tests, I don’t have to keep thinking about all the previous variable possibilities because it’s already written down. If I break it, I’ll know immediately. I don’t have to be surprised by the twelfth F5 when I realize that at some point, I screwed something up and that fixing it is going to screw up some other miniscule detail.

I don’t know why, after all these years, I can’t realize this simple fact up front and act on it. I guess I’m just weak or cocky or just plain dumb; too reliant on my cowboy coding skills and thinking that of course, I can handle every little project that comes my way without wasting my time writing unit tests. But every time, I realize that the time spent hitting F5 for every previous url variation is reduced dramatically once I actually write a few unit tests and no longer have to bother testing the same thing by hand for every change I make. If I’m really trying to save time, I’ll start with the unit tests up front.

comments powered by Disqus