Recently, I was involved in a discussion with a colleague of mine that was focused around TDD – Test-Driven Development.
During the discussion, he mentioned that he personally finds the process of coding up the method first and test later to be easier, natural and more efficient. Writing the test-first just seems more difficult to do and less efficient. He then went on to state his reasons:
He said, and I agree with him here, it’s probably easier and feels natural because that’s the way he has always programmed. Create functionality and then test that functionality. TDD flips that around and as result you have to “re-learn” development. Something he wouldn’t mind doing, expect that…
In his opinion, TDD is a slower way to code. Here is his reasoning: A great majority of the time, you’re going to change the code that you wrote to begin with – you will rename the class or method, move things around, change the number of parameters or throw away the first attempt and start again. If you were to write the tests first, then those tests would have to change as well! As a result, you’re increasing the amount of effort involved. It’s much faster to just write the test after the code has been finalized.
Until a year ago, those were my thoughts exactly. Then one day (well, probably more like over a period of several months, several books and several articles), I finally found the missing piece to the puzzle. The piece that made TDD possible for me without causing me spend countless hours making significant changes to my test cases because I would keep making significant changes to my code-base. I realized what TDD actually states. Which is: Do NOT write a single piece of production-code before first writing a test for it. And there it is, the missing piece to the puzzle – production-code!
Ah ha, I said! I finally get it! In other words, if you’re unsure how about the solution is going to look like or work – write non-production code first to work out ideas, fill-in the gaps in requirements, get your questions answered, and get a feel for what the overall solution will look like. Then and only then are you ready to write production-code!
Here is the process I now follow during the development:
- Write a spike or POC code to “brainstorm” what the solution might look like. Sometimes, I spend quite a bit of time here. Since in the brainstorming process, you’ll find out gaps in requirements or will have to go research the third-party product that you’re planning use.
- Once you’ve got a good grip on what the overall solution might look like and how it’s going to work, I throw away the spike (or at least put it out of sight).
- I then write a failing test case.
- Write code to make it pass.
- Back to Step 1 if I am unclear on what the next piece of functionality entails; otherwise, back to Step 3.
So I mentioned that to him and he sort of agreed but not really. He was sort of on the fence. After further discussion, we hit upon another point on why to follow TDD that really hit home for him. Here is the example I gave him:
Consider the following: You’ve got X number of test cases in place for a piece of existing functionality. All of the tests pass and everything is working just fine. Then you get a change request. You make the change and then run all the tests. They all pass. Are you done? It’s a little difficult to say, isn’t it? Somewhat open to interpretation. I’ve come across developers who think that they are done at this point because they didn’t break any existing tests. But they forgot to ask – have I added a test for the new change that I made?
On the other hand, if we were to first write a failing test for the change request, then there would be no debate on whether or not a test is needed. This where TDD really shines and ensures that we’ve got tests for all of the functionality. When pressure creeps in, and this is especially true for change requests, then the discipline to add tests after the fact even when all of the existing tests are passing becomes very hard to follow-through on.
Sorry for the long-winded ranting. Hopefully you got something out of it. :)