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-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.