-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVariables.js
More file actions
184 lines (135 loc) · 4.35 KB
/
Variables.js
File metadata and controls
184 lines (135 loc) · 4.35 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//: 13.1 => /* Always use "const" or "let" to declare variables. Not doing so will result in global variables. We want to avoid polluting the global namespace. */
// bad
superPower = new SuperPower();
// good
const superPower = new SuperPower();
//: 13.2 => /* Use one const or let declaration per variable or assignment */
/* Why? It’s easier to add new variable declarations this way, and you never have to worry about swapping out a ; for a , or introducing punctuation-only diffs. You can also step through each declaration with the debugger, instead of jumping through all of them at once. */
// bad
const items = getItems(),
goSportsTeam = true,
dragonball = "z";
// bad
// (compare to above, and try to spot the mistake)
const items2 = getItems(),
goSportsTeam2 = true;
dragonball2 = "z";
// good
const items3 = getItems();
const goSportsTeam3 = true;
const dragonball3 = "z";
//: 13.3 => Group all your "const"s and then group all your "let"s.
/* Why? This is helpful when later on you might need to assign a variable depending on one of the previously assigned variables. */
// bad
let i,
len,
dragonball,
items = getItems(),
goSportsTeam = true;
// bad
let i;
const items = getItems();
let dragonball;
const goSportsTeam = true;
let len;
// good
const goSportsTeam = true;
const items = getItems();
let dragonball;
let i;
let length;
//: 13.4 => Assign variables where you need them, but place them in a reasonable place.
/* Why? "let" and "const" are block scoped and not function scoped. */
// bad - unnecessary function call
function checkName(hasName) {
const name = getName();
if (hasName === "test") {
return false;
}
if (name === "test") {
this.setName("");
return false;
}
return name;
}
// good
function checkName(hasName) {
if (hasName === "test") {
return false;
}
const name = getName();
if (name === "test") {
this.setName("");
return false;
}
return name;
}
//: 13.5 Don't chain variable assignments
// bad
let a = (b = c = 1);
//? the "let" keyword only applies to variable a; variables b and c becoms global variable.
// good
let x = 1;
let y = a;
let z = b;
//: 13.6 => Avoid using unary increments and decrements (++, --).
/* Why? Per the eslint documentation, unary increment and decrement statements are subject to automatic semicolon insertion and can cause silent errors with incrementing or decrementing values within an application. It is also more expressive to mutate your values with statements like num += 1 instead of num++ or num ++. Disallowing unary increment and decrement statements also prevents you from pre-incrementing/pre-decrementing values unintentionally which can also cause unexpected behavior in your programs. */
// bad
const arr = [1, 2, 3, 4, 5];
let num = 1;
num++;
--num;
let sum = 0;
let truthyCount = 0;
for (let i = 0; i < arr.length; i++) {
let value = arr[i];
sum += value;
if (value) {
truthyCount++;
}
}
// good
const array = [1, 2, 3, 4, 5];
let num2 = 1;
num += 1;
num -= 1;
const sum2 = array.reduce((a, b) => a + b, 0);
const truthyCount2 = array.filter(Boolean).length;
//: 13.7 => /* Avoid linebreaks before or after = in an assignment. If your assignment violates max-len, surround value in parens, */
// bad
const foo =
superLongLongLongLongLongLongLongLongFunctionName();
// bad
const foo2
= 'superLongLongLongLongLongLongLongLongString';
// good
const foo3 = (
superLongLongLongLongLongLongLongLongFunctionName()
);
// good
const foo4 = 'superLongLongLongLongLongLongLongLongString';
//: 13.8 => Disallow unused variables
/* Why? Variables that are declared and not used anywhere in the code are most likely an error due to incomplete refactoring. Such variables take up space in the code and can lead to confusion by readers. */
// bad
const some_unused_var = 42;
// Write-only variables are not considered as used.
let y = 10;
y = 5;
// A read for a modification of itself is not considered as used.
let z = 0;
z = z + 1;
// Unused function arguments.
function getX(x, y) {
return x;
}
// good
function getXPlusY(x, y) {
return x + y;
}
const x = 1;
const y = a + 2;
alert(getXPlusY(x, y));
// 'type' is ignored even if unused because it has a rest property sibling.
// This is a form of extracting an object that omits the specified keys.
const { type, ...coords } = data;
// 'coords' is now the 'data' object without its 'type' property.