I’ve been hearing about the benefits of Test Driven Development (TDD) for awhile now, but have never taken the time to try it out with any of my own work.
That was until this past week.
The code that I work in with my job doesn’t really lend itself to TDD simply because it’s all straight-through procedural code. The few functions that do exist would only be able to be tested by checking the raw HTML that the emit against an expected result. It’s possible, but extremely tedious so we just avoid it completely.
Recently though, I had the opportunity to write some new code, a small module completely independent of any of our existing code. I decided to give TDD a shot and see how things went.
It took a bit to get PHPUnit up and running on my dev machine, but once I did things started to progress nicely. I stubbed out some of my initial functions and got my tests failing, it was pretty exciting in the way that playing with new tech usually is.
I’m by no means a TDD expert, but here’s some things I noticed with my experience so far:
- I REALLY need to refresh my Object Oriented design skills, it’s been too long since I’ve used them
- By doing just the minimum to make the tests ‘green’ I wrote far less code than I thought I would
- My current code is far too coupled, I need to watch out for that because it’s hard to test
- Once some tests are in place, refactoring is awesome, you know instantly if you broke something
- TDD feels like another safety net above source control
- Writing tests isn’t as hard as I thought
- TDD is a completely different way of thinking about writing software
I’m pretty excited to continue on this path and fully explore what TDD can offer. I’m thinking some continuous integration and maybe, just maybe, some automated UI testing down the road. It’s a brave new world!
What’s your experience with TDD been like? Any tips PHP specific or not? Please just leave a comment below.
Leave a Reply