Sunday, February 26, 2012

Programmer's Pursuit: Test-Driven Development

There has been little tangible progress on the project over this past weekend, and a lot of that is due to my experiment with test-driven development.  This lack of progress is partly due to trying to do something in a way that I have never tried before.  Perhaps more than that though, I had to code while essentially blind: without an implemented Room class, any tests on the Room class will necessarily fail.  Admittedly, I have my notes on how I intend to design the basic classes, but that is not the same as having methods already in place for testing.

That complaining aside, test-driven development has provided at least a couple of significant benefits that outweigh the negatives.  While developing the tests for the Room class, I had little idea of what return types I wanted to use for many methods.  Some methods are easy of course: getName returns a String containing the name of the room, addItem returns true if successful and false otherwise, and so forth.  However, other methods posed some problems.  Trying to look at all of the items in a room provides an example.  Originally, I had intended for this method to return a String containing the names of all the items in a hierarchical order of sorts.  However, after working on the tests for that section without the finality of knowing what the method had to return, I realized that this is a terribly inefficient way of solving the problem.  Using just the String would reduce flexibility, since the program would be limited to the format that the method used.  If a List is used instead though, then the calling method can handle the output however it wishes rather than having to get the String results. I do not think I would have noticed this without test-drive development; rather, I would have written tests to ensure that the existing code worked as intended. 

The Item and Person classes are fully implemented and tested; however, what I have learned from writing the tests for the Room class will also apply to these other classes as well, so I will probably have to go back to work on them again.  The project site for Programmer's Pursuit is still empty.  Still, with this experiment with test-drive development working so well, I hope that the slow progress on coding will lead to much faster work later on.

No comments:

Post a Comment