TDD – Building Complex Algorithms with Test-Driven Development

algorithmstdd

I'm trying to adopt TDD in my daily programming practice. I use it at work very effectively, but I'm having trouble with my personal projects where I'm using some complex algorithms.

The particular algorithm that makes me ask this question is the Extended Kalman Filter. It's complex enough that I'm not confident in the code I've written, but it's simple enough that it's hard to break up.

I could write a test for the algorithm with an input and the expected output, but I'll do a lot of thrashing and shotgun coding in the middle because I don't have confidence in those intermediate steps.

If you've worked with reasonable complex algorithms and use TDD, what is your approach?

Best Answer

TDD is not a substitute for design.

This is a common misconception about TDD, that you can somehow "grow" a coherent design from red-green-refactor. You can't. Your tests still need to be guided by your design skills.

If you're trying to figure out what to do between the input and output of your method under test, it probably means that you're trying to do too much in a single method. Think about what those intermediate steps should do. Write tests for those intermediate steps, and write methods that pass those tests.

Related Topic