Wiki source code of Expressive Names
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | Names should be chosen as carefully as one would name their firstborn child. | ||
2 | |||
3 | **Naming Conventions** | ||
4 | |||
5 | * **Choose Meaningful and Descriptive Names** | ||
6 | * Names should be descriptive of the purpose, e.g., use `unorderedNumbers` and `orderedNumbers` instead of a generic `numbers`. | ||
7 | * Use names that reflect the solution domain and the problem domain. | ||
8 | * Choose one word per concept (e.g. consistently use 'fetch' for the same concept, rather than mixing 'fetch' and 'retrieve'). | ||
9 | * **Clarity and Simplicity in Names** | ||
10 | * Names should be self-evident and avoid mental mappings or translations for clarity. | ||
11 | * Avoid ambiguities, such as confusion with similar names or characters (e.g., l and 1, O and 0). | ||
12 | * Make differences clear and avoid very similar expressions. | ||
13 | * Avoid superfluous or redundant context; shorter names are better as long as they are clear. | ||
14 | * **Practical Aspects of Naming** | ||
15 | * Use pronounceable names so that you can easily talk with others about it. | ||
16 | * Use searchable names, meaning names which prevent search conflicts. | ||
17 | * 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. | ||
18 | * Avoid encodings, such as references to the variable's scope or type in its name. | ||
19 | |||
20 | **Class and Method Naming** | ||
21 | |||
22 | * **Class Names** | ||
23 | * Should consist of nouns or noun phrases. | ||
24 | * **Method Names** | ||
25 | * Should consist of verbs or verb phrases. | ||
26 | * Follow the JavaBean standard for accessors, mutators, and predicates (prefixes: get, set, is, has). | ||
27 | * Use distinct names for overloaded constructors and prefer private constructors with distinct factory methods. | ||
28 | |||
29 | **General Coding Practices** | ||
30 | |||
31 | * **Avoiding Confusion and Misinformation** | ||
32 | * Avoid humorous names, puns, and any names that might cause confusion. | ||
33 | * **Contextual Naming** | ||
34 | * Add meaningful context that complements other variable and method names. | ||
35 | * Dare to rename things for clarity and improvement; colleagues should appreciate these enhancements. |