Skip to content

Commit b489173

Browse files
committed
comments and translations
1 parent ec8b369 commit b489173

File tree

1 file changed

+128
-29
lines changed

1 file changed

+128
-29
lines changed

README.md

Lines changed: 128 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Inspired from [clean-code-javascript](https://github.com/ryanmcdermott/clean-cod
3232
8. [并发](#并发)
3333
9. [错误处理](#错误处理)
3434
10. [格式化](#格式化)
35-
11. [评论](#评论)
35+
11. [注释](#注释)
3636
12. [翻译](#翻译)
3737

3838
## Introduction
@@ -2765,8 +2765,12 @@ try {
27652765

27662766
## Formatting
27672767

2768+
## 格式化
2769+
27682770
Formatting is subjective. Like many rules herein, there is no hard and fast rule that you must follow. The main point is *DO NOT ARGUE* over formatting. There are tons of tools to automate this. Use one! It's a waste of time and money for engineers to argue over formatting. The general rule to follow is *keep consistent formatting rules*.
27692771

2772+
格式化是主观的。 就像其它规则一样, 没有必须让你遵守的硬性规则。 重点是不要因为格式去争论, 有大量的工具来自动格式化, 使用其中的一个即可! 因为做为工程师去争论格式化就是在浪费时间和金钱。 要遵守的通用规则是 *保持一致的格式化规则*
2773+
27702774
For TypeScript there is a powerful tool called [TSLint](https://palantir.github.io/tslint/). It's a static analysis tool that can help you improve dramatically the readability and maintainability of your code. There are ready to use TSLint configurations that you can reference in your projects:
27712775

27722776
- [TSLint Config Standard](https://www.npmjs.com/package/tslint-config-standard) - standard style rules
@@ -2785,11 +2789,31 @@ For TypeScript there is a powerful tool called [TSLint](https://palantir.github.
27852789

27862790
Refer also to this great [TypeScript StyleGuide and Coding Conventions](https://basarat.gitbooks.io/typescript/docs/styleguide/styleguide.html) source.
27872791

2788-
### Use consistent capitalization
2792+
对 TypeScript 来说, 有一个强大的工具叫做 [TSLint](https://palantir.github.io/tslint/) 。 它是一个可以显著提高代码的可读性和可维护性的静态分析工具。 也已经有一些可用的 TSLint 配置供你在项目中参考:
2793+
2794+
- [标准的 TSLint 配置](https://www.npmjs.com/package/tslint-config-standard) - 标准风格规则
2795+
2796+
- [Airbnb 的 TSLint 配置](https://www.npmjs.com/package/tslint-config-airbnb) - Airbnb 风格指南
2797+
2798+
- [简洁代码的 TSLint 配置](https://www.npmjs.com/package/tslint-clean-code) - 受 [Clean Code: A Handbook of Agile Software Craftsmanship](https://www.amazon.ca/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882) 影响的 TSLint 规则
2799+
2800+
- [React 的 TSLint 配置](https://www.npmjs.com/package/tslint-react) - React & JSX 相关的 TSLint 配置
2801+
2802+
- [TSLint + Prettier](https://www.npmjs.com/package/tslint-config-prettier) - [Prettier](https://github.com/prettier/prettier) 的代码检查规则
2803+
2804+
- [TypeScript 的 ESLint 规则](https://www.npmjs.com/package/tslint-eslint-rules) - TypeScript 的 ESLint 规则
2805+
2806+
- [Immutable](https://www.npmjs.com/package/tslint-immutable) - TypeScript 中禁止突变的规则
2807+
2808+
也请参考这个伟大的 [TypeScript 风格指南和编码约定](https://basarat.gitbooks.io/typescript/docs/styleguide/styleguide.html)
2809+
2810+
### 使用一致的大小写
27892811

27902812
Capitalization tells you a lot about your variables, functions, etc. These rules are subjective, so your team can choose whatever they want. The point is, no matter what you all choose, just *be consistent*.
27912813

2792-
**Bad:**
2814+
大小写可以告诉你很多关于你的变量, 函数等等。 这些规则是主观的, 所以你的团队可以选择他们想要的任何东西。 关键是, 无论你选择什么, 只要 *保持一致* 即可。
2815+
2816+
**不好的:**
27932817

27942818
```ts
27952819
const DAYS_IN_WEEK = 7;
@@ -2805,7 +2829,7 @@ type animal = { /* ... */ }
28052829
type Container = { /* ... */ }
28062830
```
28072831
2808-
**Good:**
2832+
**好的:**
28092833
28102834
```ts
28112835
const DAYS_IN_WEEK = 7;
@@ -2824,14 +2848,21 @@ type Container = { /* ... */ }
28242848
Prefer using `PascalCase` for class, interface, type and namespace names.
28252849
Prefer using `camelCase` for variables, functions and class members.
28262850
2827-
**[⬆ back to top](#table-of-contents)**
2851+
建议类、 接口、 类型和命名空间使用 `PascalCase` 风格, 变量、 函数和类成员使用 `camelCase` 风格。
2852+
2853+
**[⬆ 返回目录](#目录)**
28282854
28292855
### Function callers and callees should be close
28302856
2857+
### 函数的调用方与被调用方应该靠近
2858+
28312859
If a function calls another, keep those functions vertically close in the source file. Ideally, keep the caller right above the callee.
28322860
We tend to read code from top-to-bottom, like a newspaper. Because of this, make your code read that way.
28332861
2834-
**Bad:**
2862+
如果一个函数调用另一个, 则在代码中这两个函数的竖直位置应该靠近。 理想情况下,保持被调用函数在被调用函数的正上方。 我们倾向于从上到下阅读代码, 就像读一章报纸。 由于这个原因, 保持你的代码可
2863+
以按照这种方式阅读。
2864+
2865+
**不好的:**
28352866
28362867
```ts
28372868
class PerformanceReview {
@@ -2872,7 +2903,7 @@ const review = new PerformanceReview(employee);
28722903
review.review();
28732904
```
28742905

2875-
**Good:**
2906+
**好的:**
28762907

28772908
```ts
28782909
class PerformanceReview {
@@ -2913,10 +2944,12 @@ const review = new PerformanceReview(employee);
29132944
review.review();
29142945
```
29152946

2916-
**[back to top](#table-of-contents)**
2947+
**[返回目录](#目录)**
29172948

29182949
### Organize imports
29192950

2951+
### 组织导入
2952+
29202953
With clean and easy to read import statements you can quickly see the dependencies of current code. Make sure you apply following good practices for `import` statements:
29212954

29222955
- Import statements should be alphabetized and grouped.
@@ -2932,7 +2965,22 @@ With clean and easy to read import statements you can quickly see the dependenci
29322965
- modules from a parent directory (i.e. `import foo from '../foo'; import qux from '../../foo/qux';`)
29332966
- modules from the same or a sibling's directory (i.e. `import bar from './bar'; import baz from './bar/baz';`)
29342967

2935-
**Bad:**
2968+
使用简洁易读的 import 语句, 您可以快速查看当前代码的依赖关系。 确保对 `import` 语句应用以下良好实践:
2969+
2970+
- 导入应当排序并分组;
2971+
- 未使用的导入应当删除;
2972+
- 命名导入应当排序 (比如: `import {A, B, C} from 'foo';`)
2973+
- 导入源应当在分组内排序, 比如: `import * as foo from 'a'; import * as bar from 'b';`
2974+
- 分组导入之间保留一个空行;
2975+
- 分组之间应当遵守下面的顺序:
2976+
- 兼容性填充 (比如: `import 'reflect-metadata';`)
2977+
- Node 内置模块 (比如: `import fs from 'fs';`)
2978+
- 外部模块 (比如: `import { query } from 'itiriri';`)
2979+
- 内部模块 (i.e `import { UserService } from 'src/services/userService';`)
2980+
- 来自父目录的模块 (比如: `import foo from '../foo'; import qux from '../../foo/qux';`)
2981+
- 来自相同目录或同级目录的模块 (比如: `import bar from './bar'; import baz from './bar/baz';`)
2982+
2983+
**不好的:**
29362984

29372985
```ts
29382986
import { TypeDefinition } from '../types/typeDefinition';
@@ -2944,7 +2992,7 @@ import { BindingScopeEnum, Container } from 'inversify';
29442992
import 'reflect-metadata';
29452993
```
29462994

2947-
**Good:**
2995+
**好的:**
29482996

29492997
```ts
29502998
import 'reflect-metadata';
@@ -2959,21 +3007,27 @@ import { ApiCredentials, Adapters } from './common/api/authorization';
29593007
import { ConfigPlugin } from './plugins/config/configPlugin';
29603008
```
29613009

2962-
**[back to top](#table-of-contents)**
3010+
**[返回目录](#目录)**
29633011

29643012
### Use typescript aliases
29653013

3014+
### 使用 TypeScript 别名
3015+
29663016
Create prettier imports by defining the paths and baseUrl properties in the compilerOptions section in the `tsconfig.json`
29673017

29683018
This will avoid long relative paths when doing imports.
29693019

2970-
**Bad:**
3020+
通过在 `tsconfig.json` 文件中的 compilerOptions 对象内定义路径和基础路径, 可以创建更漂亮的导入。
3021+
3022+
这将避免导入时出现太长的相对路径。
3023+
3024+
**不好的:**
29713025

29723026
```ts
29733027
import { UserService } from '../../../services/UserService';
29743028
```
29753029

2976-
**Good:**
3030+
**好的:**
29773031

29783032
```ts
29793033
import { UserService } from '@services/UserService';
@@ -2993,40 +3047,55 @@ import { UserService } from '@services/UserService';
29933047
...
29943048
```
29953049

2996-
**[back to top](#table-of-contents)**
3050+
**[返回目录](#目录)**
29973051

29983052
## Comments
29993053

3054+
## 注释
3055+
30003056
The use of a comments is an indication of failure to express without them. Code should be the only source of truth.
30013057

30023058
> Don’t comment bad code—rewrite it.
30033059
> *Brian W. Kernighan and P. J. Plaugher*
30043060
3061+
使用注释就意味着代码的表达失败。 代码应该是唯一的事实来源。
3062+
3063+
> 不是为烂代码添加注释, 而是重写它们。
3064+
> - *Brian W. Kernighan 和 P. J. Plaugher*
3065+
30053066
### Prefer self explanatory code instead of comments
30063067

3068+
### 倾向于自描述的代码而不是注释
3069+
30073070
Comments are an apology, not a requirement. Good code *mostly* documents itself.
30083071

3009-
**Bad:**
3072+
评论是代码的辩解, 不是要求。 多数情况下, 好的代码就是文档。
3073+
3074+
**不好的:**
30103075

30113076
```ts
30123077
// Check if subscription is active.
30133078
if (subscription.endDate > Date.now) { }
30143079
```
30153080

3016-
**Good:**
3081+
**好的:**
30173082

30183083
```ts
30193084
const isSubscriptionActive = subscription.endDate > Date.now;
30203085
if (isSubscriptionActive) { /* ... */ }
30213086
```
30223087

3023-
**[back to top](#table-of-contents)**
3088+
**[返回目录](#目录)**
30243089

30253090
### Don't leave commented out code in your codebase
30263091

3092+
### 不要在代码库中保存注释掉的代码
3093+
30273094
Version control exists for a reason. Leave old code in your history.
30283095

3029-
**Bad:**
3096+
因为有版本控制, 把旧的代码留在历史记录即可
3097+
3098+
**不好的:**
30303099

30313100
```ts
30323101
type User = {
@@ -3037,7 +3106,7 @@ type User = {
30373106
}
30383107
```
30393108
3040-
**Good:**
3109+
**好的:**
30413110
30423111
```ts
30433112
type User = {
@@ -3046,13 +3115,18 @@ type User = {
30463115
}
30473116
```
30483117
3049-
**[⬆ back to top](#table-of-contents)**
3118+
**[⬆ 返回目录](#目录)**
30503119
30513120
### Don't have journal comments
30523121
3122+
### 不要有日志式的注释
3123+
30533124
Remember, use version control! There's no need for dead code, commented code, and especially journal comments. Use `git log` to get history!
30543125
3055-
**Bad:**
3126+
记住, 使用版本控制! 不需要僵尸代码, 注释掉的代码, 尤其是日志式的评论。 使用 `git log`
3127+
获取历史记录。
3128+
3129+
**不好的:**
30563130
30573131
```ts
30583132
/**
@@ -3066,22 +3140,27 @@ function combine(a: number, b: number): number {
30663140
}
30673141
```
30683142

3069-
**Good:**
3143+
**好的:**
30703144

30713145
```ts
30723146
function combine(a: number, b: number): number {
30733147
return a + b;
30743148
}
30753149
```
30763150

3077-
**[back to top](#table-of-contents)**
3151+
**[返回目录](#目录)**
30783152

30793153
### Avoid positional markers
30803154

3155+
### 避免占位符
3156+
30813157
They usually just add noise. Let the functions and variable names along with the proper indentation and formatting give the visual structure to your code.
30823158
Most IDE support code folding feature that allows you to collapse/expand blocks of code (see Visual Studio Code [folding regions](https://code.visualstudio.com/updates/v1_17#_folding-regions)).
30833159

3084-
**Bad:**
3160+
它们仅仅添加了干扰。 让函数和变量名称与合适的缩进和格式化为你的代码提供视觉结构。
3161+
绝大多数 IDE 支持代码折叠, 允许你展开/关闭代码段 (查看 Visual Studio Code [folding regions](https://code.visualstudio.com/updates/v1_17#_folding-regions) ) 。
3162+
3163+
**不好的:**
30853164

30863165
```ts
30873166
////////////////////////////////////////////////////////////////////////////////
@@ -3113,7 +3192,7 @@ class Client {
31133192
};
31143193
```
31153194

3116-
**Good:**
3195+
**好的:**
31173196

31183197
```ts
31193198
class Client {
@@ -3136,17 +3215,23 @@ class Client {
31363215
};
31373216
```
31383217

3139-
**[back to top](#table-of-contents)**
3218+
**[返回目录](#目录)**
31403219

31413220
### TODO comments
31423221

3222+
### TODO 注释
3223+
31433224
When you find yourself that you need to leave notes in the code for some later improvements,
31443225
do that using `// TODO` comments. Most IDE have special support for those kind of comments so that
31453226
you can quickly go over the entire list of todos.
31463227

31473228
Keep in mind however that a *TODO* comment is not an excuse for bad code.
31483229

3149-
**Bad:**
3230+
当你发现你需要在代码中做一些后期改进是, 请使用 `// TODO` 注释。 大多数 IDE 对这种类型的注释有着特殊的支持, 你可以快速的发现全部的 TODO 列表。
3231+
3232+
记住, *TODO* 注视并不是烂代码的借口。
3233+
3234+
**不好的:**
31503235

31513236
```ts
31523237
function getActiveSubscriptions(): Promise<Subscription[]> {
@@ -3155,7 +3240,7 @@ function getActiveSubscriptions(): Promise<Subscription[]> {
31553240
}
31563241
```
31573242

3158-
**Good:**
3243+
**好的:**
31593244

31603245
```ts
31613246
function getActiveSubscriptions(): Promise<Subscription[]> {
@@ -3164,19 +3249,33 @@ function getActiveSubscriptions(): Promise<Subscription[]> {
31643249
}
31653250
```
31663251

3167-
**[back to top](#table-of-contents)**
3252+
**[返回目录](#目录)**
31683253

31693254
## Translations
31703255

3256+
## 翻译
3257+
31713258
This is also available in other languages:
31723259
- ![br](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Brazil.png) **Brazilian Portuguese**: [vitorfreitas/clean-code-typescript](https://github.com/vitorfreitas/clean-code-typescript)
31733260

3261+
本文也有其它语言版本:
3262+
3263+
- ![br](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Brazil.png) **巴西葡萄牙语**: [vitorfreitas/clean-code-typescript](https://github.com/vitorfreitas/clean-code-typescript)
3264+
31743265
There is work in progress for translating this to other languages:
31753266

31763267
- ![cn](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/China.png) Chinese
31773268
- ![ja](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Japan.png) Japanese
31783269
- ![kr](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/South-Korea.png) Korean
31793270

3271+
还有一些语言的版本也正在翻译中:
3272+
3273+
- ![cn](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/China.png) 中文
3274+
- ![ja](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Japan.png) 日语
3275+
- ![kr](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/South-Korea.png) 韩语
3276+
31803277
References will be added once translations are completed.
31813278
Check this [discussion](https://github.com/labs42io/clean-code-typescript/issues/15) for more details and progress.
31823279
You can make an indispensable contribution to *Clean Code* community by translating this to your language.
3280+
3281+
一旦翻译完成, 就会在这里添加链接。 关注这个[讨论](https://github.com/labs42io/clean-code-typescript/issues/15)以获得更多的详情以及进度。 您可以通过将其翻译成您的语言,为* Clean Code *社区做出不可或缺的贡献。

0 commit comments

Comments
 (0)