Variables
Naming
- Please name variables as descriptive nouns.
- That is, for a variable holding data related to horizontal winds, choose
horizontal_winds
or evenhoriz_winds
versuswh
. - In cases for which this may be untenable, add comments explaining non-obvious variable names.
- That is, for a variable holding data related to horizontal winds, choose
- In general, variables should use
snake_case
and be entirely lowercase, unless capitalizing is a standard convention (e.g.,T
for temperature). - We do not currently employ a stylistic distinction between different types of
variables--e.g.,
const
,static
, member variables, etc. - We do, however, ask that some convention is employed to identify member
variables of a class.
- Some commonly used techniques include distinguishing member variables
with a leading
m_
(m_var
\(\approx\) "my var"), or a trailing/leading underscore (var_
or_var
). - That said, this convention should be locally consistent within a given class, struct, or otherwise.
- Some commonly used techniques include distinguishing member variables
with a leading
- As to favoring nouns for naming, one should avoid using articles or
verbs in variable names.
- The key "exception" here would be prefixes like
is_
,has_
,can_
that are commonly used with boolean variables.
- The key "exception" here would be prefixes like
Usage
Intermediate Variables
There is a balance to be struck between introducing extra, or intermediate, variables and avoiding excess allocations/de-allocations or minimizing clutter.
- In line with the hierarchy of correct/fast/understandable, mentioned in the Style Standards Overview, it often improves matters to introduce an intermediate variable, pointer, subview, etc., rather than triply-indexing into a variable that's contained within a class-member struct.
- This principle applies to calculations as well, as there are situations in which readability is improved by breaking complex operations into multiple steps.
- Generally speaking, unless the code is highly-optimized and/or
performance-critical, usage of intermediate variables is preferred.
- Here, readability and bug-safety are the top concerns, so the clarity provided by descriptive intermediate variables and resultant shorter lines, trumps most other factors.
Descriptive Power vs. Brevity in Naming
There is obvious a crossover point at which a name becomes too descriptive or simply too long, so as to be cumbersome to use. Thus, the issue of descriptive naming should be balanced against brevity in name choice, and we give some examples and rules of thumb.
- Consider synonyms that may shorten a variable name without sacrificing clarity.
- Make use of common word truncations or abbreviations.
- E.g.,
mgr
instead ofmanager
ordev
in place ofdevice
. - However, avoid non-standard abbreviations that may only be common
parlance within a small group.
- This includes names that correspond to standard arithmetic variables
in an equation (
gamma
,y_hat
), unless the equation is included as a comment and the connection is readily apparent.
- E.g.,
- The converse of the previous is avoid making contractions that negatively
impact clarity.
- A notorious example of this is removing vowels.
- E.g.,
loop_cnt
, rather thanloop_count
provides little savings, at the expense of clarity.1
- E.g.,
- A notorious example of this is removing vowels.
-
Much like some dangerous options-trading strategies, this practice offers limited upside, yet the potential downside is unlimited. ↩