Tuesday, July 12, 2011

Code kata challenge

Code kata is a term coined by Dave Thomas, author of several programming books. He suggest you do small exercises regularly in order to keep yourself fit as a programmer.

No doubt, this can be usefull, but doing a small bank account example ten times over doesn't make you fully capable of writing financial applications. In the real world you are likely to meet real world challenges.

Now, what I like are challenges that make me better at core features or work methods. I like challenges like "here's a set of tests, write the method that makes them all pass". If you're good at that, you we'll be better at working with unit tests (TDD in fact).

After watching Justin Etheredge on Linq over at Tekpub, I've got an idea for a code kata. Recreate the Linq functions like Justin does in episode 1. Once you've got the hang of one, move to the next. That should keep you busy for the next month or so and you will be the master of new language features like extension methods, collection initializers, delegates, anonymous delegates, and lambas.

Here's how you might extend IEnumerable in order to implement a WHERE method:
using System;
using System.Collections.Generic;

namespace Linq.Kata
public static IEnumerable<T> Where<T>(
this IEnumerable<T> list, Predicate<T> expression)
foreach(var item in list)
yield return item;
This method extends IEnumerable and takes a function delegate as parameter. We traverse the list and execute the delegate function on each item in the list. The method would be called like this:
var integerlist = new int[] {1, 2, 3, 4, 5, 6};
var integerResukts = integerlist.KataWhere(x => x % 2 == 0);

var stringList = new string[] {"anders", "christian"};
var stringResults = stringList.KataWhere(x => x == "anders");

Some links:

Kata Catalogue

Mastering Linq @ Tekpub

No comments: