The credit card number checker kata
Some time ago, I was browsing www.rubyquiz.com, and found a very good exercise, namely “Credit Card Checker”. It’s not only nice because it is based on some ‘real-world-stuff’, but also because it has all the features a great TDD code kata needs.
Let’s see what the exercise is all about:
- Starting with the next to last digit and continuing with every other digit going back to the beginning of the card, double the digit
- Sum all doubled and untouched digits in the number
- If that total is a multiple of 10, the number is valid
Some weeks ago me and my colleague have started working on the exercise above, and found out this would serve as a great kata (for beginners or a bit more advanced TDDers). In order to make it slightly harder, we also added the constraint that the card number’s digit groups must be separated with spaces (so some string operations are involved).
Here is what we came up with, after a kata of just one round (it could be improved, but as I mentioned, we had time for one round only) -the whole of source code can be found here, you can take a look, comments are welcome-: https://github.com/tamasgyorfi/Code-kata—credit-card-checker
Having solved this, I thought it would serve as a great first kata for my new project’s teams. It is a good start for several reasons:
- it’s simple enough to be understood
- it’s hard enough for TDD newbies to take them two or more rounds (anyway, extra constraints can be introduced for those progressing too fast.)
- test cases are quite simple and straight-forward (just follow the steps above)
- one can achieve 100% code coverage
- confers the feeling of self-confidence upon successful completion
Although no one was able to finish the exercise entirely, there were some really good ideas, and we’ve got an almost-ready solution. We have had two rounds; the progress between those two rounds was a striking one. The second round’s solutions were waaaay better, every pair made significant progress.
One of the team members has mentioned after the first round, that he was under pressure because of the time limit (which was set to fifteen minutes). I told him it was important for progress measure, as we compare the first round’s solution to the second’s. I also told him, that his second solution would be far better than the first. And guess what, he proved me right. They (as we were working in pairs) managed to get way farther then they did at the first time. I hope I convinced him that time limit was necessary indeed…
In spite that we had no fully finished solution, I consider this kata really successful. The teams finally had a “do-it-yourself” session of TDD, and I think it was a good experience for all of us. Who knows, maybe we’ll look at this kata once more in some months and observe the progress made…