From: Robert Klemme Date: 2012-04-21T03:35:29+09:00 Subject: Re: When are numbers NOT Magic? Example. On Fri, Apr 20, 2012 at 6:39 PM, Greg Willits wrote: > Robert Klemme wrote in post #1057330: >> But a constant's name goes a long way in documenting the meaning. > > Generically, I agree that's a good purpose for a constant, but in this > case, it seems to me that > > <%= float_as_thousands_with_precision(watts, PRECISION_OF_WATTS) -%> > isn't any clearer or better documented than > <%= float_as_thousands_with_precision(watts, 2) -%> Probably, yes. :-) > I think that's the crux of what I see. To me, the latter can't really be > improved upon. It can be different, but is the extra code really adding > value? Your FORMATS idea is interesting (and useful for a repeatedly > used value), but in the end isn't it a variation on a list of constants? I prefer to look at it differently: I have introduced typed values. That extra information about the data can be used elsewhere to properly present a datum. It can also be used for other things, e.g. flagging errors if someone tries to compare apples and oranges, or do argument checks in methods that are designed for a particular type of data only etc. That separates type logic from presentation logic while improving type safety by attaching the type info at the location which creates the data. If your data would consist of two values (e.g. a coordinate) it would be much more obvious to create a specific class for it but you can do so for single values also. Another approach would be to create several classes and use different logic for the calculation of the precision (i.e. look up via obj.class). > Anyway, thanks for your comments. I appreciate your taking the time to > noodle on my epistle. Don't call me "Noodles" - that name is taken already. :-) http://akas.imdb.com/title/tt0087843/combined Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/