Naming variables has always been a source of debates around developers. The general idea is that you should pick a name as short as possible, but long enough to provide meaning, one which would be self-explanatory, for which you needn’t browse the code to understand what it’s used for.
Studying naming conventions can help. Also, having some specially set within your team is a good idea. Here are some that are quite general:
- For variables, you should
use nouns. Other ideas:
- with a plural form for arrays;
- if your variable is a boolean, you should prefix it with
- For functions, you should
use verbs, with a recommended format of
- Avoid using
myin the names you pick (e.g.
myClearFunctionName) and other keywords like
- Do not combine case styles (e.g. use
- Go from general to specific – e.g.
nameis the general part,
Lastare the specific parts);
- Constants should be written in
Apart from these rather general rules, I found it easier and productive to keep my variables on the positive side (e.g. use
isValid instead of
isInvalid) throughout the code of a project.
It is hard to apply all of them right from the beginning, but with time and practice, everybody gets better and better.
A source for more best practices is
More information and sources: