22 November 2010

Bad programmers don't TDD

When I finished university I felt like I could program anything. I look back on that code as my early days of hacking without cleanliness. But something changed in me when I started to do Test Driven Development. I saw myself making mistakes. Sometimes it was not fully understanding the problem and others it was the simple failure to implement something correctly. It dawned on me that TDD was saving me from being a bad programmer. Until you have had this moment with TDD it makes no sense.

Since I have learnt this lesson I have been forced to work without TDD for one project. My code was a disaster all over again. It was better designed, I had learnt that lesson well enough, but I lost a lot of confidence in the codes correctness. Alas I also got reacquainted with my debugger. So I now know I can't code without TDD, I produce rubbish code without it and I produce much better code with it.

There is no good scientific evidence that any of the programming practices of recent years actually work (DRY, TDD, CI). Many developers will tell you they feel that it improves the software but its not easy to test. Many other factors come into a project that its hard isolate the effects of them. Most of the current crop of software practices are aimed at the human condition and that just makes them doubly hard to validate. We can't prove that one programming language is better than another and that is for a good reason, they too are trying to manage the human problem. Languages are a study in the reduction of capability to improve the chance that the software will work when a human writes it. Our industry is a constant battle to manipulate ourselves into avoiding patterns that cause common mistakes.

Sometimes I am implementing code and realise I needed to do a wider refactor. Its feels like a lot of effort and it is not directly tied to what I am trying to add. So I write it down in my notepad and come back to it. I might go back to it but nine times out of ten I actually forget or justify why I do something else. So now I have learnt to slavishly refactoring until its done. Sometimes that breaks my estimates, but it normally takes less time than I think. I can't afford to skip it because too often I will justify not doing it later. I need to trick myself into doing the code well. None of these problems are really software problems, they are human factors in real projects dealing with real deadline pressures. Its really hard to keep doing the right thing especially when those around you want to cut corners. But that is what being a professional is all about, applying the practices because they improve the outcome of what you are doing most of the time.

So I am going to go out on a limb and say that you are probably a bad programmer if you don't TDD,  because it works. You also need to refactor so you don't repeat yourself and about the best way to improve is to pair program. But I can say with certainty that your a bad programmer if you don't TDD. Its important because everyone makes mistakes, absolutely everyone. The second thing to note is that it takes 6 months of constant practice to get any good at all. Your first tests will be brittle, tied to the implementation and in many other ways rubbish. But that will change with time and a focus on improving. This practice is quite possibly the single most important one for programmers to know well.

I want to leave you with an interesting debate that happened earlier this year in the British press. Surgeons in the NHS doing common procedures didn't want to use checklists. This practice seemed pointless and slowed things down so why should they adopt it? The governing  group wanted to introduce them because in mainland Europe it had been shown to save lives. It did so because people made mistakes, especially in procedures they were very used to. It seemed to work and it was so simple to do. As far as I know surgery in the UK is currently not using checklist technology despite anecdotal evidence that shows it does work.

TDD is the software industries surgery checklists, somehow many of our 'surgeons' have got it into their heads that they are infallible. They are wrong.

No comments:

Post a Comment