Tuesday, October 16, 2012

Should tests influence the production code?

For a very long time I was told that tests should not influence the production code. By this I mean, that we should not change our design only in order to be able to test it. That was quite OK for me, since - you know - test are only tests - the production code is what matters and what drives the design. Tests should just shut up and follow. Right?

Lastly I am flipping the bit and diving into TDD and I must say - this is quite an adventure. At the beginning I was like a babe in the woods. It was not easy to start. Everything was harder - writing code, designing, thinking... I had to firstly write a test, and then the code - everything was upside down - I didn't even have the code to test. Damn! I started to think about basics again, and that... was really refreshing!

The Example

One day I was given a task to figure out changes in history lines of some objects and update the meta-data for each line accordingly - with ids of objects that were changed. For the simplicity of the example let's assume, that those objects were rectangles and I was interested only in their length.

Let's use an example:

As you can see (you have to excuse me my poor Paint skills), the rectangle A was changed only in the first step (transition from A1 to A2), rectangle B was not changed at all and rectangle C was changed only in the second step (transition from C2 to C3). As a result, there should be the id of the rectangle A in the Line 2, and the id of the rectangle C in the Line 3.

The task was not that hard and my very first thought was to create some meta-data generator that would accept a list of Lines and update each of them. Sounds easy, doesn't it?