Wednesday, November 30, 2011

Coin Changer

You know those little machines at Walgreens that spit out coins after you buy your gummy bears? Have you ever thought about why those machines give you three quarters and three pennies, rather than 78 pennies? I hadn't, although if I ever came across a poorly programmed coin machine that spat out 78 pennies, you can be sure that I would start thinking about it (that is, I would think about it after I finish whining about my heavy pockets).

On my first afternoon at 8th Light, I sat down with Paul and he walked me through the process of building a coin changer.

We first built the coin changer to return the correct number of pennies. Using Test Driven Development (TDD), Paul and I went through the red/green/refactor process of creating failing tests, fixing those tests, and writing more efficient code. Eventually we worked through nickels, dimes, and quarters, and by the time we had finished, you could have added 78 cent pieces to the mix and it still would have worked.

It was a great intro to Ruby and TDD, and it became my kata for the week. Here's a link to the Coin Changer Kata on GitHub. It isn't quite finished; I still need to remember how to take out all of that nasty duplication.

I will attack this problem again soon.