I recently started learning test-driven development but I always see myself changing the design of my class so that higher level class can access some more properties. (After changing design, my test cases will have to be rewritten too)
For example, I am practicing this by writing a MineSweeper program.
The MineFieldImp class has a width and a height property, but I didn’t expose this in the MineField interface I created earlier. But later on I find out I need this features, so adding these properties requires me to add public methods to this class as well as the interface it’s implementing.
I constantly see myself adding methods or fields to a class that I didn’t think of earlier.
What am I doing wrong? What should I do to improve?
Thanks,
Yang
You aren’t doing anything wrong. Class design is an constantly evolving process, its constantly changing. That’s the beauty of TDD and unit testing. As your design changes you have your unit tests to assure you that your re-factoring hasn’t broken anything.
I’d highly suggest reading Fowler’s Refactoring book, there’s lots of great information in there, including many useful refactoring patterns and guidance on how to refactor. It will definately help you be more productive and help you produce better object oriented designs.