You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Then it might be a better variant to refactor it into functions like:
93
+
將它重構成函式會是更好的寫法,就像:
94
94
95
95
```js
96
96
addWhiskey(glass);
@@ -111,21 +111,21 @@ function addJuice(container) {
111
111
}
112
112
```
113
113
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.
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.
So, explanatory comments are usually bad. Which comments are good?
120
+
所以,解釋用途的註解通常不好,那怎樣才算是好?
121
121
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.
@@ -139,42 +139,43 @@ Document function parameters and usage
139
139
}
140
140
```
141
141
142
-
Such comments allow us to understand the purpose of the function and use it the right way without looking in its code.
142
+
這種註解讓我們理解函式的用途,並且不用看內部的程式碼就可以正確地使用它們。
143
143
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.
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 的資訊。
147
147
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.
If there are many ways to solve the task, why this one? Especially when it's not the most obvious one.
151
+
若有多種解決任務的方法,為什麼選了這種?尤其當它並非是最明顯的一個時。
152
152
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.
0 commit comments