refactoring is hard

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):

  1. every JUnit test must run in under 1 second
  2. maximum of 1 if statement per class
  3. 0-2 fields per class
  4. 0-1 arguments per method
  5. 1-3 statements per method
  6. 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…

Advertisements

5 thoughts on “refactoring is hard

  1. Interesting, and very much echos my experience: many developers claim good understanding of OO but in practice often don’t display a good talent for actually writing good OO code.

    I recently read David West’s book, ‘Object Thinking’, in which he cites a set of empirically derived metrics for successful OO projects (from research by Lorenz and Kidd). Your metrics aren’t a million miles away from these :o)

  2. Great, I love reading posts about dealing with an average developer/team.

    Too many experts talk about working with geniuses, whereas right now I’m more interested in getting the team pointed in the right direcetion, before I can even think about the other stuff.

    Also, the link in your article with the text “maximum of 1 if statement per class” is out of date.

    Cheers,
    Dave

  3. It’s true to have developers to think in object oriented way is a sort of NP-complete problem :P
    So when you have a chance, make sure at least you are able to hire the right kind of person at the start.

  4. Pingback: object thinking « silk and spinach

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s