2017-06-30 10:24:03 +02:00
# Naming cheatsheet
Naming things is hard. Is it?
## Guidelines
2017-06-30 10:26:05 +02:00
* Pick **one** naming convention and follow it. Whether it is `likeThis` , or `like_this` , or anyhow else, it does not matter. What matters is consistency in your work.
* Name, whether of a variable, method, or something else, should be *short* , *descriptive* and *intuitive* :
2017-06-30 10:24:03 +02:00
* **Short**. Variable should not take long to type, and therefore to remember,
* **Descriptive**. Name of the variable should reflect what this variable possesses/does in the most efficient way,
* **Intuitive**. Name of the variable should read naturally, as close to common speach as possible
```js
2017-06-30 10:49:04 +02:00
/* Bad */
2017-06-30 10:24:03 +02:00
const a = 5; // "a" could mean anything
2017-06-30 10:26:05 +02:00
const isPaginatable = (a > 10); // "Paginatable" sounds extremely unnatural
2017-06-30 10:24:03 +02:00
2017-06-30 10:49:04 +02:00
/* Good */
2017-06-30 10:24:03 +02:00
const postsCount = 5;
const shouldDisplayPagination = (postsCount > 10);
```
2017-06-30 10:29:50 +02:00
* Name should not duplicate the context when the latter is known, and when removing the context from the name does not decrease its readability:
2017-06-30 10:24:03 +02:00
```js
class MenuItem {
/* Method name duplicates the context it is in "...MenuItem..." */
handleMenuItemClick = (event) => { ... }
/* This way it reads as MenuItem.handleClick() */
handleClick = (event) => { ... }
}
```
2017-06-30 10:49:04 +02:00
* Name should reflect expected result:
```js
/* Bad */
const isEnabled = this.props.enabled;
return (< Button disabled = {!isEnabled} / > );
/* Good */
const isDisabled = this.props.disabled;
return (< Button disabled = {isDisabled} / > );
```
2017-06-30 10:24:03 +02:00
2017-06-30 11:53:09 +02:00
## Pattern
2017-06-30 10:24:03 +02:00
```
2017-06-30 10:40:36 +02:00
prefix? + action (A) + high context (HC) + low context (LC)
2017-06-30 10:24:03 +02:00
```
2017-06-30 12:29:56 +02:00
This is not a rule, but rather a pattern which can be applied quite often when naming variables.
> Keep in mind, that order of the contexts affects the core 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 whether it should.
In other words, high context emphasizes the meaning of the variable.
2017-06-30 11:53:09 +02:00
### Example
2017-06-30 10:40:36 +02:00
| Name | Prefix | Action | High context | Low context |
| ---- | ---- | ------ | ------------ | ----------- |
| `getPost` | | `get` | `Post` | |
| `getPostData` | | `get` | `Post` | `Data` |
| `handleClickOutside` | | `handle` | `Click` | `Outside` |
2017-06-30 10:55:43 +02:00
| `shouldDisplayMessage` | `should` | `Display` | `Message` | |
2017-06-30 10:24:03 +02:00
2017-06-30 11:53:09 +02:00
## Naming methods
### Action
2017-06-30 10:24:03 +02:00
#### `get`
Access data immediately (i.e. shorthand getter of internal data).
```js
function getFruitsCount() {
return this.fruits;
}
```
#### `fetch`
2017-06-30 10:40:36 +02:00
Request for data, which takes time (i.e. async request).
2017-06-30 10:24:03 +02:00
```js
function fetchPosts(postCount) {
return fetch('https://api.dev/posts', { ... });
}
```
#### `set`
2017-06-30 10:40:36 +02:00
Declaratively set `variableA` with `valueA` to `valueB` .
2017-06-30 10:24:03 +02:00
```js
function Component() {
this.state = { fruits: 0 };
function setFruits(nextFruits) {
this.state.fruits = nextFruits;
}
}
```
#### `reset`
2017-06-30 10:40:36 +02:00
Set something back to its initial value.
2017-06-30 10:24:03 +02:00
```js
const initialFruits = 5;
const fruits = initialFruits;
setFruits(10); // fruits = 10
function resetFruits() {
fruits = initialFruits;
}
resetFruits(); // fruits = 5
```
#### `compose`
2017-06-30 10:40:36 +02:00
Create new data from the existing one. Probably, applicable mostly to strings.
2017-06-30 10:24:03 +02:00
```js
function composePageUrl(pageName, pageId) {
return `${pageName.toLowerCase()}-${pageId}` ;
}
```
#### `handle`
Handler for a dedicated action. Usually, used as a callback method.
```js
function handleLinkClick(event) {
event.preventDefault();
console.log('Clicked a link!');
}
link.addEventListener('click', handleLinkClick);
```
2017-06-30 11:53:09 +02:00
## Prefixes
2017-06-30 12:07:57 +02:00
Prefixes enhance variables or methods, indicating additional meaning behind them.
2017-06-30 12:07:17 +02:00
2017-06-30 11:53:09 +02:00
#### `is`
2017-06-30 12:07:17 +02:00
Describes certain characteristic or state of the context.
2017-06-30 11:53:09 +02:00
```js
const color = 'blue';
2017-06-30 12:07:17 +02:00
const isBlue = (color === 'blue'); // characteristic
const isRemoved = false; // state
2017-06-30 11:53:09 +02:00
2017-06-30 12:07:17 +02:00
if (isBlue & & !isRemoved) {
console.log('The color is blue and it is present!');
2017-06-30 11:53:09 +02:00
}
```
#### `min`/`max`
Represent minimum or maximum value. Usually describe allowed limits.
```js
function PostsList() {
this.minPosts = 3;
this.maxPosts = 10;
}
```
2017-06-30 12:07:17 +02:00
#### `has`
Describes whether current context possesses a certain value or state.
```js
/* Bad */
const isProductsExist = (productsCount > 0);
const areProductsPresent = (productsCount > 0);
/* Good */
const hasProducts = (productsCount > 0);
```
2017-06-30 10:24:03 +02:00
#### `should`
2017-06-30 11:53:09 +02:00
Reflects conditional statement (returns `Boolean` value).
2017-06-30 10:24:03 +02:00
```js
const currentUrl = 'https://dev.com';
function shouldUpdateUrl(url) {
return (url !== currentUrl);
}
```