Monday, March 23, 2009

Expressing time as a domain object, instead of insane long calculations


Hands up everyone here who has had to represent time as a long? OK, next question, how many of you have had to express a large amount of time (for example 7 days) in milliseconds? I bet you've done this:
7*24*60*60*1000

Kind of nice, and I bet you've even refactored like this:
HOURS_IN_A_DAY*MINUTES_IN_AN_HOUR*SECONDS_IN_A_MINUTE*MILLISECONDS_IN_A_SECOND

OK, getting better. It's a bit clearer as to what all those troublesome little numbers mean-but something still not quite right. What if, say for example, you pass this result into a method that accepts time in a different unit of measure? eg.
void sleepFor(long seconds) {
Thread.sleep(seconds*1000);
}

Now the compiler isn't going to complain, you're going to be waiting a lot longer than you anticipated-say 1000 times longer? We've passed the wrong unit of time into this method. Our constant is represented in milliseconds, yet this method accepts time as seconds, but they are both the same datatype! But, I hear you say, we don't want a different datatype for every unit of time-that would be madness. Indeed it would.

To stop the world descending into a pit of chaos, overlorded by mad monkeys*, try this. Don't represent time as a long, represent the amount of time you are interested in as a long, and combine it with a TimeUnit!

Call it what you like, but I called it Duration. An example of which can be found in a project I'm currently working on called gibble here. This is only a small example of the Duration class that I use elsewhere.

Now, we can represent the above code as follows:
sleepFor(days(7)); //static import of Duration here

And the sleepFor method like this:
void sleepFor(Duration timeout) {
Thread.sleep(timeout.inMillis());
}

What's so cool is that we can never be tricked into sleeping for the wrong amount of time! I hate being tricked into sleeping 1000 times more that I have too. Sleep too much and the monkeys might take over!

* Would that be any worse/different that what we have now?

1 comment:

  1. awesome! Duration is the best abstraction since Bread and Slice.

    ReplyDelete