一切福田,不離方寸,從心而覓,感無不通。

Naming cheatsheet

Naming things is hard. This sheet attempts to make it easier.

Although these suggestions can be applied to any programming language, I will use JavaScript to illustrate them in practice.

English language

Use English language when naming your variables and functions.


 

Like it or not, English is the dominant language in programming: the syntax of all programming languages is written in English, as well as countless documentations and educational materials. By writing your code in English you dramatically increase its cohesiveness.

Naming convention

Pick one naming convention and follow it. It may be camelCase, or snake_case, or anyhow else, it does not matter. What matters is for it to remain consistent.


 

S-I-D

A name must be shortintuitive and descriptive:

  • Short. A name must not take long to type and, therefore, remember;
  • Intuitive. A name must read naturally, as close to the common speech as possible;
  • Descriptive. A name must reflect what it does/possesses in the most efficient way.

 

Avoid contractions

Do not use contractions. They contribute to nothing but decreased readability of the code. Finding a short, descriptive name may be hard, but contraction is not an excuse for not doing so.


 

Avoid context duplication

A name should not duplicate the context in which it is defined. Always remove the context from a name if that doesn’t decrease its readability.


 

Reflect the expected result

A name should reflect the expected result.


 


Naming functions

A/HC/LC Pattern

There is a useful pattern to follow when naming functions:

 

Take a look at how this pattern may be applied in the table below.

Name Prefix Action (A) High context (HC) Low context (LC)
getPost get Post
getPostData get Post Data
handleClickOutside handle Click Outside
shouldDisplayMessage should Display Message

Note: The order of context affects the meaning of a variable. For example, shouldUpdateComponent means you are about to update a component, while shouldComponentUpdate tells you that component will update on itself, and you are but controlling when it should be updated. In other words, high context emphasizes the meaning of a variable.


Actions

The verb part of your function name. The most important part responsible for describing what the function does.

get

Accesses data immediately (i.e. shorthand getter of internal data).


 

See also compose.

set

Sets a variable in a declarative way, with value A to value B.


 

reset

Sets a variable back to its initial value or state.


 

fetch

Request for some data, which takes some indeterminate time (i.e. async request).


 

remove

Removes something from somewhere.

For example, if you have a collection of selected filters on a search page, removing one of them from the collection is removeFilternot deleteFilter (and this is how you would naturally say it in English as well):


 

See also delete.

delete

Completely erases something from the realms of existence.

Imagine you are a content editor, and there is that notorious post you wish to get rid of. Once you clicked a shiny "Delete post" button, the CMS performed a deletePost action, not removePost.


 

See also remove.

compose

Creates new data from the existing one. Mostly applicable to strings, objects, or functions.


 

See also get.

handle

Handles an action. Often used when naming a callback method.


 


Context

A domain that a function operates on.

A function is often an action on something. It is important to state what is its operable domain, or at least an expected data type.


 

Some language-specific assumptions may allow omitting the context. For example, in JavaScript, it’s common that filter operates on Array. Adding explicit filterArray would be unnecessary.

Prefixes

Prefix enhances the meaning of a variable. It is rarely used in function names.

is

Describes a characteristic or state of the current context (usually boolean).


 

has

Describes whether the current context possesses a certain value or state (usually boolean).


 

should

Reflects a positive conditional statement (usually boolean) coupled with a certain action.


 

min/max

Represents a minimum or maximum value. Used when describing boundaries or limits.


 

prev/next

Indicate the previous or the next state of a variable in the current context. Used when describing state transitions.


 

Singular and Plurals

Like a prefix, variable names can be made singular or plural depending on whether they hold a single value or multiple values.


 

from:https://github.com/kettanaito/naming-cheatsheet