Precious tessstes

I was writing a class for my project. It was test driven, going red-green, got great speed, method for method the class was growing. The coverage was 100%, everything was flowing, I was pleased.

But sometimes one need to take a step back, take a look from a higher altitude. Suddenly I realize, I can do without this class.

I look at the new solution with suspicion. It is to good to be true. But my new design makes sense. It is simpler, it provides what is needed. I attack it from several angles, but it holds. It is a better solution, so why do I hate it? Why do my mind fight it?

It dawns on me, I don't want to throw away my class.

The class itself is nothing special, it is a simple class (much of the simplicity was born from the test driven approach). The class did solve a well defined, narrow problem, but there isn't much in the class itself that makes it worth keeping.

So what is it that keeps me from throwing it all away? Sunk costs? Aversion from throwing away code? (I might be a code hoarder).

No, the costs are negligible and I do not feel any attachment to the class itself. There was nothing unique or valuable in the class. If it was I would be able to factor it out somehow. Still there was this feeling that I would lose something if I threw it out.

It is the tests.

The tests were beautiful and well crafted. Tests defines the class, and their value is coupled to that class. Throw the class out, and the tests are worthless.

The though of killing good tests like that was agonizing, and the agony was completely irrational. Still if I hadn't stopped to do some mental introspection, I could have thrown away a simpler, better solution in favor of my tests.

Personal attachment to code is nothing new, yet tests are special. because unlike other code they has no value by itself, it has only the value of the class it tests.

This does of course not mean that tests are to be avoided, just make sure that you don't get to attached to them. They are means to an end, not an asset on their own...