Loading...

Test Driven Refactoring

I've been blogging about this a lot recently, mostly because it's now completely part of my life, but I'd like to just say Holy crap test driven development is awesome! The reason for this exclamation now is that I was trying to find out why two tests were failing (because the tests were written to adhere to how the class is supposed to work, instead of how it does work).

While debugging the class, I noticed that every function, including the constructor, was opening a handle to the sqlite database, reading in data, and then closing it. I began refactoring to make the code clear (which actually led to understand the why and where of the bugs I was trying squish. After making some necessary changes, I ran the test script, and noticed I had more failures than before, so I refactored some more, and ran the tests again. Each time, there were few issues, until eventually, I had all the tests passing (including the two that weren't passing before).

Along with that, the class methods are completing an average of more that twice as fast (because it's using the already opened handle to the sqlite database, instead of creating a closing a new one each time. The file went from 620 lines to 584, with some additional functionality, like __str__() methods and such for the class. The greatest feat, though, is that I completed this all in about half an hour.

I am absolutely sold on test driven development.

No comments have been posted yet. Be the first to leave a comment!

Leave a Comment