13
Jan
10

Calculator TDD Kata in JavaScript

So, I’m back from the deadosphere.

Here’s a run through  Roy Osherove’s Calculator Kata, in JavaScript, using Screw.Unit.

It was quite educational to do this kata and watch it back on video. The main things I learned were:

I am a horrible typist.

Writing code takes a lot longer than I think it does.

I expected to be able to do the beginner steps in about 10 minutes, but it actually took more than twice that long. Some of this is down to unfamiliarity with the editor (RubyMine), and some of it is standard programmer optimism.

It is embarrassing to watch yourself code….

I did this kata three times so I’ve watched myself code more than is healthy.

… but it is a great learning experience as well.

I see a lot of areas I can improve on, coding-wise, and there are some things I do a lot that I’ve never noticed before.

Without further ado…

Here it is. The first 21 minutes are the “beginner” steps, the following steps are about 15 minutes, and the remaining few minutes are code tidying. There is sporadic commentary for the bits that I feel are noteworthy.


4 Responses to “Calculator TDD Kata in JavaScript”


  1. 1 Luca
    January 13, 2010 at 10:07 am

    thanks very much. I will try Screw.Unit soon.

    I noted a possible mistake in your tdd style. For example, at 8.30 you refactor “add” method introducing “calculateSum” method when you are in red bar :)

  2. 2 anotherdave
    January 13, 2010 at 10:25 am

    Hi Luca. Thanks for watching.

    Screw.Unit is nice and pretty simple to use, especially if you have used RSpec.

    You might also want to check out JSpec: http://wiki.github.com/visionmedia/jspec

    Re: mistake in my tdd style, I’m definitely not a TDD purist.

    I often follow a pattern of refactoring as I take it from red-to-green, while re-running the tests to make the sure the green ones are still green (you’ll see it in a couple places in this kata). I guess it’s not really red-green-refactor though in that case.

    Also, in “real life” development I might write a bunch of larger-grained tests right off the bat rather than one at a time, then write smaller tests on a one-by-one basis until all of the high-level tests pass. I think it’s pretty efficient (for me at least).

    Cheers,
    Dave

  3. 3 Trevor
    January 13, 2010 at 11:05 am

    Cool Dave! It’s informative to watch other people code, particularly in languages I only use some of the time and don’t have the time to learn a lot about them.

    We use Jshoulda for our javascript tests. http://jshoulda.scriptia.net/ We were getting weird behavior with Screw.Unit that I think was self-inflicted so we tried something different.

  4. January 15, 2010 at 2:10 pm

    It’s nice to see how RubyMine works for you. I’ve been curious how folks are finding it useful, especially for javascript development.

    I was just reading about how parseInt works, and how you should always pass the second, radix argument. For example: parseInt(“012”) returns 10, whereas parseInt(“012”, 10) returns 12.


Leave a comment


January 2010
M T W T F S S
 123
45678910
11121314151617
18192021222324
25262728293031