For November’s Tech Talk we learn why Test-Driven Development (TDD) affects design with James Shore.
You’ve said that TDD can lead to better design. Can you tell me more?
There are a few ways I've seen TDD lead to better design:
None of these force you to create better design, but if you're working hard to create good design, I find that these things make it easier to get there.
Isn’t the opposite also true? Can’t poor TDD lead to bad design?
Yes. There are a few ways I've seen TDD lead to worse design:
None of these are required by TDD, but they're still common. The first two are obvious solutions to the sorts of design problems TDD exposes. They're also very poor solutions, and you can (and should) choose not to do these things. It is possible to create fast tests and rapid feedback without these mistakes, and you can see us take that approach in the screencast.
What do you recommend engineers do to ensure TDD leads to good design?
TDD doesn't create good design. You do. TDD can help expose design smells. You have to pay attention and fix them. TDD can push you toward facile solutions. You have to be careful not to make your design worse just to make testing better.
What's the bottom line? Should we use TDD?
TDD is an excellent tool for improving design, as long as it's used as a tool and not a panacea. TDD can help you see new ways to improve your design, but some of those aren't obvious. You are a programmer and have to pay attention and be creative. It won't just happen automatically. If TDD is pushing your code in a direction that makes the design worse, stop and think about what you can do differently.
So pay attention. Think about design. And if TDD is pushing you in a direction that makes your code worse, stop. Take a walk. Talk to a colleague. And look for a better way.
Learn more on James' blog.
These Stories On News