Clever Design meets TDD
I've been working on my own PHP framework for a while now. I use it as a scratch pad to try out new concepts. I recently decided that I wanted to make some architectural changes that I thought were super clever (yes I know super clever is nearly always too clever), things that would save me tons of tedious programming, free my mind from all the mundane details that I detest, maybe make toast. The inspiration was so intense that I made my changes first then thought about testing.
When is Testability not important?
The answer to that question is obvious isn't it? Testability is always important. I spent a couple days implementing my new architecture, then about a week figuring out how to test some of the more complex features. I was discouraged to say the least so I headed out to scour the net for some insight into where and what I had done wrong and how I could make my testing life easier. I found plenty of advice and in another day I had come up with much more testable design, in other words pretty much my framework before the changes. I had a dilemma, testing or my beautiful simple elegant design.
After a lot of thinking, I realized that what I was doing wasn't too clever, it wasn't bad design, it was a combination of a mental block on my part that has been pounded into my head by dozens of articles and books expounding the virtues of low coupling, a lot of naive unit testing examples for PHP and a desire to play with the cool kids who only do TDD.
I went back to my original design and worked harder at the test cases with a new perspective on coupling, why was I trying to decouple classes that would never be used outside my framework, my framework is the runtime environment they will always live in, and suddenly my tests became much easier to write. I would never have been able to accomplish this with my old perspective on testing.
Finally the reason I wrote this post, It seems like the TDD buzz is getting ever louder in the PHP world. I am not knocking TDD, but I wonder if certain viable maintainable designs could ever evolve using TDD? Don't let the current favored methodology of the programming community stop you from thinking for yourself, there is no one true way. I think I'll stick with test early and sometimes just sometimes try being too clever.
Comments
One Response to “Clever Design meets TDD”
Leave a Reply
Keep in mind that you'll never have 100% code coverage in unit tests, and even if you're near it won't prevent bugs that you didn't think could happen. Sometimes it's better to just go on and add tests when you stumble on bugs to prevent regression. Another good moment to write tests is when you refactorize a part of your code without changing its interface. You'll be able to ensure the output looks the same as your previous version of the code, and that'll add more tests to your project.
Testability is an important part of our framework and despite this we don't have that much tests yet since we only add tests when we stumble on problems or when refactoring. But then again we're not proponents of TDD, we just appreciate the usefulness of unit testing.