Blog

I is for Interface

Back in 1995 I sat in a large ballroom to listen to politicians from the North and South of Ireland talk about the peace process, which at that time was far from a sure thing.

A story, told by one of the Unionists stuck in my head. He told of how he had been involved in selling a ceasefire to loyalist paramilitaries and one of the questions that he was faced with was.

Continue reading

The Anagrams Kata

The following is my C# implementation of the Anagrams Kata as described on cyber-dojo.com

Write a program to generate all potential anagrams of an input string.

For example, the potential anagrams of “biro” are

biro bior brio broi boir bori
ibro ibor irbo irob iobr iorb
rbio rboi ribo riob roib robi
obir obri oibr oirb orbi orib

Let’s write a test.

[Test]
public void NoCharacters()
{
    var expected = new List<string> {""};
    Assert.That(Anagrams.Of(""), Is.EqualTo(expected));
}

As is almost always the case, we start with the degenerate case of an empty string. We don’t do this solely to ensure the degenerate case is handled (that’s part of it), but with this first test we’re thinking about the API of our new code.

Continue reading

The Pilot and the Project Manager

To land a plane you need to line up with the runway, figure out the right rate of descent and airspeed, then monitor and manage those, all the way down to the ground.

Your goal is to touch down on the runway, rather than before or after it, while travelling fast enough that the plane doesn’t stall and fall out of the sky, but slow enough that the wheels stay attached when they hit the ground, and you can stop within the amount of runway you have at your disposal.

Continue reading

Are Unit Tests the new Comments?

It’s verging on heresy to even talk of Unit Tests and Comments as being in any way related. They serve different purposes, work in different ways, and have nothing in common.

Except

Comments

  • Document Interfaces, API’s etc.
  • Can drive development by writing pseudo code comments first.
  • Mark outstanding work using TODO comments.
  • Explain particularly complicated pieces of code.
  • Context to help future developers avoid breaking the code.
  • Document expectations, side-effects etc.
  • Can get out of sync with the code.

Unit Tests

  • Document Interfaces, API’s etc.
  • Can drive development by writing tests first.
  • Mark outstanding work using failing tests.
  • Help tease out particularly complicated algorithms.
  • Tell future developers when they’ve broken the code.
  • Test expectations, side-effects etc.
  • Likely to fail if out of sync with the code.

OK, I’m using a little bit of poetic license here to highlight the similarities. You can find a handful of similarities between almost anything if you look hard enough. Nonetheless, there are similarities. Automated tests solve many problems that comments have used to tackle.

Continue reading

Getting Functional with F# and The Game Of Life

One session at NDC that really kicked my grasp of functional programming up a few notches was Vagif Abilov’s discussion of Conway’s Game Of Life using F#.

I’m not going to rehash the rules of Game Of Life here, if you aren’t familiar with them then read this.

Vagif’s source code is on github and his slides are on slideshare. His stuff is well worth a look, but don’t look yet.

If you want to really play along with the spirit of this post, stop reading now, and go and code up Conway’s game of life in the object oriented language of your choice. I’ll be here when you get back.

Continue reading

You're Not Gonna Fix It

I’m not going to justify kludges, or apologise for kludges. I don’t need help figuring out how to avoid them. Kludges don’t usually come about because we don’t know how to avoid them. They usually exist because we make a judgement call. We decide that a kludge is not worth avoiding. Dress it up any way you like, but it comes down to a decision.

This post starts from the premise that in all liklihood there will always be kludges. I want to talk about the lie that programmers tell ourselves every time we resort to a kludge.

Continue reading

Fluent Mocking

Here’s a scenario (for once not a hypothetical scenario, this is a real system I worked on). I was building a wizard based app. To be more accurate I was building lots of wizard based apps.

After a couple of wizards the functionality became clear and I extracted it to it’s own framework. My apps could then focus on actual bread and butter functionality.

Two of the objects in my framework are ‘Wizard’ and ‘WizardStep’. A Wizard contains WizardSteps. You get the idea.

Continue reading

Retrofitting Tests To Legacy Code

One problem with TDD is that those who try it, often begin by writing a few trivial tests to get the lie of the land. Then instead of using TDD to write new code they do something much much harder.

They do what I did, they start out by trying to write some unit tests for existing code. Either a past project, or more likely the project they are currently working on. Then they discover that these projects are almost impossible to test.

Continue reading