If you need an object that includes time, you’re probably going to need to convert it. Unit conversion is the cause of many bugs, some more disastrous than others.
When designing software, always make the right thing easy and the wrong thing hard.
What if you never had to bother about which one it was and would get it right 100% of the time no matter if you needed milliseconds or hours?
Here’s how.
Let’s presume you’re measuring exercise duration.
What you get from using this pattern is, it’s impossible to mess up saving or getting information from that object.
It doesn’t matter what internal representation you choose. You’ll never go wrong in either setting or getting time out because you choose a unit each time you actually need it.
You’ll never go wrong in either setting or getting time out because you choose a unit each time you actually need it.
Caution Points
- Watch out for precision loss with nanos, consider your own usecase.
- Don’t use this to get days, weeks or, heaven forbid, years. Dates are more complicated than you think, use JodaTime for anything like that.
Ask me questions on Twitter or comment below!
Top comments (1)
Lockheed Martin should be sued for negligence. If you work in science, you never don’t work in metric. Accident is 100% their fault.