Since attending Roy Osherove’s TDD Master Class I have been doing my daily TDD Kata, I will admit I have missed some days but generally I have been keeping up with it and really seeing the benefit.
Here I talked about what a Kata is and how it compares to Kumon Maths and I also took the opportunity to practice my reviewing techniques by looking at the best and reviewing Uncle Bobs prime factors TDD Kata.
I had promised to provide a web cast of the TDD SharePoint session that I did at the Best Practices conference, but I feel now that I need to really improve my keyboard mastery before taking on that one. Instead I have taken the opportunity to record my current TDD Kata which is based on Roy Osheroves String Calculator. I have limited the steps I have done in this recording as I think 30 minutes would get a little boring.
String Calculator
- Create a simple String calculator with a method int Add(string numbers)
- The method can take 0, 1 or 2 numbers, and will return their sum (for an empty string it will return 0) for example “” or “1″ or “1,2″
- Start with the simplest test case of an empty string and move to 1 and two numbers
- Remember to solve things as simply as possible so that you force yourself to write tests you did not think about
- Remember to refactor after each passing test
- Allow the Add method to handle an unknown amount of numbers
- Allow the Add method to handle new lines between numbers (instead of commas).
- the following input is ok: “1\n2,3″ (will equal 6)
- the following input is NOT ok: “1,\n”
- Make sure you only test for correct inputs. there is no need to test for invalid inputs for these katas
- Allow the Add method to handle a different delimiter:
- to change a delimiter, the beginning of the string will contain a separate line that looks like this: “//[delimiter]\n[numbers.]” for example “//;\n1;2″ should return three where the default delimiter is ‘;’ .
- the first line is optional. all existing scenarios should still be supported
The idea behind TDD is to work through one thing at a time, so in the example we start with 0 numbers (or empty string) and progress through each requirement continuing to adopt the Red, Green, Refactor approach – hopefully this will be clear from the video.
The Kata is not about knowing how to solve this problem, it is about knowing how to do things quickly and also knowing how to approach problems using TDD.
In this example, as in my day job, I am using ReSharper to aid refactoring, NUnit 2.5 for my tests as this supports TestCase and TestDriven.Net to run the tests.
I have setup some live templates for the Test method and common Assert.AreEquals statements and have added a Keyboard shortcut to Re-Run the tests in TestDriven.Net (I use Alt + R).
Hope you enjoy the video, I have left the sound off - if you perfer some nice background music I can edit and add some
Would love to hear your thoughts on this.
TDD Kata – Calculator Part 1 by AndrewWoody from Andrew Woodward on Vimeo.
You can download the WMV video to watch locally from the Vimeo site.
