KarateChopInCSharp

June 16, 2010
Not long ago I recorded a katacast of the KarateChopKata in Java. With a recent bit of downtime in between projects I decided to do a C# version in Visual Studio 2010 and ReSharper 5. (I put down some observations about the latest editions of these tools in Vs2010AndReSharper5 - if I had more time I'd have liked to do the same kata with just Visual Studio 2010 to help demonstrate some of the ReSharper advantages).

One catalyst for the new C# version was a small twitter discussion with Kent Beck and Paul Nelson about TDD-ing a quick sort algorithm. In my Java effort I experimented with directly exposing comparison counts from the production class, but that was messy and wrong. The production class didn't need to record counts and exposing internals is generally to be avoided. Kent tweeted a better alternative: inject a comparator class.

I've taken that approach here which allows me to properly drive the production class from the tests. The production code can go the easy route as the tests evolve but then has to shift gears into the binary search once the comparator is introduced.

This may be gratuitous use of injection for the sake of the kata, but it does demonstrate the power of injection to decouple some aspects of an algorithm, if only for testability in this case.

Unlike the Java kata where I tried to tease out internals of the algorithm from tests, in this performance I simply jump from the loop to the binary search. I've also reduced the number of tests because I wanted to try a more proper arrangement of the tests in separate methods in addition to the split screen presentation. I suppose an ideal performance would include both the comparator concept and the smaller grained tests that can go green when only part of the algorithm is implemented.



Like the Java recording, the screen capture and video encoding was done with Microsoft Expression 3 on Windows 7. KeyPosé was used for the keypress overlay and a quick shout out to Audacity for helping me quickly splice my 2 mp3 files together. There are a few edits for timesake, including the same magical appearance of the upTo method.

For those paying attention, I'm not using the default shortcuts for ReSharper. I worked with Eclipse for a couple of years before ReSharper, so ever since starting with R# I've been keeping up vssettings files to change R# shortcuts to match the Eclipse ones which can now be found here.

I decided to not do the ary() helper method in the test code like I did in the Java version, though it could be done in C#:

private int[] Array(params int[] values)
{
return values;
}


Again, thanks to my employer, Improving Enterprises, for access to the tools. As advertised at the end of the katacast, Improving offers all sorts of agile training and consulting services, click through for more details.

tags: ComputersAndTechnology
comments powered by Disqus