Expressive Names

Version 1.1 by chrisby on 2023/11/17 21:51

Choose meaningful names. * Names should be chosen as carefully as the name of his firstborn child. * Implicity: It should be self-evident from reading the code how it works. *

Choose names that are descriptive of the purpose. * For example, a variables name should stand for one concept. Its better to have a variable unorderedNumbers, which is sorted and stored in orderedNumbers instead of saving both lists in the same variable numbers. *

Avoid misinformation. * For example, ambiguities, confusion with similar names or easily confused characters (l and 1, O and 0). *

Make differences clear. * Avoid very similar expressions. * Blank words are redundant (a, an, the, info, data).

  • Use pronounceable names. Programming is a social activity that people talk about with others.

Use searchable names. * The length of a name should correspond to the size of its scope. For local counting loops, one letter is enough; if the variable is used in multiple places in the code, it needs a longer name. *

Avoid encodings. * There should be no references to the scope or type of the variable in the name. *

Avoid mental mappings. * The name of a variable should not have to be mentally translated into another. Clarity has absolute priority. *

Class names * Names of classes consist of nouns or substantivistic expressions. *

Method names * They 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). * Overloaded constructors can lead to confusion, e.g. if a float is to be passed once and an int once. Constructors should be declared as private and functions should be used to create instances whose names highlight the difference.

  • Avoid humorous names.

Choose one word per concept. * "get" instead of "fetch" and "retrieve".

  • No puns.
  • Avoid ambiguities as in the word "add" (addition or adding).

Use names of the solution domain. * Programmers will read your code, so use technical language. *

Use names of the problem domain. * If there are no terms from computer science. Then at least domain experts can refer to it. *

Add meaningful context. * Together with the names of other variables and methods, this context can be created. *

Do not add superfluous context. * Shorter names are better than longer ones, as long as they are clear. Names should be simple, but meaningful.

  • Dare to rename things. Your colleagues should be grateful for improvements.