December 3, 2009 8

String Calculator

By Corey Haines in Katas

Roy Osherove and Gil Zilberfeld of TypeMock talked about the katacast series on their podcast, ‘This Week in Testing,’ and called me out to do Roy’s standard TDD practice, String Calculator. There are a few screencasts of this kata in C# linked from his page, which are well-worth watching. It is always interesting seeing the same kata performed in different languages. Ever eager to take up Roy’s challenge, I spent some time on the kata.

In the interest of time, I only did the first part (for beginners) of his kata, and here is Roy’s description:

  1. Create a simple String calculator with a method Add(numbers) that takes a string
    1. 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”
    2. Start with the simplest test case of an empty string and move to 1 and two numbers
    3. Remember to solve things as simply as possible so that you force yourself to write tests you did not think about
    4. Remember to refactor after each passing test
  2. Allow the Add method to handle an unknown amount of numbers
  3. Allow the Add method to handle new lines between numbers (instead of commas).
    1. the following input is ok:  “1\n2,3”  (will equal 6)
    2. the following input is NOT ok:  “1,\n”
    3. Make sure you only test for correct inputs. there is no need to test for invalid inputs for these katas
  4. Allow the Add method to handle a different delimiter:
    1. 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 ‘;’ .
    2. the first line is optional. all existing scenarios should still be supported
  5. Calling Add with a negative number will throw an exception “negatives not allowed” – and the negative that was passed.if there are multiple negatives, show all of them in the exception message

As always, thanks to Techsmith for the Camtasia license. It is a pretty smooth screen capture app with a fairly low learning curve.

And, thanks to Sarah Gray for pairing with me on the first draft of a solution in Ruby.

I’ve pushed up a practice git repo here.

Enjoy! As always, I appreciate any and all feedback in the comments section. Or, better yet, write a post on your blog with a screencast of your own!

(it is tough to see so small, just click the full-screen button on the video. Or, you can go to vimeo and download the source video directly).

String Calculator from Corey Haines on Vimeo.

8 Responses to “String Calculator”

  1. XenThraL says:

    Made my first one of these as a simple test.
    language: lua, runtime: ~19 minutes
    http://www.vimeo.com/8027709

    Think I implemented all of the points in the description except for 7

  2. Corey Haines says:

    Thanks. Interesting. I’ve not seen much lua, so that was neat.

    A couple comments:
    - It didn’t appear that you needed the if bNegative in the first loop, as you immediately put a if digit < 0. Is this true?
    - Why did you add the negative numbers to the resultsTbl? You didn’t end up using the table at all, so, if you had negatives, you didn’t need to do that loop at all, the final string building loop would have done what you needed, right?

    I’d love to see it recorded more smoothly, perhaps seeing a TDD approach with some more cleanup/refactoring of the code between the steps; it seems like the code ended up with a lot of ifs and loops. Could you remove those?

    Thanks. If you practice it up more, I’d love to see it on your blog with an explanation of some of the parts you were focused on practicing.

  3. chris says:

    Any chance of getting the katacasts as a podcast for on the go craftmanship training? ;-)

  4. XenThraL says:

    “It didn’t appear that you needed the if bNegative”
    “Why did you add the negative numbers to the resultsTbl? You didn’t end up using the table at all”

    I did that to fulfill the 5th requirement which needs me report all negative numbers, so I check ahead if theres any negatives, then in the loop I just collect the negatives in resultTbl.
    I could have done it without bNegative, but in that case I’d have filled resultTbl with both positive and negative numbers, checking ahead helps maintain DRY.

    “it seems like the code ended up with a lot of ifs and loops. Could you remove those?”
    Probably, its looking very Cish at the moment and not very ‘ZenOfLua’ :)

    “I’d love to see it on your blog with an explanation of some of the parts you were focused on practicing.”
    Don’t really have a blog…this kata is good practice for me to remember the “magic patterns” in lua, since I don’t use them very often and when I do need them I have to keep a list on the other monitor.

  5. [...] Corey Haines recreated my TDD Kata on his KataCasts blog.  (click here to learn more about TDD katas) [...]

  6. Travis says:

    On a whim, I decided to check cast out. To see how you did the kata. I assumed you did this in VS.NET/C#. A little turned off seeing VIM, OSX and black console windows. But bare with me here…

    But honestly, after watching it for a few, especially after you started running your spec tests, for the first time I think actually I get it, get it meaning Ruby. The way you have your 3 windows laid out and how easy it is for you to code, run your tests and see the results was simply amazing to me. I have to say, I’m a changed man. I guess all it takes is to see a pro do it. Downloading Ubuntu as I write this. Thank you Corey.

  7. [...] Corey Haines recreated my TDD Kata on his KataCasts blog.  (click here to learn more about TDD katas) [...]

  8. Wow. I just learned ALOT watching that screencast. I look forward to watching it again and watching the Kata for the other languages.

    By the way I thought of you when I read this quote on mastery:

    “We fail to realize that mastery is not about perfection. It’s about a process, a
    journey. The master is the one who stays on the path day after day, year after year.
    The master is the one who is willing to try, and fail, and try again, for as long as he
    or she lives.”

    ~ George Leonard from Mastery, http://bit.ly/da3pAv

Leave a Reply