Skip to content

Commit cff5242

Browse files
lenchen1112kaddopurArvinH
authored
Comments (#44)
* feat: translation of Comments * Apply suggestions from code review Co-Authored-By: Jason Huang <[email protected]> Co-Authored-By: ArvinH <[email protected]>
1 parent 9042cb4 commit cff5242

File tree

1 file changed

+54
-53
lines changed

1 file changed

+54
-53
lines changed

1-js/03-code-quality/03-comments/article.md

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
1-
# Comments
1+
# 註解
22

3-
As we know from the chapter <info:structure>, comments can be single-line: starting with `//` and multiline: `/* ... */`.
3+
如我們從章節 <info:structure> 所學,註解可以是由 `//` 開始的單獨一行,或是使用 `/* ... */` 的多行註解。
44

5-
We normally use them to describe how and why the code works.
5+
我們通常使用它們來描述程式碼是如何與為什麼會運作。
66

7-
At first sight, commenting might be obvious, but novices in programming often use them wrongly.
7+
第一次寫註解應該會覺得很簡單,但菜鳥程式設計師通常會錯誤地使用它們。
88

9-
## Bad comments
9+
## 不佳的註解
1010

11-
Novices tend to use comments to explain "what is going on in the code". Like this:
11+
初學者傾向於使用註解來解釋 "程式碼中發生什麼事",像這樣:
1212

1313
```js
14-
// This code will do this thing (...) and that thing (...)
15-
// ...and who knows what else...
14+
// 這段程式碼會這樣 (...) 和那樣 (...)
15+
// ...但誰知道還有什麼其它事...
1616
very;
1717
complex;
1818
code;
1919
```
2020

21-
But in good code, the amount of such "explanatory" comments should be minimal. Seriously, the code should be easy to understand without them.
21+
但在良好的程式碼中,這種 "解釋用途" 註解的數量應該要盡量少。嚴格來說,就算沒有這些註解,程式碼應該也要易於理解。
2222

23-
There's a great rule about that: "if the code is so unclear that it requires a comment, then maybe it should be rewritten instead".
23+
關於這點很棒的規則:"若程式碼不清楚到需要幫它寫註解,那也許它應該要被改寫"。
2424

25-
### Recipe: factor out functions
25+
### 訣竅:分解成函式
2626

27-
Sometimes it's beneficial to replace a code piece with a function, like here:
27+
有時候將一段程式碼片段替換成函式會更有幫助,像這樣:
2828

2929
```js
3030
function showPrimes(n) {
3131
nextPrime:
3232
for (let i = 2; i < n; i++) {
3333

3434
*!*
35-
// check if i is a prime number
35+
// 確認 i 是否為質數
3636
for (let j = 2; j < i; j++) {
3737
if (i % j == 0) continue nextPrime;
3838
}
@@ -43,7 +43,7 @@ function showPrimes(n) {
4343
}
4444
```
4545

46-
The better variant, with a factored out function `isPrime`:
46+
更好的寫法,使用分解出來的函式 `isPrime`
4747

4848

4949
```js
@@ -65,21 +65,21 @@ function isPrime(n) {
6565
}
6666
```
6767

68-
Now we can understand the code easily. The function itself becomes the comment. Such code is called *self-descriptive*.
68+
現在我們可以更容易理解程式碼了。此函式本身變成了註解,這種程式碼被稱之為具有 *自我描述性(self-descriptive*
6969

70-
### Recipe: create functions
70+
### 訣竅:建立函式
7171

72-
And if we have a long "code sheet" like this:
72+
若我們有一個很長的 "程式碼片段(code sheet)",像這樣:
7373

7474
```js
75-
// here we add whiskey
75+
// 這邊我們加入威士忌
7676
for(let i = 0; i < 10; i++) {
7777
let drop = getWhiskey();
7878
smell(drop);
7979
add(drop, glass);
8080
}
8181

82-
// here we add juice
82+
// 這邊我們加入果汁
8383
for(let t = 0; t < 3; t++) {
8484
let tomato = getTomato();
8585
examine(tomato);
@@ -90,7 +90,7 @@ for(let t = 0; t < 3; t++) {
9090
// ...
9191
```
9292

93-
Then it might be a better variant to refactor it into functions like:
93+
將它重構成函式會是更好的寫法,就像:
9494

9595
```js
9696
addWhiskey(glass);
@@ -111,21 +111,21 @@ function addJuice(container) {
111111
}
112112
```
113113

114-
Once again, functions themselves tell what's going on. There's nothing to comment. And also the code structure is better when split. It's clear what every function does, what it takes and what it returns.
114+
再次強調,函式本身就會告訴我們發生什麼事,所以不需要註解。而且拆開後的程式碼結構也變得更好了,它做了什麼、需要什麼和回傳什麼都變得很清楚。
115115

116-
In reality, we can't totally avoid "explanatory" comments. There are complex algorithms. And there are smart "tweaks" for purposes of optimization. But generally we should try to keep the code simple and self-descriptive.
116+
事實上,我們無法完全避免 "解釋用途" 的註解。因為會有一些複雜的演算法,和一些做優化用的聰明 "調整" 存在。但一般來說我們應該試著維持程式碼簡單並具有自我描述性。
117117

118-
## Good comments
118+
## 良好的程式碼
119119

120-
So, explanatory comments are usually bad. Which comments are good?
120+
所以,解釋用途的註解通常不好,那怎樣才算是好?
121121

122-
Describe the architecture
123-
: Provide a high-level overview of components, how they interact, what's the control flow in various situations... In short -- the bird's eye view of the code. There's a special language [UML](http://wikipedia.org/wiki/Unified_Modeling_Language) to build high-level architecture diagrams explaining the code. Definitely worth studying.
122+
描述架構
123+
: 提供一個組件之間的高層次概況,它們是如何互動的,不同情況的控制流程是什麼... 簡單來說 -- 程式碼的鳥瞰圖。有種特殊的語言 [UML](http://wikipedia.org/wiki/Unified_Modeling_Language) 可建立高層次架構圖用以解釋程式碼,值得一讀。
124124

125-
Document function parameters and usage
126-
: There's a special syntax [JSDoc](http://en.wikipedia.org/wiki/JSDoc) to document a function: usage, parameters, returned value.
125+
將函式參數和用途寫入文件
126+
: 有個特殊的語法 [JSDoc](http://en.wikipedia.org/wiki/JSDoc) 可以幫函式寫文件:用途、參數和回傳值。
127127

128-
For instance:
128+
舉個例:
129129
```js
130130
/**
131131
* Returns x raised to the n-th power.
@@ -139,42 +139,43 @@ Document function parameters and usage
139139
}
140140
```
141141

142-
Such comments allow us to understand the purpose of the function and use it the right way without looking in its code.
142+
這種註解讓我們理解函式的用途,並且不用看內部的程式碼就可以正確地使用它們。
143143

144-
By the way, many editors like [WebStorm](https://www.jetbrains.com/webstorm/) can understand them as well and use them to provide autocomplete and some automatic code-checking.
144+
對了,很多編輯器如 [WebStorm](https://www.jetbrains.com/webstorm/) 也可以看懂它們,並且提供自動完成和一些自動程式碼檢查。
145145

146-
Also, there are tools like [JSDoc 3](https://github.com/jsdoc3/jsdoc) that can generate HTML-documentation from the comments. You can read more information about JSDoc at <http://usejsdoc.org/>.
146+
同樣的,有些工具像是 [JSDoc 3](https://github.com/jsdoc3/jsdoc) 可以由註解產生 HTML 文件,你可以在 <http://usejsdoc.org/> 閱讀更多 JSDoc 的資訊。
147147

148-
Why is the task solved this way?
149-
: What's written is important. But what's *not* written may be even more important to understand what's going on. Why is the task solved exactly this way? The code gives no answer.
148+
為什麼任務要用這種方式處理?
149+
: 寫下了什麼是很重要的,但什麼 *沒被* 寫下的東西可能對於想理解發生什麼事來說更為重要。為什麼這個任務一定得使用這種方式處理?程式碼中可能得不到答案。
150150

151-
If there are many ways to solve the task, why this one? Especially when it's not the most obvious one.
151+
若有多種解決任務的方法,為什麼選了這種?尤其當它並非是最明顯的一個時。
152152

153-
Without such comments the following situation is possible:
154-
1. You (or your colleague) open the code written some time ago, and see that it's "suboptimal".
155-
2. You think: "How stupid I was then, and how much smarter I'm now", and rewrite using the "more obvious and correct" variant.
156-
3. ...The urge to rewrite was good. But in the process you see that the "more obvious" solution is actually lacking. You even dimly remember why, because you already tried it long ago. You revert to the correct variant, but the time was wasted.
153+
沒有這種註解的話,底下這些情況就可能會發生:
154+
1. 你(或你同事)打開一段時間前寫的程式碼,並發現它 "並非最佳解"。
155+
2. 你想著:"我當初怎麼這麼蠢,還好我現在聰明多了",並且使用 "更明顯且正確" 的寫法改寫它。
156+
3. ...改寫的衝勁是好的,但在過程中你發現 "更明顯" 的解法其實有所不足,甚至你依稀想起為什麼不行,因為你很久之前早就試過了。接著你把程式碼恢復原樣,但時間早已被浪費掉。
157157

158-
Comments that explain the solution are very important. They help to continue development the right way.
158+
解釋解法的註解是非常重要的,它們有助於在正確的道路上開發。
159159

160-
Any subtle features of the code? Where they are used?
161-
: If the code has anything subtle and counter-intuitive, it's definitely worth commenting.
160+
程式碼中有著微妙的功能?它們被用在哪?
161+
: 若程式碼內有任何微妙且違反直覺的東西,那就值得註解下來。
162162

163-
## Summary
163+
## 總結
164164

165-
An important sign of a good developer is comments: their presence and even their absence.
165+
一個良好開發者的重要指標之一就是其所寫的註解:知道何時該寫註解,何時不必寫。
166166

167-
Good comments allow us to maintain the code well, come back to it after a delay and use it more effectively.
167+
良好的註解讓我們更好維護程式碼,在隔段時間後回來看它依然可以有效率的使用。
168168

169-
**Comment this:**
169+
**註解這些:**
170170

171-
- Overall architecture, high-level view.
172-
- Function usage.
173-
- Important solutions, especially when not immediately obvious.
171+
- 整體架構,高層次觀點。
172+
- 函式的使用。
173+
- 重要的解法,特別是那些不那麼立即明顯的。
174174

175-
**Avoid comments:**
175+
**避免註解這些:**
176176

177-
- That tell "how code works" and "what it does".
178-
- Put them in only if it's impossible to make the code so simple and self-descriptive that it doesn't require them.
177+
- 告訴你 "程式碼如何運作" 和 "它做了什麼"。
178+
- 只有在沒辦法讓程式碼夠簡單並具有自我描述性質的時候,才需要使用註解。
179+
180+
註解也可以被像是 JSDoc3 之類的自動文件工具所使用:可以讀懂註解並生成 HTML 文件(或其它格式)。
179181

180-
Comments are also used for auto-documenting tools like JSDoc3: they read them and generate HTML-docs (or docs in another format).

0 commit comments

Comments
 (0)