3

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?

  • 6
    KISS because YAGNI – Constantin Galbenu Mar 17 '18 at 19:49
  • 1
    As long as fellow programmers have a way to lookup / ascertain the physical unit of a particular object field or database column. This can be done with documentation or variable naming. Also, noise level is probably going to be in dB (so, there is no confusion with the unit), but the minor detail that makes a difference is the noise measurement methodology or test procedure (PDF). These don't belong to code, but belong to the policy of those using the software. Perhaps a string comment field may be needed. – rwong Mar 17 '18 at 20:07
  • 1
    @rwong thanks for the feedback! Yes, currently property/field names are annotated with the unit, e.g. noise_level_db or download_speed_mbps. – H2CO3 Mar 17 '18 at 21:09
  • 1
    @DocBrown I would say the main difference between "documentation and variable naming" and "proper physical units library" is that the former is "human readable, with a bit of information hunting", and the latter is "machine readable, but also requires the human to do a bit of information hunting". Modern IDEs aren't resourceful enough to show you the physical units of a variable when you hover the mouse over it. – rwong Mar 17 '18 at 23:49
  • 1
    Remember that the alternative to using a library doesn't have to be using primitive types - you can write your own classes for modeling physical quantities with just the functions you need. – bdsl Mar 18 '18 at 10:05
3

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.

| improve this answer | |
1

Just distill the question down to its essence:

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

The answer is obviously no.

| improve this answer | |
0

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); 
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.