Programming Koans: The what, how and why

in Software Engineering, Testing

Immediate digression, I really wanted to call this piece The French KOANection.

As a developer, there is nothing that beats trying out a new methodology, design pattern or the newest JavaScript framework than getting your hands dirty and as close to the code as possible. Group exercises such as katas use this foundation to complete exercises from a given context: developing a shopping cart for instance - something that is designed to be completed multiple times, bringing a new angle or method to the next, new attempt.

Koans can be seen as stripped back katas, the elevator pitch of a koan is to explain programming concepts through unit tests - presenting failing tests and requiring the candidate to fix the tests in an effort to imbue knowledge. Going through a koan is a good method of jumping into using a new programming language or framework with them being similar to katas, presenting the theory of testing and red, green, refactor (though admittedly not much refactoring is required). If a kata was owning a car, driving it to work and back everyday then a koan would be going for a test drive, tuning into your favourite radio station, finding where the best hiding places for sweets are and learning why you should never, ever, push the little red button on the top of the gear stick.

After 'fixing' the given assertion library for your koan (A meta-koan of sorts, showing how to assert equality etc), a straight-forward example for JavaScript could be the following:

describe("About addition operator", function() {

  it("should understand adding two number together", function() {
    var expectedValue = FILL_ME_IN;
    var actualValue = 1 + 1;

    expect(actualValue).toEqual(expectedValue);
  })
}

In the above example FILL_ME_IN would be changed to a value that hopefully satisfies the test and makes it pass. The idea being that the process of reading the code, interpreting it and modifying it accordingly would infer an understanding. The structure, syntax and underlying purposes of, in this case, the addition operator when used for numbers is inferred by fixing the issue. An additional test could be added, but for using the addition operator for strings in JavaScript (something you may or may not be too familiar with) and a concept that is learnt by fixing the test.

Just like katas, koans can be completed solo or in groups to help gain collective knowledge. As they are split into logical sections for concepts / themes they allow more flexibility. If you want to see what the fuss is all about in F#, why not have a go over your lunch (not including initial setup time!).

Koans are valuable exercises to get practice for a language or framework, no matter what your experience is.

Koan Resources

There are loads of koans out there for languages and frameworks.