Style for physics units in variable names?

3k views Asked by At

Are there any Hungarian-in-spirit style guides for appending (SI or Imperial) units to variable names for physics-intensive C/C++? I'd expected that this wheel had already been invented, but online I found only simple examples like duration_sec and length_ft. (No boost, please.) (Roughly related: naming of physical quantities in python .)

Typeset prose has a good style guide, but its superscripts, slashes and italics fail in programming languages.

I'd even be happy with arguments resolving particular issues that arise with compound units like spectral radiance, moment, torque, or energy:

  • plural or not (lbs, lb)
  • capital or not (Lb, lb)
  • reciprocals (PerSec, pSec, SecInv)
  • superscripts like cubed or inverse squared
1

There are 1 answers

2
Kevin On BEST ANSWER

Ultimately, what's important is picking a style and sticking with it.

If I were implementing this, I'd probably start with SI abbreviations:

int distance_m; // meters

For exponents, append the exponent to the unit:

int area_m2; // square meters

For products, split with an underscore:

int torque_N_m; // Newton-meters

(the use of capital N here may or may not be a good idea; be consistent)

For quotients, write per:

int velocity_m_per_s; // meters per second

You should only need one per per variable. Assume it has a lower precedence than multiplication.

The above is just a suggestion, of course; you should modify it to suit your needs. Just make sure the whole development team agrees on the rules.