Skip to content

Commit cd8ae94

Browse files
committed
Updates README
1 parent c9a1d0a commit cd8ae94

File tree

1 file changed

+101
-103
lines changed

1 file changed

+101
-103
lines changed

README.md

Lines changed: 101 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -5,99 +5,91 @@
55
</p>
66

77
# Naming cheatsheet
8-
Naming things is hard. Let's make it easier.
9-
10-
This document contains systematized concepts and patterns often used when naming variables.
11-
12-
## Summary
13-
* [Guidelines](#guidelines)
14-
* [HC/LC Pattern](#hclc-pattern)
15-
* **[Actions](#actions)**
16-
* [get](#get)
17-
* [set](#set)
18-
* [reset](#reset)
19-
* [fetch](#fetch)
20-
* [remove](#remove)
21-
* [delete](#delete)
22-
* [compose](#compose)
23-
* [handle](#handle)
24-
* **[Prefixes](#prefixes)**
25-
* [is](#is)
26-
* [has](#has)
27-
* [should](#should)
28-
* [min/max](#minmax)
29-
* [prev/next](#prevnext)
30-
31-
## Guidelines
32-
* 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 code.
8+
Naming things is hard. This sheet attempts to make it easier.
9+
10+
Although these suggestions can be applied to any programming language, I will use JavaScript to illustrate them in practice.
11+
12+
## Naming convention
13+
Pick **one** naming convention and follow it. It may be `likeThis`, or `like_this`, or anyhow else, it does not matter. What matters is for it to remain consistent.
3314

3415
```js
3516
/* Bad */
36-
const pages_count = 5;
37-
const shouldUpdate = true;
17+
const pages_count = 5
18+
const shouldUpdate = true
3819

3920
/* Good */
40-
const pagesCount = 5;
41-
const shouldUpdate = true;
21+
const pagesCount = 5
22+
const shouldUpdate = true
4223

4324
/* Good as well */
44-
const pages_count = 5;
45-
const should_update = true;
25+
const pages_count = 5
26+
const should_update = true
4627
```
4728

48-
* Name, whether of a variable, method, or something else, should be *short*, *descriptive* and *intuitive*:
49-
* **Short**. Variable should not take long to type and, therefore, to remember,
50-
* **Descriptive**. Name of the variable should reflect what it does/possesses in the most efficient way,
51-
* **Intuitive**. Name of the variable should read naturally, as close to the common speach as possible
29+
## S-I-D
30+
31+
A name must be *short*, *intuitive* and *descriptive*:
32+
* **Short**. A name must not take long to type and, therefore, to remember,
33+
* **Intuitive**. Name must read naturally, as close to the common speach as possible
34+
* **Descriptive**. Name must reflect what it does/possesses in the most efficient way,
5235

5336
```js
5437
/* Bad */
55-
const a = 5; // "a" could mean anything
56-
const isPaginatable = (postsCount > 10); // "Paginatable" sounds extremely unnatural
57-
const shouldPaginatize = (postsCount > 10); // Made up verbs are so much fun!
38+
const a = 5 // "a" could mean anything
39+
const isPaginatable = (postsCount > 10) // "Paginatable" sounds extremely unnatural
40+
const shouldPaginatize = (postsCount > 10) // Made up verbs are so much fun!
5841

5942
/* Good */
60-
const postsCount = 5;
61-
const shouldDisplayPagination = (postsCount > 10);
43+
const postsCount = 5
44+
const shouldDisplayPagination = (postsCount > 10)
6245
```
6346

64-
* Do *not* use contractions. The latter contribute to nothing but decreased code readability. Finding a short, descriptive name may be hard, but don't think contractions help you in any way.
47+
## Avoid contractions
48+
49+
Do **not** use contractions. They contribute to nothing but decreased readability of your code. Finding a short, descriptive name may be hard, but contraction is not an excude to not doing so.
6550

6651
```js
6752
/* Bad */
68-
const onItmClck = () => {};
53+
const onItmClk = () => {}
6954

7055
/* Good */
71-
const onItemClick = () => {};
56+
const onItemClick = () => {}
7257
```
7358

74-
* Name should not duplicate the context when the latter is known, and when removing the context does not decrease the name's readability:
59+
## Avoid context duplication
60+
61+
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.
7562

7663
```js
7764
class MenuItem {
78-
/* Method name duplicates the context it is in (which is "MenuItem") */
65+
/* Method name duplicates the context (which is "MenuItem") */
7966
handleMenuItemClick = (event) => { ... }
8067

81-
/* This way it reads as MenuItem.handleClick() */
68+
/* Reads nicely as `MenuItem.handleClick()` */
8269
handleClick = (event) => { ... }
8370
}
8471
```
85-
* Name should reflect the expected result:
8672

87-
```js
73+
## Reflect expected result
74+
75+
A name should reflect the expected result.
76+
77+
```jsx
8878
/* Bad */
89-
const isEnabled = (itemsCount > 3);
90-
return (<Button disabled={!isEnabled} />);
79+
const isEnabled = (itemsCount > 3)
80+
return <Button disabled={!isEnabled} />
9181

9282
/* Good */
93-
const isDisabled = (itemsCount <= 3);
94-
return (<Button disabled={isDisabled} />);
83+
const isDisabled = (itemsCount <= 3)
84+
return <Button disabled={isDisabled} />)
9585
```
9686

9787
---
9888

99-
## HC/LC Pattern
100-
There is a useful pattern you may follow when naming your methods:
89+
# Naming functions
90+
91+
## A/HC/LC Pattern
92+
There is a useful pattern to follow when naming functions:
10193

10294
```
10395
prefix? + action (A) + high context (HC) + low context? (LC)
@@ -112,13 +104,14 @@ To illustrate, take a look at how this pattern may be applied in the table below
112104
| `handleClickOutside` | | `handle` | `Click` | `Outside` |
113105
| `shouldDisplayMessage` | `should` | `Display` | `Message`| |
114106

115-
> **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.
116-
In other words, **high context emphasizes the meaning of the variable**.
107+
> **Note:** Context order affects the 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.
108+
In other words, **high context emphasizes the meaning of a variable**.
117109

118110
---
119111

120112
## Actions
121-
Chosing a proper action name may grant explicit descriptiveness to your methods. This is a good place to start when naming your methods.
113+
114+
The verb part of your function name. The most important part responsible for describing what the function *does*.
122115

123116
#### `get`
124117
Accesses data immediately (i.e. shorthand getter of internal data).
@@ -132,38 +125,38 @@ function getFruitsCount() {
132125
Declaratively sets a variable with value `A` to value `B`.
133126

134127
```js
135-
const fruits = 0;
128+
const fruits = 0
136129

137130
function setFruits(nextFruits) {
138-
fruits = nextFruits;
131+
fruits = nextFruits
139132
}
140133

141-
setFruits(5);
134+
setFruits(5)
142135
console.log(fruits) // 5
143136
```
144137

145138
#### `reset`
146139
Sets a variable back to its initial value or state.
147140

148141
```js
149-
const initialFruits = 5;
150-
const fruits = initialFruits;
151-
setFruits(10);
152-
console.log(fruits); // 10
142+
const initialFruits = 5
143+
const fruits = initialFruits
144+
setFruits(10)
145+
console.log(fruits) // 10
153146

154147
function resetFruits() {
155-
fruits = initialFruits;
148+
fruits = initialFruits
156149
}
157150

158-
resetFruits();
159-
console.log(fruits); // 5
151+
resetFruits()
152+
console.log(fruits) // 5
160153
```
161154

162155
#### `fetch`
163156
Requests for a data, which takes time (i.e. async request).
164157
```js
165158
function fetchPosts(postCount) {
166-
return fetch('https://api.dev/posts', { ... });
159+
return fetch('https://api.dev/posts', { ... })
167160
}
168161
```
169162

@@ -173,16 +166,16 @@ Removes something *from* somewhere.
173166
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):
174167

175168
```js
176-
const selectedFilters = ['price', 'availability', 'size'];
169+
const selectedFilters = ['price', 'availability', 'size']
177170

178171
function removeFilter(filterName) {
179-
const filterIndex = selectedFilters.indexOf(filterName);
172+
const filterIndex = selectedFilters.indexOf(filterName)
180173

181174
if (filterIndex !== -1) {
182-
selectedFilters.splice(filterIndex, 1);
175+
selectedFilters.splice(filterIndex, 1)
183176
}
184177

185-
return selectedFilters;
178+
return selectedFilters
186179
}
187180
```
188181

@@ -193,7 +186,7 @@ Imagine you are a content editor, and there is that notorious post you wish to g
193186

194187
```js
195188
function deletePost(id) {
196-
return database.find({ id }).delete();
189+
return database.find({ id }).delete()
197190
}
198191
```
199192

@@ -202,7 +195,7 @@ Creates a new data from the existing one. Mostly applicable to strings or object
202195

203196
```js
204197
function composePageUrl(pageName, pageId) {
205-
return `${pageName.toLowerCase()}-${pageId}`;
198+
return `${pageName.toLowerCase()}-${pageId}`
206199
}
207200
```
208201

@@ -211,74 +204,79 @@ Handles a dedicated action. Often used in naming the callback methods.
211204

212205
```js
213206
function handleLinkClick(event) {
214-
event.preventDefault();
215-
console.log('Clicked a link!');
207+
event.preventDefault()
208+
console.log('Clicked a link!')
216209
}
217210

218-
link.addEventListener('click', handleLinkClick);
211+
link.addEventListener('click', handleLinkClick)
219212
```
220213

221214
---
222215

216+
## Context
217+
218+
A domain that a function operates on.
219+
220+
---
221+
223222
## Prefixes
224-
Prefixes act as enhancers, indicating additional meaning behind variables.
225223

226-
#### `is`
227-
Describes certain characteristic or state of the current context (returns `Boolean`).
224+
Prefix enhances the meaning of a variable.
225+
226+
### `is`
227+
Describes certain characteristic or state of the current context (returns `boolean`).
228228

229229
```js
230-
const color = 'blue';
231-
const isBlue = (color === 'blue'); // characteristic
232-
const isPresent = true; // state
230+
const color = 'blue'
231+
const isBlue = (color === 'blue') // characteristic
232+
const isPresent = true // state
233233

234234
if (isBlue && isPresent) {
235-
console.log('The color is blue and it is present!');
235+
console.log('Blue is present!')
236236
}
237237
```
238238

239-
#### `has`
240-
Describes whether the current context possesses a certain value or state (returns `Boolean`).
239+
### `has`
240+
Describes whether the current context possesses a certain value or state (returns `boolean`).
241241

242242
```js
243243
/* Bad */
244-
const isProductsExist = (productsCount > 0);
245-
const areProductsPresent = (productsCount > 0);
244+
const isProductsExist = (productsCount > 0)
245+
const areProductsPresent = (productsCount > 0)
246246

247247
/* Good */
248-
const hasProducts = (productsCount > 0);
248+
const hasProducts = (productsCount > 0)
249249
```
250250

251-
#### `should`
251+
### `should`
252252
Reflects a positive conditional statement (returns `Boolean`) tightly coupled with a certain action.
253253

254254
```js
255-
const currentUrl = 'https://dev.com';
256-
257-
function shouldUpdateUrl(url) {
258-
return (url !== currentUrl);
255+
function shouldUpdateUrl(url, expectedUrl) {
256+
return (url !== expectedUrl)
259257
}
260258
```
261259

262-
#### `min`/`max`
263-
Represent minimum or maximum value. Useful for describing boundaries or allowed limits.
260+
### `min`/`max`
261+
Represent minimum or maximum value. Useful for describing boundaries or limits.
264262

265263
```js
266264
function PostsList() {
267-
this.minPosts = 3;
268-
this.maxPosts = 10;
265+
this.minPosts = 3
266+
this.maxPosts = 10
269267
}
270268
```
271269

272-
#### `prev`/`next`
270+
### `prev`/`next`
273271
Indicate the previous and the next state of a variable in the current context. Useful for describing state transitions.
274272

275273
```jsx
276274
function fetchPosts() {
277-
const prevPosts = this.state.posts;
275+
const prevPosts = this.state.posts
278276

279-
const fetchedPosts = fetch('...');
280-
const nextPosts = prevPosts.merge(fetchedPosts);
277+
const fetchedPosts = fetch('...')
278+
const nextPosts = prevPosts.merge(fetchedPosts)
281279

282-
return this.setState({ posts: nextPosts });
280+
return this.setState({ posts: nextPosts })
283281
}
284282
```

0 commit comments

Comments
 (0)