Must be having a Martin Fowler month. Only a few days ago, I posted on my experiences regarding his post on CollectionClosure, and now I want to refer to Mitch Denny’s article referring to Martin’s CodeAsDocumentation.
I’ve just finished writing some Unit Tests for our Curve model. This code has a very mathematical bias and for the last few months I have wanted to change the way it worked so that instead of exposing attributes it exposes objects. My manager has however always steadfastly refused. However yesterday we had a problem in our most critical part of our software and this was coming from the curve model. Once involved I found a missing sort on the offer side which existed on the bid side (lots of credo from the CEO).
In order to correctly prove that the change was needed, I started to work on the proving that this worked, and so I worked top down to simulate the whole process of loading markets and prices, then testing that the results matched expected values. Since I wanted to make this as bullet proof as possible I decided to try writing as Mitch suggested in very small chunks. I think the largest is 14 lines and that is where we perform 14 tests.
My results are quite shocking. Code reuse is well up. Total lines are I believe less, but certainly no more than 10%. Ease of reading is exponential improved. Why? Well just look at this function.
public void TestCase2TwoDistinctMarkets()
{
const int entity = 1813;
const int seniority = 1;
const int ccy = 1;
CurveModel cm = new CurveModel(null);
InitOldCurve(cm, entity, seniority, ccy);
cm.AddQuote(InitNewQuote(entity, seniority, ccy, 2, 90,90));
cm.AddQuote(InitNewQuote(entity, seniority, ccy, 5, 90,90));
CheckMarket(cm, entity, seniority, ccy,
79.5,
90,
90,
90,
90,
93.9130434782609,
99.1304347826087);
}
Its so readable. <evangelicalMoment>I think I can now see the light. </evangelicalMoment>