A couple of thoughts I’ve had on the TDD process that may not be inherently obvious.
- If you already have a complete spec with all of the API, and cannot deviate, you are not doing TDD.
- When writing the test first, the failed compile is a good thing. This helps prove that you don’t have any kind of strange name overwrites that you’re unaware of. The value of this, of course, depends on language.
- Once the compile errors are fixed, the failed tests tell us that the component isn’t “accidentally” doing what it needs to be. If it is, we need to figure out in what circumstances, or why.
- One of the big problems people seem to have with TDD is not refactoring. Refactoring is, in my opinion, the most important step. Without refactoring, using TDD, you will have garbage code (though it’ll probably have a nice API).
- People seem to balk at the idea of writing the minimal code to get the test to pass. They somehow think that this is supposed to be final code. It’s not (necessarily). The point is to allow you to enter the “green” (refactoring) phase earlier and with less work. When possible, we want to be in the “green,” so that we know if we do something that causes a break. If we try to write our ultimate design up-front, that just means that a lot of code is being written at one time without any assurances that it works.
- If you find that you need to refactor to make a test work, comment out the test first, and then do the refactoring. Always refactor when green. Otherwise, you’re getting into the trap of refactoring while changing functionality, and that’s a slippery business.
- People think TDD means no design. I don’t agree. Generally, you start with a class, which implies a certain amount of design. But TDD folks think about design, a lot. They just don’t think about design a huge amount up front, except for high-level architectural decisions, and they retain flexibility to change the design in case they were wrong.
And a final though: Since adopting TDD, I find development without it to be somewhat weird. Just as non-TDD’ers think we’re weird for writing tests first, I think what they’re doing is strange. To me, writing tests last (or not at all!) is essentially writing a bunch of code, and then trying to figure out what it does. It just feels very unsafe, and I just get a weird feeling when doing it.
I don’t know how valid that is, or if I’ll think I’m an idiot in five years for thinking like this.
Post a Comment
You must be logged in to post a comment.