Skip to content

Commit 4a613f1

Browse files
committed
variables
1 parent 68700b3 commit 4a613f1

File tree

1 file changed

+58
-26
lines changed

1 file changed

+58
-26
lines changed

README.md

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Inspired from [clean-code-javascript](https://github.com/ryanmcdermott/clean-cod
3333
9. [错误处理](#错误处理)
3434
10. [格式化](#格式化)
3535
11. [评论](#评论)
36+
12. [翻译](#翻译)
3637

3738
## Introduction
3839

@@ -84,13 +85,17 @@ improvement. Beat up the code instead!
8485

8586
**[⬆ 返回目录](#目录)**
8687

87-
## Variables
88+
## Variables 变量
8889

8990
### Use meaningful variable names
9091

92+
### 使用有意义的变量名称
93+
9194
Distinguish names in such a way that the reader knows what the differences offer.
9295

93-
**Bad:**
96+
使用可以区分的名称, 让读者知道他们的区别是什么。
97+
98+
**不好的:**
9499

95100
```ts
96101
function between<T>(a1: T, a2: T, a3: T): boolean {
@@ -99,21 +104,25 @@ function between<T>(a1: T, a2: T, a3: T): boolean {
99104

100105
```
101106

102-
**Good:**
107+
**好的:**
103108

104109
```ts
105110
function between<T>(value: T, left: T, right: T): boolean {
106111
return left <= value && value <= right;
107112
}
108113
```
109114

110-
**[back to top](#table-of-contents)**
115+
**[返回目录](#目录)**
111116

112117
### Use pronounceable variable names
113118

119+
### 使用可拼读的变量名称
120+
114121
If you can’t pronounce it, you can’t discuss it without sounding like an idiot.
115122

116-
**Bad:**
123+
如果你不能把它读出来, 那你就不能和同事讨论它, 这看起来像块纱布。
124+
125+
**不好的:**
117126

118127
```ts
119128
type DtaRcrd102 = {
@@ -123,7 +132,7 @@ type DtaRcrd102 = {
123132
}
124133
```
125134
126-
**Good:**
135+
**好的:**
127136
128137
```ts
129138
type Customer = {
@@ -133,51 +142,62 @@ type Customer = {
133142
}
134143
```
135144
136-
**[⬆ back to top](#table-of-contents)**
145+
**[⬆ 返回目录](#目录)**
137146
138147
### Use the same vocabulary for the same type of variable
139148
140-
**Bad:**
149+
### 为相同类型的变量使用相同的词汇
150+
151+
**不好的:**
141152
142153
```ts
143154
function getUserInfo(): User;
144155
function getUserDetails(): User;
145156
function getUserData(): User;
146157
```
147158

148-
**Good:**
159+
**好的:**
149160

150161
```ts
151162
function getUser(): User;
152163
```
153164

154-
**[back to top](#table-of-contents)**
165+
**[返回目录](#目录)**
155166

156-
### Use searchable names
167+
### 使用可搜索的名称
157168

158169
We will read more code than we will ever write. It's important that the code we do write is readable and searchable. By *not* naming variables that end up being meaningful for understanding our program, we hurt our readers. Make your names searchable. Tools like [TSLint](https://palantir.github.io/tslint/rules/no-magic-numbers/) can help identify unnamed constants.
159170

160-
**Bad:**
171+
172+
我们要阅读的代码比要写的代码多得多, 所以我们写出的代码的可读性和可搜索性是很重要的。 使用没有
173+
意义的变量名将会导致我们的程序难于理解, 将会伤害我们的读者, 所以请使用可搜索的变量名。 类似 [TSLint](https://palantir.github.io/tslint/rules/no-magic-numbers/)
174+
的工具可以帮助我们找到未命名的常量。
175+
176+
**不好的:**
161177

162178
```ts
163179
// What the heck is 86400000 for?
180+
// 艹, 86400000 是什么鬼?
164181
setTimeout(restart, 86400000);
165182
```
166183

167-
**Good:**
184+
**好的:**
168185

169186
```ts
170187
// Declare them as capitalized named constants.
188+
// 将他们声明为大写的变量
171189
const MILLISECONDS_IN_A_DAY = 24 * 60 * 60 * 1000;
172190

173191
setTimeout(restart, MILLISECONDS_IN_A_DAY);
174192
```
175193

176-
**[back to top](#table-of-contents)**
194+
**[返回目录](#目录)**
177195

178196
### Use explanatory variables
179197

180-
**Bad:**
198+
### 使用解释性的变量
199+
200+
**不好的:**
181201

182202
```ts
183203
declare const users: Map<string, User>;
@@ -187,7 +207,7 @@ for (const keyValue of users) {
187207
}
188208
```
189209

190-
**Good:**
210+
**好的:**
191211

192212
```ts
193213
declare const users: Map<string, User>;
@@ -197,36 +217,44 @@ for (const [id, user] of users) {
197217
}
198218
```
199219

200-
**[back to top](#table-of-contents)**
220+
**[返回目录](#目录)**
201221

202222
### Avoid Mental Mapping
203223

224+
### 避免心理映射
225+
204226
Explicit is better than implicit.
205227
*Clarity is king.*
206228

207-
**Bad:**
229+
显示比隐式更好。 *清晰为王!*
230+
231+
**不好的:**
208232

209233
```ts
210234
const u = getUser();
211235
const s = getSubscription();
212236
const t = charge(u, s);
213237
```
214238

215-
**Good:**
239+
**好的:**
216240

217241
```ts
218242
const user = getUser();
219243
const subscription = getSubscription();
220244
const transaction = charge(user, subscription);
221245
```
222246

223-
**[back to top](#table-of-contents)**
247+
**[返回目录](#目录)**
224248

225249
### Don't add unneeded context
226250

251+
### 不添加不必要的上下文
252+
227253
If your class/type/object name tells you something, don't repeat that in your variable name.
228254

229-
**Bad:**
255+
如果你的类/类型/对象名有意义, 不必在变量名上再重复。
256+
257+
**不好的:**
230258

231259
```ts
232260
type Car = {
@@ -240,7 +268,7 @@ function print(car: Car): void {
240268
}
241269
```
242270

243-
**Good:**
271+
**好的:**
244272

245273
```ts
246274
type Car = {
@@ -254,13 +282,17 @@ function print(car: Car): void {
254282
}
255283
```
256284

257-
**[back to top](#table-of-contents)**
285+
**[返回目录](#目录)**
258286

259287
### Use default arguments instead of short circuiting or conditionals
260288

289+
### 使用默认变量替代短路运算或条件
290+
261291
Default arguments are often cleaner than short circuiting.
262292

263-
**Bad:**
293+
默认参数通常比短路运算更清晰。
294+
295+
**不好的:**
264296

265297
```ts
266298
function loadPages(count?: number) {
@@ -269,15 +301,15 @@ function loadPages(count?: number) {
269301
}
270302
```
271303

272-
**Good:**
304+
**好的:**
273305

274306
```ts
275307
function loadPages(count: number = 10) {
276308
// ...
277309
}
278310
```
279311

280-
**[back to top](#table-of-contents)**
312+
**[返回目录](#目录)**
281313

282314
## Functions
283315

0 commit comments

Comments
 (0)