5 ms·
To this I would like to draw attention to the mention of context in this example, which I think is important and often looked over when naming variables. The c
by jmts 10y ago
To this I would like to draw attention to the mention of context in this example, which I think is important and often looked over when naming variables.
The context given for maxphysaddr is that of a global variable. Pike also mentions loop indices, which have very local scope. A loop variable can be very short because you know everything about it simply by reading the few lines around it. A global must encode much more information in its name, because its context is spread globally. If the name is not good, the developer has to spend extra time looking for appropriate context, and aside from the added frustration, time is money.
In some ways one could suggest that variable name length should be proportional to its scope. This isn't quite true, however, because we should then find that MaximumPhysicalAddress should provide greater value than maxphysaddr. Instead, we find that MaximumPhysicalAddress is a worse name due to its length. They are however both unambiguous and provide the same semantic information. This suggests that perhaps a rule of thumb should be that the name unambiguously encode the necessary semantic information proportional to its context, and no more.
Naming is however hard, in general. It seems picking good examples is equally difficult.
- dwringer 10y agoThat's exactly my point. I did not offer MaximumPhysicalAddress originally as an alternative, because as you said it is not really any less ambiguous in this case. However, a new programmer approaching the codebase still has to remember WHICH words are abbreviated and which variables rely on which abbreviations. Since MaxPhysAddr is already 11 characters long, and I can type MaximumPhysicalAddress in almost exactly the same amount of time (the overhead is essentially negligible) I see no benefit to using the abbreviated form. One must remember that maxphysaddr actually carries MORE information than MaximumPhysicalAddress, specifying that the words are all abbreviated. One can argue that the abbreviations used are conventional, but if the convention is not already known then the information required is even greater. That information is not relevant to the variable's function, and thus adds an unnecessary burden to the programmer. IMHO. But, again, it is not a good example, and I do not have a suggestion for an alternative.