Click here to Skip to main content
15,896,462 members
Articles
(untagged)

To TDD or Not To TDD?

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
19 Mar 2012CPOL4 min read 15.9K   9   4
To TDD or not to TDD

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:

  1. 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.
  2. 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).
  3. I then write a failing test case.
  4. Write code to make it pass.
  5. 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. :)

This article was originally posted at http://nizarnoorani.com?p=434

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) NCube, Inc.
United States United States
Nizar Noorani is an independent software consultant. He provides services in the areas of web applications and systems architecture, analysis, design and development. Although proficient in a variety of technologies, he specializes in the .NET technology. He can be reached at nizar.noorani@gmail.com.

Comments and Discussions

 
QuestionTDD is not only about testing Pin
Loïc BERTHOU21-Mar-12 7:04
Loïc BERTHOU21-Mar-12 7:04 
I think that most of us programmers started to work in a certain way for some years and are comfortable with a particular logic of development. Changing these habits means going out of the comfort zone and accepting to come back to a situation where one would not be as efficient as before (so we think...).
Writing tests is viewed by a TDD rookie as just a way to ensure that the functionality does not fail when being re-factored or modified. Following this logic, why would I write my tests first then ?

The answer lies in the fact that TDD is an iterative process and is not only about tests :
1 - You write very basic tests then very basic implementation.
2 - Then add more complex tests for more complex functionality.
3 - Finally you refactor (tools in your IDE) what has just been written because from the coding arises a more efficient architecture, a test that was forgotten or a need for a new functionality (or many more reasons)...
4 - Go to step 1

By doing so, you don't need to do production-code vs test-code and you clearly see that TDD is also about software design among others.
Robert C. Martin gives a very clear and real world example in his book Agile Principles, Patterns, and Practices of this process.
AnswerRe: TDD is not only about testing Pin
Nizar Noorani5-Apr-12 15:49
Nizar Noorani5-Apr-12 15:49 
QuestionMy vote of 5 Pin
Ashley Davis19-Mar-12 13:48
Ashley Davis19-Mar-12 13:48 
QuestionGood hands on thoughts Pin
Suchi Banerjee, Pune18-Mar-12 19:31
Suchi Banerjee, Pune18-Mar-12 19:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.