[llvm-dev] Variable names rule
Michael Platings via llvm-dev
llvm-dev at lists.llvm.org
Fri Feb 1 06:20:31 PST 2019
As application of the naming rules are currently under discussion  this seems like a good time to bring this up:
The current variable naming rule  states: Variable names should be nouns (as they represent state). The name should be camel case, and start with an upper case letter (e.g. Leader or Boats).
I'm a relatively new arrival to the LLVM codebase and I want to follow the rules wherever I can, but I humbly submit that this rule is suboptimal for readable code.
The rationale given at the time this rule was added was "document the prevailing convention" . It was debated after the policy change whether this was the right choice .
The main problem I find with this rule is that it is the same as the type naming rule. Why is this a problem? It is famously hard to name things ("There are 2 hard problems in computer science: cache invalidation, naming things, and off-by-1 errors.") and with this rule if you've already used a name for a type, you can't reuse it for a variable of that type.
So what do you do instead? Often it seems the answer is to use an acronym (Target T), which hurts readability, or prepend "The" to the type name (Target TheTarget), which wastes space and also hurts readability because the start and end of a word are the most important for readnig. 
So we've got declarations like "LB(L, PSE, LI, DT, TLI, TTI, AC, ORE, VF.Width, IC, &LVL, &CM)"  which is quite intimidating to newcomers.
If we could also use snake_case variable names then straight away you have an obvious, readable name (Target target). It looks like there's plenty of code that does this already, and it's consistent with our Python code. Also, it makes C idioms like "for (int i = 0; ..." permissible.
I realize that there's also a rule  "Avoid abbreviations unless they are well known" - I believe allowing a trivial way to get readable names would help greatly to adhere to this.
What do you think? Could we relax the variable naming rule?
 Mostly based on my own experience, but somewhat supported by evidence: https://en.wikipedia.org/wiki/Transposed_letter_effect
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the llvm-dev