Names should be chosen as carefully as one would name their firstborn child.
Naming Conventions
- Choose Meaningful and Descriptive Names
- Names should be descriptive of the purpose, e.g., use unorderedNumbers and orderedNumbers instead of a generic numbers.
- Use names that reflect the solution domain and the problem domain.
- Choose one word per concept (e.g. consistently use 'fetch' for the same concept, rather than mixing 'fetch' and 'retrieve').
- Clarity and Simplicity in Names
- Names should be self-evident and avoid mental mappings or translations for clarity.
- Avoid ambiguities, such as confusion with similar names or characters (e.g., l and 1, O and 0).
- Make differences clear and avoid very similar expressions.
- Avoid superfluous or redundant context; shorter names are better as long as they are clear.
- Practical Aspects of Naming
- Use pronounceable names so that you can easily talk with others about it.
- Use searchable names, meaning names which prevent search conflicts.
- The length of a name should correspond to the size of its scope. E.g. counters only used locally in loops, could be short or even single letters, but names used in a broad scope should be more descriptive and longer.
- Avoid encodings, such as references to the variable's scope or type in its name.
Class and Method Naming
- Class Names
- Should consist of nouns or noun phrases.
- Method Names
- Should consist of verbs or verb phrases.
- Follow the JavaBean standard for accessors, mutators, and predicates (prefixes: get, set, is, has).
- Use distinct names for overloaded constructors and prefer private constructors with distinct factory methods.
General Coding Practices
- Avoiding Confusion and Misinformation
- Avoid humorous names, puns, and any names that might cause confusion.
- Contextual Naming
- Add meaningful context that complements other variable and method names.
- Dare to rename things for clarity and improvement; colleagues should appreciate these enhancements.