naming-cheatsheet/README.md

245 lines
6.4 KiB
Markdown
Raw Normal View History

2017-08-25 16:10:23 +02:00
<p align="center">
2018-01-12 09:59:02 +01:00
<a href="https://github.com/kettanaito/naming-cheatsheet">
<img src="./naming-cheatsheet.png" alt="Naming cheatsheet" />
</a>
2017-08-25 16:10:23 +02:00
</p>
2017-06-30 10:24:03 +02:00
# Naming cheatsheet
2017-08-15 11:41:34 +02:00
Naming things is hard. Let's make it easier.
2017-06-30 10:24:03 +02:00
## 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.
2018-01-12 09:59:02 +01:00
2017-06-30 12:37:41 +02:00
```js
/* Bad */
const pages_count = 5;
const shouldUpdate = true;
/* Good */
const pagesCount = 5;
const shouldUpdate = true;
/* Good as well */
const pages_count = 5;
const should_update = true;
```
2018-01-12 09:59:02 +01:00
2017-06-30 10:26:05 +02:00
* Name, whether of a variable, method, or something else, should be *short*, *descriptive* and *intuitive*:
2018-01-12 09:59:02 +01:00
* **Short**. Variable should not take long to type and, therefore, to remember,
* **Descriptive**. Name of the variable should reflect what it does/possesses in the most efficient way,
* **Intuitive**. Name of the variable should read naturally, as close to the common speach as possible
2017-06-30 10:24:03 +02:00
```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 12:37:41 +02:00
const isPaginatable = (postsCount > 10); // "Paginatable" sounds extremely unnatural
const shouldPaginatize = (postsCount > 10); // Made up verbs are so much fun!
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 12:37:41 +02:00
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:
2018-01-12 09:59:02 +01:00
2017-06-30 10:24:03 +02:00
```js
class MenuItem {
2017-12-20 11:31:12 +01:00
/* Method name duplicates the context it is in (which is "MenuItem") */
2017-06-30 10:24:03 +02:00
handleMenuItemClick = (event) => { ... }
/* This way it reads as MenuItem.handleClick() */
handleClick = (event) => { ... }
}
```
2017-12-20 11:31:12 +01:00
* Name should reflect the expected result:
2018-01-12 09:59:02 +01:00
2017-06-30 10:49:04 +02:00
```js
/* Bad */
2017-07-01 11:28:55 +02:00
const isEnabled = (itemsCount > 3);
2017-06-30 10:49:04 +02:00
return (<Button disabled={!isEnabled} />);
/* Good */
2017-07-01 11:28:55 +02:00
const isDisabled = (itemsCount <= 3);
2017-06-30 10:49:04 +02:00
return (<Button disabled={isDisabled} />);
```
2017-06-30 10:24:03 +02:00
2018-01-12 09:59:02 +01:00
## HC/LC Pattern
There is a useful pattern you may follow when naming your methods:
2017-06-30 10:24:03 +02:00
```
2017-12-20 11:31:12 +01: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
2018-01-12 09:59:02 +01:00
To illustrate, take a look at how this pattern may be applied in the table below.
2017-06-30 12:29:56 +02:00
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
2018-01-12 09:59:02 +01:00
> **Note:** The order of the contexts affects the core meaning of a method. 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 do that right now.
In other words, **high context emphasizes the meaning of the variable**.
2017-06-30 11:53:09 +02:00
2017-12-20 11:31:12 +01:00
## Actions
2018-01-12 09:59:02 +01:00
Chosing a proper action name may grant explicit descriptiveness to your methods. This is a good place to start when naming your methods.
2017-06-30 13:46:30 +02:00
2017-06-30 10:24:03 +02:00
#### `get`
2017-12-20 11:31:12 +01:00
Accesses data immediately (i.e. shorthand getter of internal data).
2017-06-30 10:24:03 +02:00
```js
function getFruitsCount() {
2017-08-15 11:41:34 +02:00
return this.fruits.length;
2017-06-30 10:24:03 +02:00
}
```
#### `fetch`
2017-12-20 11:31:12 +01:00
Requests for a 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', { ... });
}
```
2017-06-30 13:46:30 +02:00
2017-06-30 10:24:03 +02:00
#### `set`
2017-12-20 11:31:12 +01:00
Declaratively sets a variable with `valueA` to `valueB`.
2018-01-12 09:59:02 +01:00
2017-06-30 10:24:03 +02:00
```js
2017-06-30 14:02:58 +02:00
const fruits = 0;
2017-06-30 10:24:03 +02:00
2017-06-30 14:02:58 +02:00
function setFruits(nextFruits) {
fruits = nextFruits;
2017-06-30 10:24:03 +02:00
}
2017-06-30 14:02:58 +02:00
2018-01-12 09:59:02 +01:00
setFruits(5);
console.log(fruits) // 5
2017-06-30 10:24:03 +02:00
```
2017-06-30 13:46:30 +02:00
2017-06-30 10:24:03 +02:00
#### `reset`
2018-01-12 09:59:02 +01:00
Sets something back to its initial value or state.
2017-06-30 10:24:03 +02:00
```js
const initialFruits = 5;
const fruits = initialFruits;
2018-01-12 09:59:02 +01:00
setFruits(10);
console.log(fruits); // 10
2017-06-30 10:24:03 +02:00
function resetFruits() {
fruits = initialFruits;
}
2018-01-12 09:59:02 +01:00
resetFruits();
console.log(fruits); // 5
2017-06-30 10:24:03 +02:00
```
2017-06-30 13:46:30 +02:00
#### `remove`
2018-01-12 09:59:02 +01:00
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 `removeFilter`, **not** `deleteFilter` (and this is how you would naturally say it in English as well):
2017-06-30 13:46:30 +02:00
```js
const selectedFilters = ['price', 'availability', 'size'];
function removeFilter(filterName) {
const filterIndex = selectedFilters.indexOf(filterName);
2018-01-12 09:59:02 +01:00
if (filterIndex !== -1) {
selectedFilters.splice(filterIndex, 1);
}
2017-06-30 13:46:30 +02:00
return selectedFilters;
}
```
#### `delete`
2018-01-12 09:59:02 +01:00
Completely erazes something from the realms of existance.
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`.
2017-12-20 11:31:12 +01:00
```js
function deletePost(id) {
2018-01-12 09:59:02 +01:00
return database.find({ id }).delete();
2017-12-20 11:31:12 +01:00
}
```
2017-06-30 13:46:30 +02:00
2017-06-30 10:24:03 +02:00
#### `compose`
2017-12-20 11:31:12 +01:00
Creates a new data from the existing one. Applicable mostly to strings or objects.
2018-01-12 09:59:02 +01:00
2017-06-30 10:24:03 +02:00
```js
function composePageUrl(pageName, pageId) {
return `${pageName.toLowerCase()}-${pageId}`;
}
```
#### `handle`
2017-12-20 11:31:12 +01:00
Handles a dedicated action. Often used in naming the callback methods.
2018-01-12 09:59:02 +01:00
2017-06-30 10:24:03 +02:00
```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-12-20 11:31:12 +01:00
Prefixes enhance variables and methods, indicating an additional meaning behind them.
2017-06-30 12:07:17 +02:00
2017-06-30 11:53:09 +02:00
#### `is`
2017-12-20 11:31:12 +01:00
Describes certain characteristic or state of the current context (returns `Boolean`).
2018-01-12 09:59:02 +01:00
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
2017-12-20 11:31:12 +01:00
const isPresent = true; // state
2017-06-30 11:53:09 +02:00
2017-12-20 11:31:12 +01:00
if (isBlue && isPresent) {
2017-06-30 12:07:17 +02:00
console.log('The color is blue and it is present!');
2017-06-30 11:53:09 +02:00
}
```
#### `min`/`max`
2017-08-15 11:41:34 +02:00
Represent minimum or maximum value. Handy when describing boundaries or allowed limits.
2018-01-12 09:59:02 +01:00
2017-06-30 11:53:09 +02:00
```js
function PostsList() {
this.minPosts = 3;
this.maxPosts = 10;
}
```
2017-06-30 12:07:17 +02:00
#### `has`
2017-12-20 11:31:12 +01:00
Describes whether the current context possesses a certain value or state.
2018-01-12 09:59:02 +01:00
2017-06-30 12:07:17 +02:00
```js
/* Bad */
const isProductsExist = (productsCount > 0);
const areProductsPresent = (productsCount > 0);
/* Good */
const hasProducts = (productsCount > 0);
```
2017-06-30 10:24:03 +02:00
#### `should`
2018-01-12 09:59:02 +01:00
Reflects a positive conditional statement (returns `Boolean`) tightly coupled with a certain action.
2017-06-30 10:24:03 +02:00
```js
const currentUrl = 'https://dev.com';
function shouldUpdateUrl(url) {
return (url !== currentUrl);
}
```
2017-12-20 11:31:12 +01:00
#### `prev`/`next`
2018-01-12 09:59:02 +01:00
Indicate the previous and the next state of the variable in the current context. Useful for describing a state mutation.
2017-12-20 11:31:12 +01:00
```jsx
function fetchPosts() {
2018-01-12 09:59:02 +01:00
const prevPosts = this.state.posts;
2017-12-20 11:31:12 +01:00
2018-01-12 09:59:02 +01:00
const fetchedPosts = fetch('...');
const nextPosts = prevPosts.merge(fetchedPosts);
2017-12-20 11:31:12 +01:00
2018-01-12 09:59:02 +01:00
return this.setState({ posts: nextPosts });
2017-12-20 11:31:12 +01:00
}
```