TDD Pro-Tip: Time Needs Technique

TDD pro-tip: any "time" u refer to a clock, be on the lookout.

It’s a time — I just can’t help myself — to seriously microtest your way through.

If you have to talk about "now", consider whether you want to be able to pass a clock rather than call the system. For the price of an additional argument, you get perfect testability.

The price of resistance? Your now-based object will have to be thought about every time you place it inside another object as a component.

Some middle grounds that might be appropriate: 1) use a default argument for that clock. 2) use a global with settability — what most people naively call a singleton.

When you do clock or date math, always always always test on the boundary point in addition to clearly-before and clearly-after. If you don’t, you’re sure to blow inclusive/exclusive endpoints for durations.

Never assume that two calls to the system now have a certain gap between them. Two calls to now might be the same time, they might be split by a tiny amount, they might be split by a lot. It is not a countable-on kinda thing.

Finally, if you’re manipulating times a lot, please do use a serious library rather than rolling your own. It’s not cuz i’m against re-inventing wheels, I do that all the time.

It is about efficiency, but not in the way you think. You are so extraordinarily likely to handle the complexities of time and date incorrectly against all cases that you will waste way too much time — lord, help me — later debugging corner cases.

In other words, you’re gonna screw up a million different ways and spend the rest of your life sitting cold and lonely on a parkbench with a bottle and NO FRIENDS.

Oh wait, I got another, cuz joe was responding in real time — help! I’m trapped in a pun factory — think of time as a set of timestamps, and think of them as potentially going backwards. Yes. Backwards.

That’s all. Have fun.

Want new posts straight to your inbox once-a-week?
Scroll to Top