Break it Down
One of the problems I faced last week with TDD, was trying to do to much in one step. In other words, I’d write a test and then write a bunch of code, hoping that the test would pass. It wouldn’t.
Over the weekend, I read most of “Test-Driven Development” by Kent Beck. While the book uses Java and Python for its examples (I’ve never coded in either language), the point of reading it is to get a higher level overview of TDD. The book did a good job of outlining the thinking behind writing tests, as well as giving a lot of examples and patterns.
Getting back to my failing test — one of the things that dawned on me this weekend was that I was trying to take too many steps at a time with my code. It wasn’t that my test was necessarily bad; it was that I was trying to accomplish too much, while still getting my test to pass. Beck’s book emphasizes that it’s okay to take very small steps, especially when you’re stuck. He gives some great examples of how you can take very small steps toward getting your test to pass. With that in mind, I got back to my code on Monday and started over. This time, I took very small steps in order to ‘get to green’, and it worked!
I finally realized that, for me, TDD is more about the process than the outcome. You have to be very deliberate, and you have to consider the design of your code. I’ve been coding for a while now, and this is a new way of thinking versus the way I’ve always done things. Before, I would write code only with the outcome in mind — getting it to work. And I never wrote tests. That’s bad, and it resulted in very interdependent and brittle code. Now I’m using TDD to help me write better code. Writing code in an incremental, thoughtful way will hopefully help me avoid writing bad code, and the tests will tell me along the way if i’m being successful.
With my mentor’s help, I finished the coin changer kata, tests included. Now I’m moving on to a bigger challenge — writing an unbeatable tic tac toe game that is also covered by tests. It should be an interesting week!