Poll #1872 Variables with units
Open to: Registered Users, detailed results viewable to: All, participants: 11
Which do you prefer:
View Answers
quantity width = 2.0*meters;
0 (0.0%)
int width_m = 2;
4 (36.4%)
int width=2; // meters
9 (81.8%)
int width=2;
3 (27.3%)
no subject
Date: 2009-12-10 01:43 pm (UTC)no subject
Date: 2009-12-10 06:52 pm (UTC)If _all_ measurements are in m through the whole system then you don't need to signpost it at all. If some are in m and some are in ft then you need some kind of notation (hungarian or otherwise) so that someone can instantly tell that:
area_m = height_ft * width_m; is wrong.
You could go for a measurement class that includes the unit, and have the checks happen internally, but that seems awfully heavyweight and potentially slow. On the other hand, unless you're doing an awful lot of computation of your measurements that might not matter.
If you were doing a CAD package then I'd say the first was the best option, for general usage the second one.
no subject
Date: 2009-12-11 10:11 am (UTC)I was just editing some code that used a small number of units (mostly period and frequency with different SI prefixes, and in one places tenths of microseconds). And altered it so each variable name included the unit; without that it seemed just a matter of time before a conversion was wrong somewhere.
Maybe what people mean is that it should never have used different units at all, but always used s or Hz, stored floating point. That could be better, and would need much less disambiguation, but having different units without disambiguation seemed the worst of both worlds.
Apparently 75% of people think that comments on variables are actually useful.
I guess that could be, "if you have one unit used throughout the program, you should document it _somewhere_".
Or, come to think of it, "use an editor which displays comments next to variable declarations when you use the variable", in which case it's a superior version of the hungarian annotation.
They probably know better than _me_ :)
You could go for a measurement class that includes the unit,
The coding on the first poll option was screwed up, but it was supposed to be boost::units, which seemed very cumbersome, but is _supposed_ to compile back to ints. I don't know if it will in practice.
no subject
Date: 2009-12-11 09:15 am (UTC)