Practical Testing: 19 - Removing the duplicate code
The code in the last two articles in the “Practical Testing” series have contained a considerable amount of duplication. This came about for a couple of reasons. Firstly part 17 was a bit rushed and secondly it was useful to compare the CCallbackTimerQueue
implementation with the CCallbackTimerQueueEx
implementation. I’m also a firm believer that in this kind of situation it’s better to get both sets of code working independently and then refactor to remove any duplication rather than attempting to design a duplicate-free solution from the start.
Anyway, this time around we’ll remove the duplication by creating a base class that does 99% of the work and then have our two timer queue implementations inherit from it.
It’s actually easier to think about the differences between CCallbackTimerQueue
and CCallbackTimerQueueEx
rather than the duplicate code since they share so much code! The main differences are that CCallbackTimerQueueEx
uses GetTickCount64()
directly whilst CCallbackTimerQueue
fabricates a 64-bit tick count from GetTickCount()
. By hoisting most of the code from CCallbackTimerQueueEx
into our new base class, CCallbackTimerQueueBase
, we can have CCallbackTimerQueueEx
provide an implementation of the abstract method, GetTickCount64()
, which simply returns the tick count. Then we can remove the shared code from CCallbackTimerQueue
and have it use the slightly more complex implementation of GetTickCount64()
that it requires.
Finally there’s the whole concept of the ‘maintenance timer’ that CCallbackTimerQueue
uses. This can also be hoisted into the base class, made slightly more generic and CCallbackTimerQueue
can then set and manipulate its maintenance timer by calling the appropriate methods on its base class.
Once again we can be sure that we haven’t broken anything because our tests still run. There’s still some duplication in the tests and, perhaps, we’ll address this next time…