Changes for page Expressive Names
Last modified by chrisby on 2023/11/18 17:45
Summary
-
Page properties (1 modified, 0 added, 0 removed)
Details
- Page properties
-
- Content
-
... ... @@ -1,21 +1,21 @@ 1 1 Names should be chosen as carefully as the name of your first-born child. It should be obvious from reading the code how it works. 2 2 3 -* **Names describe purpose.** For example, it's better to have a variable `unorderedNumbers` which is sorted and thenstored in `orderedNumbers` than tohave a variable`numbers` towhichthelistsareassignedbeforeand afterorting.4 -* **Avoid misinformation.** For example, ambiguity, confusion with similar names, or easily confused characters (l and 1, O and 0).5 -* **Make differences clear.**Avoid very similar expressions andredundant empty words (a, an, the, info, data).6 -* **Use pronounceable names.** Programming is a social activity that people talk about with others, so use names that are easy to use in conversation.7 -* **Use searchable names.**Searchable means avoiding search conflicts with other independent things with the same name. The length of a name should match the size of its scope. For local counting loops, one letter is sufficient; if the variable is used in multiple places in the code, it needs a longer name.8 -* **Avoid encodings.**There should be no references to the scope or type of the variable in the name.9 -* **Avoid mental mappings.** The name of a variable should not require mental effort to understand. For example, unusual abbreviations should be avoided.10 -* **Names of classes consist of nouns**or substantivistic expressionsas they express and entity.11 -* **Method names start witha verb**becausethey expressan action tobeperformedAccessors, mutators, and predicates should be named after their value and follow thestandards like theJavaBean standard (prefixes: get, set, is, has). For example, getAge(), setAge(...), isFeatureXEnabled() -> boolean, hasPermissionY() -> boolean, etc.12 -* **Use object creation functions rather than overloaded constructors**, as the latter can cause confusion. Overloaded constructors should be declared private, andmore specifically named functions should be used to create instances.13 -* **No puns or humorous names.**14 -* **Choose one word for each concept.**For example, if you use the word "fetch" once for a particular concept, you should consistently use "fetch" instead of synonyms such as "retrieve".15 -* **Avoid ambiguitiessuchas the word "add",which could have the meaning of "addition"or"adding".16 -* **Domain-specific terms**3 +* **Names describe purpose.** For example, it's better to have a variable `unorderedNumbers` which is sorted and stored in `orderedNumbers` than to store both lists in the same variable `numbers`. 4 +* Avoid misinformation.** For example, ambiguities, confusion with similar names, or easily confused characters (l and 1, O and 0). 5 +* Avoid very similar expressions and empty words that are redundant (a, an, the, info, data). 6 +* Use pronounceable names.** Programming is a social activity that people talk about with others. 7 +* Searchable means avoiding search conflicts with other independent things with the same name. The length of a name should match the size of its scope. For local counting loops, one letter is sufficient; if the variable is used in multiple places in the code, it needs a longer name. 8 +* There should be no references to the scope or type of the variable in the name. 9 +* Avoid mental mappings.** The name of a variable should not require mental effort to understand. For example, unusual abbreviations should be avoided. 10 +* Class names consist of nouns or substantivistic expressions. 11 +* Method names consist of a verb or an expression with a verb. Accessors, mutators, and predicates should be named after their value and follow the JavaBean standard (prefixes: get, set, is, has). For example, getAge(), setAge(...), isFeatureXEnabled() -> boolean, hasPermission() -> boolean, etc. 12 +* Use object creation functions rather than overloaded constructors, as the latter can cause confusion. Overloaded constructors should be declared private, and functions should be used to create instances whose names make the difference clear. 13 +* No puns or humorous names. 14 +* For example, if you use the word "fetch" once for a particular concept, you should consistently use "fetch" instead of synonyms such as "retrieve. 15 +* Avoid ambiguity, as in the word "add" (addition or adding). 16 +* Domain-specific terms 17 17 * Use terms from the solution domain. Programmers will be reading your code, so use technical language. 18 18 * Use terms from the problem domain. If there are no computer science terms, at least domain experts can refer to them. 19 -* **Add meaningful context.**By including thenamesof other variables and methods,this context can be created tomakeitspurposeclearer than without context.Namescan be chosen to work well with others.20 -* **Do not add unnecessary context.**Shorter names are better than longer ones, as long as they are clear.21 -* **Dareto rename things.**Your colleagues should be grateful for improvements.19 +* Add meaningful context.** Along with other variables' and methods' names, context can be created. 20 +* Shorter names are better than longer ones, as long as they are clear. 21 +* Don't be afraid to rename things. Your colleagues should be grateful for improvements.