Can you hide the units?
Sebastian Good

In the kind of programming we do — scientific simulations and decision support — modeling is usually the first task, and often the hardest. Structuring your problem the right way can make all the difference in determining whether future code is graceful or spaghetti-like.

I’ve been unconsciously using an approach recently which seems worth noting. I’ve been trying to get rid of units of measure in our models. You know, those pesky things that often go assumed or, worse, undefined, in a lot of systems. Meters? Feet? US Survey feet? Arbitrary but consistent measures of length? If representing feet in a domain event, then is “ft” good enough? Or should you use“urn:ogc:def:uom:UCUM:[ft_us]“? For many key attributes, you can’t escape saying what’s what.

But it seems like in many cases you can pull a fast one and make the units disappear from your model, so you’re more immune to changes in representation. How? By following the same rule that journalists should follow when they quote units: divide them by other units. For instance, would you be impressed or unimpressed that about 27 million people were added to the US population between the 2000 and 2010 census? I’m not entirely sure I know either. But if I told you that was a 9.7% increase in 10 years, you’d be better off.

If we can reduce our inputs to a unitless fraction — ratios —  then we do. A recent example involved storing the viewport size for a persisted visualization session. We could have saved something like “1600×1200″ and had people guess it was pixels. We could report a click in the middle of the screen as coming in at “800×600″. But then when that session was rejoined on another machine and the poor user was on projector which only supported a screen which was 1024 pixels wide, then we’d have to recalculate his viewport to be something like “1024×768″ and report that center point as coming in at “512×384″. The math you’re doing in your head there is already dealing with ratios instead of numbers. That aspect ratio is 4:3, and that center point is (0.5×0.5). This way we’re ready to display our scene on any sized screen.

The dimensional analysis doesn’t always work out this way, but where we can get away with it, we’ve been pleased.

Where can you hide your units?