Monday, 1 June 2009

Why I love TDD/BDD

After watching Uncle Bob's presentation at RailsConf a couple of weeks ago I was reminded by one of the points he made of the reason I love TDD and more specifically BDD so much. Actually there are plenty of reasons I love it but one stands out above the rest as the main one.

Now this is something that plenty of people have already talked and blogged about before but I just wanted to get involved and say it myself.

TDD/BDD is great because it removes the fear from refactoring.

For me there is something that tends to be found in the best developers. Its not that they can always come up with the best design for every problem before they even touch the keyboard. Its not that they never write code that goes against the SOLID principles. It's not even that they have learnt every pattern in the book off by heart.

The best developers are those that continuously refine their code. They take something that is just a solution to the problem at hand and transform it into something which is clean and elegant. They craft a piece of code that is easily understandable by other developers and which can be easily maintained as the requirements of the system changes.

For me refactoring is the main technique I use when developing. I often describe my approach to it as 'aggressive refactoring', firstly because it sounds cools and secondly because it gets across how important I think it is. Refactoring lets me take my initial and often messy first stab at solving a problem and change it into something I can be proud of and which captures the insight I gain as I improve my understanding of the problem. If I just did what far too many of the developers I've met do and simply solved the problem the first way I could and then moved on, I'd leave behind me a trail of unmaintainable code.

To bring this post back to the subject of its title, what TDD and BDD give me is the safety net that tells me my code still does what I want it to do after I've refactored the hell out of it. This works just as well for code I wrote a minute ago as it does for code I wrote a year ago. In fact for any system that is going to last longer than five minutes the only way it can avoid becoming the unmaintainable mess that most legacy systems are is by being built in such a way as to support constant refactoring during its lifetime and that means having a good suite of tests or specs.

And so almost solely for that reason I love you TDD/BDD

0 comments: