When and When Not to Abbreviate Names
Introduction
One best practice in naming variables, methods, and classes is not to abbreviate names. This is a great practice, but should this always be done, or is there an exception to the rule? This article is going to lay out what it means to not abbreviate names.
Why is it Bad?
One of the hardest things to do in programming is to name things well. Names can be obvious; other times a lot of time has to be put into a good name. Abbreviating names makes code harder to read. They can be a result of a developer not being aware of good practices, being rushed, or just being lazy. Code is read way more than it is written. Some statistics say code is read 10+ times more than it is written. Using abbreviated names can result in harder to read and understand code.
When is it Okay to Abbreviate Names?
There are times when using abbreviated names is okay. If the abbreviated name is more common than the non-abbreviated name, the abbreviated name should be used instead. Using temporaryId isn't any better than tempId. Using the name identifier instead of id isn't any better and, in this case, probably worse.
Also, don't abbreviate a name that is already abbreviated. For example, don't name a variable tmpId when you could name it tempId.
What About Loop Counters?
Loop counters such as what you would see in a for loop are okay being a single character and are not considered abbreviated like what is outlined above.
// Java code
for (int i = 0; i < 10; i++) {
for (int j = 0; i < 10; j++) {
}
}
Loop counters are named with i. If you have nested loops, each new counter you use is named using the next letter, i, j, k, etc. This is shown in the previous example.
This is pretty standard. It doesn't hurt to name loop counters something else, but it is best to stick with the standard unless you have a good reason to deviate from it.
What About Acronyms?
Acronyms are okay to use. They are not considered abbreviating a name like what is outlined above. However, when working with acronyms, it is important you name them with the proper casing. For example, write XmlRequest and not XMLRequest. Treat acronyms like any other word in terms of proper casing.
Class Names
Class names can have a much larger scope than methods or local variables and should never be abbreviated. They can, however, contain acronyms.
Conclusion
You shouldn't abbreviate method or variable names, but this isn't to be practiced 100% of the time. There are cases where the abbreviated name is more commonly recognized and should be used instead. Not using the abbreviated name when it is more commonly recognized can cause confusion.