8

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
Community
  • 1
  • 1
Camille Goudeseune
  • 2,934
  • 2
  • 35
  • 56
  • The easy things to name (as far as units go) as m, kg, Pa, etc. However some compound units get tricky like density (kg/m^3), acceleration (m/s^2), etc. The fractions and exponents are difficult to name well. – Cory Kramer Nov 19 '14 at 17:52
  • Exactly. The compound units are what I expect *someone* has given serious thought to. – Camille Goudeseune Nov 19 '14 at 17:54
  • 1
    One of the listed disadvantages of hungarian notation is listed on wikipedia as "Hungarian Notation becomes confusing when it is used to represent several properties, as in a_crszkvc30LastNameCol: a constant reference argument, holding the contents of a database column LastName of type varchar(30) which is part of the table's primary key." I think no matter what you do, compound units fit that level...you're going to end up appending tens of characters to your variable name. – IdeaHat Nov 19 '14 at 18:00
  • That's fine. My monitor is 1920 pixels wide :) – Camille Goudeseune Nov 19 '14 at 18:04
  • 2
    Use a typed units library then you don't need a hungarian-style notation, the units are part of the type. There's an [example](http://www.boost.org/doc/libs/1_57_0/doc/html/ratio/users_guide.html#ratio.users_guide.Examples.si_units) in the Boost docs that can be adapted to use `std::ratio` easily. – Jonathan Wakely Nov 19 '14 at 18:28
  • 2
    Wouldn't it be better to name the quantity you are measuring than its units? You can comment what units you are using, and if they differ from SI or Imperial conventions. So instead of `length_m` you use `pipe_length`. – Weather Vane Nov 19 '14 at 18:34
  • mPipeLength is a bit more Hungarian. – Weather Vane Nov 19 '14 at 22:50
  • Amazingly, OP wants to consider lbs and ft, despite the SI system being in existence for half a century, despite causing a megabucks spacecraft to crashland, and despite gallons still being a local measure. If you have archaic units input as data - convert/ dump them at the point of entry. Anyone got any spare groats? – Weather Vane Nov 19 '14 at 22:55
  • The SO jury seems to have decided that an answer can be only "opinion-based"; in other words, they have not found a style guide either. If so, then the OP's question has a direct answer: no. – Camille Goudeseune Nov 20 '14 at 04:25
  • 1
    Archaic or SI is a side issue. The units' names don't materially affect the question. – Camille Goudeseune Nov 20 '14 at 04:29

1 Answers1

5

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.

Kevin
  • 28,963
  • 9
  • 62
  • 81
  • 1
    No more than one "per"? Energy transfer rate per unit area = energy ÷ time ÷ area. `joules_sec_metre_2`? – Weather Vane Nov 19 '14 at 18:35
  • 1
    @WeatherVane (e/t)/a = (e/t) * (1/a) = e/at, so using this answer's convention that would be `joule_per_m2_s`, or `watt_per_m2` which is pretty sensible. Any other nested fractions can be flattened in the same way, though perhaps not always with such a nice result. But (some kinds of) engineers routinely juggle lots of units in calculations, it's manageable. –  Nov 19 '14 at 19:03