Wednesday, March 17, 2010

Test Driven Reverse Engineering

I stumble upon a piece of code, trying to figure out what the code was doing.
There was some useless comments, and the method I was studying was simply too complex.
To complete the picture, no unit test was available.

So instead of trying to understand the code, probably missing a lot of subtle flows, I created a unit test from scratch. Doing so made me understand the behavior of the particular class.

As in Test Driven Development, you are writing the behavior of the class in the unit test before implementing it.
(Check the very good M. Fowler article)
In my case the code was there, so it was Test Driven Reverse Engineering.
The end result was great :
  • I could understand the behavior of the method
  • Having a working unit test, I could easily refactor with confidence to make the code understandable.

No comments: