1. Avoid nonsensical names.
This one is fairly obvious. Don't call your variable "var1", "abcdef", and other silly names. Perhaps you can manage it in a small program, but as you start developing larger applications, it will become more difficult to keep track of everything.2. Avoid overly generic names.
Some might argue that this falls into the same category as my first point. However, a lot of programmers think these names are okay. They aren't completely nonsensical, but they also don't tell you anything.The "Manager" suffix is often a red flag. It usually includes lots of disparate methods. If you have a Manager class, you may need to refactor the code rather than just rename it. If you are lucky, it may just be that it's a "Repository" or "Validator" class that was poorly named. However, my experience is that a Manager class usually isn't just improperly named, but improperly designed.
The verb "check" can be vague if one isn't specifying what is being checked. Prefer the verb "validate" if you are doing data validation.
The word "data" can almost always be replaced with something better.
| Bad Names | Better Names |
|---|---|
| checkPerson | validatePerson |
| checkFile | isFileReadable, isFileWritable |
| getFileData | getFileContents, getFileAttributes |
3. Avoid abbreviations unless they are well known.
Developers want to avoid excess typing, but code is read more often than it is written and abbreviations make it harder to read. Modern IDEs have code completion, so it's rarely necessary to type out the full name to reference it again. The problem with abbreviations are that they can be used inconsistently. For example, someone might shorten "keyboard" to kb, kbd, kboard, or other names . Depending on the domain, the abbreviations could be easily confused for another term. If you are writing software to track patient appointments with doctors, does "doc" refer to the doctor or a document? Does "desc" mean descending order or a description? Additionally, not all developers speak English as their first language. They may not understand what the abbreviation means and it's not always obvious to non-native speakers how words are shortened.
| Bad Names | Better Names |
|---|---|
| attr | attribute |
| cnt | count |
| nrml | normal |
Acceptable Abbreviations
- URL, HTTP, FTP
4. Avoid "of"
This is more of a personal preference, but I find that it is better to avoid using "of" in variable names. The names without "of" tend to be shorter. I also find that those who use "of" in names do so inconsistently so it's hard to remember what variables or methods are using "of" and which aren't. It's easier just to avoid it outright.| Bad Names | Better Names |
|---|---|
| numberOfRecords | recordCount |
| dateOfBirth | birthDate |
| typeOfProduct | productType |
5. Be consistent
Names can be inconsistent for several reasons. Sometimes it's for backwards compatibility or the application changed many hands. However, I once inherited a project that only ever had one developer before me, but it still had very little consistency. For example, this application had to store addresses and mark whether it was residential or business. Across the database column name, class field, and JSON serialization, it had different names. It was either addressType, addressCode, or addressCategory. Three names for the same field! It gets worse though because the field could either be represented by a numeric ID (1 for Residential, 2 for Business) or the actual labels. It was never consistent in regard to whether those variable names were storing the ID or storing the name.
This inconsistency can also be confusing if there really are two separate fields. Maybe addressType specifies Residential or Business and addressCode specifies shipping/delivery options to that location. A lot of time is wasted trying to determine whether differently named fields are actually the same or not. If I see addressCategory, I waste more time trying to figure out whether that represents an addressType or an addressCode.