Thursday, March 3, 2011

converting number units in Magik

I was looking at some 3rd party custom code today and noticed what seemed like really helpful methods...
simple_number_mixin.cm_per_foot
simple_number_mixin.cm_to_feet

I think the 3rd party product developers thought this would be a very convenient method. It definitely is, but the problem is that other product developers working on other components made use of these methods. When the 3rd party product was removed, the other custom components broke. The 3rd party product developers rightly declared these methods as RESTRICTED, so the custom product developers should not have called these methods... but we all know that method classifications are widely ignored.

So, how can you make use core Magik classes to do exactly the same thing? Make use of the class unit_value.

Where the custom code might look like...

53.5.cm_to_feet

... you can do exactly the same thing with supported core functionality...

unit_value.new(53.5,:cm).value_in(:feet)


In the same way that each Magik reference to coordinate should really have a corresponding coordinate_system (another pet peeve of mine), most numbers in Magik should ideally be represented as unit_value objects in order to avoid these customized unit conversion methods.

No comments: