I have an application that needs to accept and output values in both US Custom Units and Metric system.
Right now the conversion and input and output is a mess. You can only enter in US system, but you can choose the output to be US or Metric, and the code to do the conversions is everywhere.
So I want to organize this and put together some simple rules. So I came up with this:
- user can enter values in either US or Metric, and User Interface will take care of marking this properly
- All units internally will be stored as US, since the majority of the system already has most of the data stored like that and depends on this. It shouldn't matter I suppose as long as you don't mix unit.
- All output will be in US or Metric, depending on user selection/choice/preference.
In theory this sounds great and seems like a solution. However, one little problem I came across is this:
There is some data stored in code or in the database that already returns data like this:
4 x 13/16" screws, which means "four times screws". I need the to be in either US or Metric. Where exactly do I put the conversion code for doing the conversion for this unit?
The above already mixing presentation and data, but the data for the field I need to populate is that whole string. I can certainly split it up into the number 4, the 13/16", and the " x " and the " screws", but the question remains... where do I put the conversion code?
Different Locations for Conversion Routines
1) Right now the string is in a class where it's produced. I can put conversion code right into that class and it may be a good solution. Except then, I want to be consistent so I will be putting conversion procedures everywhere in the code at-data-source, or right after reading it from the database. The problem though is I think that my code will have to deal with two systems, all throughout the codebase after this, should I do this.
2) According to the rules, my idea was to put it in the view script, aka last change to modify it before it is shown to the user. And it may be the right thing to do, but then it strikes me it may not always be the best solution. (First, it complicates the view script a tad, second, I need to do more work on the data side to split things up more, or do extra parsing, such as in my case above).
3) Another solution is to do this somewhere in the data prep step before the view, aka somewhere in the middle, before the view, but after the data-source. This strikes me as messy and that could be the reason why my codebase is in such a mess right now.
It seems that there is no best solution. What do I do?
My goal is to figure out how to structure and where to place my unit conversion code within the system. Software system in question is MVC-like (Zend Framework 2 - like) LAMP system.
Right now I have some class that given an
id at creation time, has a method
getValue(), which returns
4 x 13/16" screws. Depending on
id, different numbers can be produced, but they are still in inches. I should note that some numbers are written with the dividing bar (
13/16") and some use decimal notation (
0.8125"). How could I set up my framework so that I can display such values in millimeters as well?