This post originated from an RSS feed registered with .NET Buzz
by Sam Gentile.
Original Post: Educating Microsoft and .NET Community on TDD
Feed Title: Sam Gentile's Blog
Feed URL: http://samgentile.com/blog/Rss.aspx
Feed Description: .NET and Software Development from an experienced perspective - .NET/CLR, Rotor, Interop, MC+/C++, COM+, ES, Mac OS X, Extreme Programming and More!
Following up on my post and others, Mike Gunderloy rightfully says, “First, they need to yank the offending content from MSDN before any more people notice how stupid they look. Second, some of the more prominent Microsoft agile bloggers need to step up to the plate to explain what they're doing to evangelize within the company so that this sort of thing is less likely to happen again in the future.” Both things need to happen. On the second, I know that there are prominent Microsoft agile bloggers internally working this issue and educating. It is also our job on the outside of the company but as part of the Agile community. One of the first places I would point people to is my friend Jim Shore's post on what TDD actually is. Let me draw out his steps so its very clear. We call this process Red-Green-Refactor:
Think: Figure out what test will best move your code towards completion. (Take as much time as you need. This is the hardest step for beginners.)
Red: Write a very small amount of test code. Only a few lines... usually no more than five. Run the tests and watch the new test fail: the test bar should turn red. (This should only take about 30 seconds.)
Green: Write a very small amount of production code. Again, usually no more than five lines of code. Don't worry about design purity or conceptual elegance. Sometimes you can just hardcode the answer. This is okay because you'll be refactoring in a moment. Run the tests and watch them pass: the test bar will turn green. (This should only take about 30 seconds, too.)
Refactor: Now that your tests are passing, you can make changes without worrying about breaking anything. Pause for a moment. Take a deep breath if you need to. Then look at the code you've written, and ask yourself if you can improve it. Look for duplication and other "code smells." If you see something that doesn't look right, but you're not sure how to fix it, that's okay. Take a look at it again after you've gone through the cycle a few more times. (Take as much time as you need on this step.) After each little refactoring, run the tests and make sure they still pass.
Repeat: Do it again. You'll repeat this cycle dozens of times in an hour. Typically, you'll run through several cycles (three to five) very quickly, then find yourself slowing down and spending more time on refactoring. Than you'll speed up again. 20-40 cycles in an hour is not unreasonable.
One thing I want to emphasize to you; this is a testing method but it's primary function is design. TDD and Red-Green-Refactor is an organic, discovery process of design. I start with a User Story task and since I don't really know much up front, I discover it. I think what I want to do and then codify my question of what I am trying to discover or my design step into a test. I keep iterating this until I find out what I want to know or I have designed the method I am designing and it has been refactored into its most clean, agile self. Then I move to the next. When we say TheSourceCodeIsTheDesign, we know that BDUP does not work. We don't know enough about the domain or the system until we actually start writing the tests and the code and have the “system tell us” so we don't waterfall a 3 months “Requirements phase” because we know, no matter how hard wemight try, we haven't got everything and the requirements will change constantly as both Business and Development learn more about the system together. This isn't cowboy coding but a very disciplined approach of design-code-test-Refactor in many iterations. Design is a constant activity. For another great description of TDD see Scott Bellware's post and let's talk some more. I prefer this conversation to take advantage of the natural synergy of blog communities so link to this and lets talk.