When You Should Have a Naming Convention
We need some naming conventions so that everyone working on the project are on the same page.
Also, we run a program over to another person for changes, they’ll also be on the same page.
It also makes reviewing things easier as we don’t have to find out what something means.
When a program is so big that we can’t hold them in brains, then having conventions reduce the cognitive load of our brains.
Informal Naming Conventions
Naming conventions vary between languages, but there’re some that apply to language.
Variable names and functions names are both camelCase except for classes constructor functions, which are named in PascalCases.
Objects are in camelCase.
To make identifying global variables easier, we can put a
g prefix before the global variable.
But we may put a
_ before the member variable of a class to distinguish them from other variables.
Index variables are named with short names like
Constants are named with all caps with underscores in between words.
set are used for accessor methods.
Property names of objects are also named with camel case.
Parameters of functions are also camel case as with variables.
We may want to prefix our variables names to make identifying them easier.
To do that, we may add semantic prefixes to some variables to make them easier to understand.
For instance, we may have
c for count,
first for the first element of an array,
g for a global variable,
i for array index, and
last for the last element of an array.
m prefix may be used for class instance variables.
max for the maximum of a set of numbers,
min is the minimum number from an array.
Advantages of Standardized Prefixes
Standardized prefixes give us all the advantages of standard naming conventions,
We don’t have to dig deep to find what something means.
The distinctions between prefixes like
max are helpful.
Creating Short Names That Are Readable
If we abbreviate variable names, then we should make sure that they’re abbreviations that everyone understands.
There are many ways to abbreviate variable names.
We can use standard abbreviations.
Nonleading vowels should be removed from abbreviations. For instance,
the , etc. should be removed.
The first letter or first few letters should be used.
Truncating words consistently may also be used for naming variables.
Using the first and last letters of each word is another possibility.
Every significant word in the name can also be used.
Useless suffixes can also be removed.
These are all possible ways that we can abbreviate variable names.
We can also use phonetic abbreviations. However, it’s not very clear so it’s probably a bad idea to use them.
Good Practices When Abbreviating Variable Names
We shouldn’t abbreviate by removing one character from a word.
Since it’s only one character shorter, it doesn’t help us too much.
Abbreviating consistent is also a good idea. We should keep some conventions to remove our cognitive load.
Creating names that we can pronounce is also good.
Combinations that may result in misreading or mispronunciation also isn’t good. So
sEnd is better than
send , for example.
A thesaurus can help us resolve naming collisions if we do run into them.
If we have short names, we may want to document them so that people can understand what those names stand for.
A project-level document for names that people might not understand is also a good idea to have.
To make sure that everyone understands the names that we name, we got to name them clearly.
If we abbreviate, then we got to stick to a convention for abbreviation and keep a list of abbreviations if needed.