Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What should I name a variable that has units with a fixed point?

int herpLimitLo_psig2 = 6000; // 60.00 psig
int derpLimitLoPsigwithtwodigits;
int herpLimitHiPsigfixedtwo;
int herpLimitHi_psig_timesOneHundred;

Apparently I suck at naming things.

share|improve this question

closed as primarily opinion-based by durron597, Snowman, GlenH7, MichaelT, gnat Aug 29 '15 at 3:55

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

up vote 5 down vote accepted

Just herpLimitLo is enough.

Variable names are expensive; you can't make them too long or your code will be unreadable (after all, excessive repetition of excessively redundant information excessively causes excessive exceeding of the human brain's excessiveness processing systems if used excessively), but you do want them to be as descriptive as possible. This means that you should name them after their meaning; things like expected value range or value semantics can be expressed through other language features in almost every programming language.

For example, in C, you can use typedef; even if the variable's "real" type is still int, writing it as fixed_2 herpLimitLo suggests what it is. In a language with more sophisticated type systems, whether dynamic or static, you can define an actual type for your base-2 fixed-point numbers. Depending on the language, you can then even overload common numeric operations to work transparently with such fixed-point numbers.

Some languages will even refuse to compile your code if you do stupid things like trying to directly assign a float to a fixed_2, provided you do in fact define them as different types - no naming convention in the world can do that for you.

share|improve this answer
+1 for declaring types with domain-specific meanings. Very handy! – Frank Shearar Sep 7 '12 at 16:14
Uh, I've advocated for adding units to our naming convention. It's been a bit of a problem in the past. Of course, our previous naming non-convention had real winners like v, vlo, vlop, calc, and e. – Philip Sep 7 '12 at 16:28

what about naming the variable according to its meaning in the current context? You should avoid naming variables after their type.

The only important information you should be able to retrieve easily is the interpretation factor. In most cases, there will be only one, so you can define a constant and use it everywhere you need it (and avoid putting it in the variable names).

If you are using a lot of different precisions in your computations, you should use a specific type for fixed point computations.

At least in debug mode, this type should be able to give you the precision of a given variable, and prevent you to do stuff like adding two numbers with different precisions (at least in debug mode). And so you are also free to give your variable a short meaningful name.

example in python:

speed = FixedPoint(precShift=5, val=64) # --> 2 m/sec
duration = FixedPoint(precshift=2, val=5) # --> 2.5 sec
length = speed * duration # precShift=7, value=320
share|improve this answer
Well for this specific thing the context is that it's the low limit for the herpaderp as measured in psig with a fixed point of two. It's type is an integer. There are other high and low values for different values, and there's a variety of units and precisions. But typically precisions in a unit are all the same. So voltage is always fixed point 3. – Philip Sep 7 '12 at 16:16
if you have high and low values, then maybe you can define a structure range containing the members high and low. Same if the unit is meaniful in your app: define an enum and add it to the structure, or anything that can help you represent your problem domain. – Simon Sep 7 '12 at 16:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.