I had an experience a few months ago that yet again raised the question of what kind of skills are required to make XP work. I was trying to get a team to adopt TDD, and was having very little success. The reason, it turned out, was that the existing code hadn’t been written TDD. Anyone trying to add a new feature incurred a huge time penalty from trying to get to the point of being able to write simple unit tests.
So I stepped back from TDD and tried to get the team to only produce well-factored code from now on. Another failure, and this time the legacy code was only partly to blame. It turned out that only two of the fifteen people on the team really understood what well-designed object-oriented code looks like. The rest wrote working algorithmic code, and were completely blind to the duplications and strong couplings they had created. For lots of reasons, including timezone differences in some cases, it was impractical to pair with everyone to help them towards an understanding of ‘simple’ code. So I did a few public worked examples via NetMeeting, and still had almost no impact on the code being produced. I guess that refactoring is a design skill, and that it has to be taught, because most programmers don’t learn to appreciate ‘good’ code in university.
Anyhow, pressed for time and somewhat frustrated, I tried another tack. I wrote up a series of simple metrics that all new code must pass. And again I publicly refactored a piece of legacy code to remove all duplication, showing at the end that the resulting code did indeed now pass the metrics. The metrics themselves are purely a response to that particular situation, but may nevertheless prove interesting (they applied to Java code):
- every JUnit test must run in under 1 second
- maximum of 1 if statement per class
- 0-2 fields per class
- 0-1 arguments per method
- 1-3 statements per method
- fewer than 6 imported classes per class
Of course ‘bad’ code can still pass these tests. But I was trying to get across an understanding that each method and class should do only one thing. So I made it very difficult to find enough space to do two things!
Did they encourage the team to write better code? I’ll never know, because the project was soon cancelled…