-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdont-shorten-names.html
84 lines (62 loc) · 3.03 KB
/
dont-shorten-names.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<script src="https://rawcdn.githack.com/oscarmorrison/md-page/232e97938de9f4d79f4110f6cfd637e186b63317/md-page.js"></script><noscript>
# Don't shorten names
## Shortening a name to avoid duplicating names: 🔴
If you have something called `flight` and something called `flt`, the names need to be *different* - not shortened or abbreviated.
Add some context to the names to differentiate them - `outboundFlight` and `inboundFlight` or something.
Use [destructuring](https://hacks.mozilla.org/2015/05/es6-in-depth-destructuring/) to rename destructured parameters when appropriate.
```javascript
// 🔴 bad - 2 different objects with similar names
const flight = response.body.trips.routes.outbound.flight;
const flt = response.body.trips.routes.inbound.flight;
processFlight(flight);
processFlight(flt);
// 🔴 bad - 1 used and 1 unused, with similar names
const flight = response.body.trips.routes.outbound.flight;
const flt = getValidFlightInfo(flight);
// call it something different - like `validFlight`
// 🟢 good
const { outbound, inbound } = response.body.trips.routes;
const { flight: outboundFlight } = outbound;
const { flight: inboundFlight } = inbound;
processFlight(outboundFlight);
processFlight(inboundFlight);
// 🟢 also good - just reuse `outbound` and `inbound`
const { outbound, inbound } = response.body.trips.routes;
processFlight(outbound.flight);
processFlight(inbound.flight);
// we could also potentially have `processFlight` accept the `inbound/outbound` objects directly
```
---
## Shortening a name because it's too long to type: 🔴
Modern editors have completion features.
If you type part of a name and press `tab` (or possibly some other key combo) it will fill in the entire name.
If you find yourself naming `flight` as `flt` or `f` just to avoid typing, let the editor do the work for you.
```javascript
// 🔴 bad
const f = response.body.trips.routes.outbound.flight;
// 🟢 good
const { flight } = response.body.trips.routes.outbound;
```
Exception: sometimes a very short name can be acceptable within a short anonymous function body, but I still prefer to avoid it:
```javascript
// 🟡 acceptable
// the name `el` is acceptable because there is high locality.
// it's clear what the name means because of the immediate context
// and it is used only once.
const foos = array.map(el => el.foo);
// 🟢 preferred
// however, temporary variables like this are called `points`
// in Functional Programming style, we strive to eliminate them
// which is called `pointfree`
// https://kyleshevlin.com/just-enough-fp-pointfree
const _ = require('lodash');
const foos = array.map(_.property('foo'));
```
---
## Shortening a name because it's too verbose: 🟢
Names like `flightOptionsForAncillaryProductPassengers` can be shortened because they contain more information than is strictly necessary.
Consider rearranging the name to communicate the same thing more efficiently: `ancillaryFlightOptions`
---
## TL;DR
Name things succinctly, but with enough context to be meaningful within the surrounding code.
Your team will thank you!