I wasn’t going to blog about today’s 12 TDDs of Christmas but for two reasons…

Firstly: today was the first time that I added a major refactor as part of the process.

Refactoring is simply another means of saying that you altered – hopefully for the better – some code that has previously been written without affecting the way in which it operates from an external perspective. The major benefit of tests in your development is that your tests allow you to test that any refactoring you do does not have any knock-on effects. The major benefit of test driven development is that your tests should comprehensively test the code so that you have confidence in your work.

Today’s challenge – “Ranges” – is simply to create a Range object that represents a range of integers (then doubles). That object can then allow you to retrieve the minimum or maximum, or to retrieve an intersection between this range and another. I started by coding the integer range object, starting with constructor tests and moving onwards, fleshing out the functionality. Same iteration over the double range object. All in all, 26 tests. Then, as had been my plan, I decided to alter the code to run off a base class using generics. What do you know – all duplicated code reduced down to one generic class and the original two IntegerRange and DoubleRange which just contain constructors with the expected signature. And what’s more, all tests are green. Major refactor, major set of duplicated code removed, confidence I haven’t ballsed something too major up. That’s gold dust.

The second reason I decided to blog was I’ve just read @martin_evans‘ blog post entitled The 12TDDs of Christmas – the Times Crossword for Programmers. In it he nicely posts that he likes my approach to the Number Names challenge. I like my approach too. I don’t like some of the code in it, but I like the approach. :-)

As always, the code’s available at the BitBucket repo: https://bitbucket.org/CraigHawker/12-tdds-of-christmas.

Items in this series:

  1. 12 TDDs of Christmas (day one: Calc Stats)
  2. 12 TDDs of Christmas (day two: Number Names)
  3. 12 TDDs of Christmas (day three: Mine Field)
  4. 12 TDDs of Christmas (day four: Monty Hall, day five: FizzBuzz and day six: Recently-Used List)
  5. 12 TDDs of Christmas (day seven: Template Engine)
  6. 12 TDDs of Christmas (day eight: ranges)