Question

I'm working on the data model of a service describing houses and flats. This involves storing quantitative physical properties of certain features of the premises, for example:

  • Speed of the Internet connection in Mbps
  • Length of a swimming pool in meters
  • Noise level of kitchen appliances in dB

The quantities will always be specified and stored in terms of the same units, and there will be no need for performing calculations on them. The most complex operation will be comparing and ordering values of the same unit.

My question is: would you recommend me to use a proper physical units library for storing these quantities, or is it valid to decide that I don't want to depend on a 3rd-party library just for storing some numbers if I won't be performing arithmetic on them, and I know they are always specified using the same units?

Was it helpful?

Solution

Usually the reason to support multiple units of measure (aside from measuring things) is to support localization or conversions between systems (e.g. miles to kilometers).

The conversions from one unit to another are widely known. I would even argue it is more a matter of localization than anything else. I live in the USA, so my spacial awareness deals mostly with inches, feet, and miles. Someone from Canada (or practically anywhere else on planet Earth) is probably more familiar with the metric system.

So, decide on which one to store. You settled on the metric system. So if you need to localize this system, convert meters (or metres) to feet.

  1. Read from DB
  2. Convert
  3. Display

It follows the same pattern as dates and times, frankly. Dealing with different units of measure in the database does not benefit the application, since conversions are easy if you need to support other measurement systems in the future.

Since a possible refactoring in the future might be localizing the measurement systems, you are good for the time being having designed the system with storing values in one measurement system.

The reason you don't need a library just yet is not to KISS it, or YAGNI --- it's because the cost of refactoring to support other systems is just another form of localization that could be handled in the UI layer of the application. It shouldn't need to be a wide spread gutt-and-rewrite refactoring job.

OTHER TIPS

Just distill the question down to its essence:

Should I use a library for something I don't need.

The answer is obviously no.

As alternate point of view, without the library, a label might have a signature like this:

String dimensionsLabel(int height, int width); 

If this signature appeals to you, it might be less work to use a library than rolling your own types:

String dimensionsLabel(Meter height, Meter width); 
Licensed under: CC-BY-SA with attribution
scroll top