Comments
單行註解 // 多行註解 /*...*/
Bad comments
好的程式碼不需要解釋註解或盡量減少,規則是如果一段程式碼必須要註解說明,他必須要被重寫。
// This code will do this thing (...) and that thing (...)
// ...and who knows what else...
very;
complex;
code;function showPrimes(n) {
nextPrime:
for (let i = 2; i < n; i++) {
// check if i is a prime number
for (let j = 2; j < i; j++) {
if (i % j == 0) continue nextPrime;
}
alert(i);
}
}
// better
function showPrimes(n) {
for (let i = 2; i < n; i++) {
if (!isPrime(i)) continue;
alert(i);
}
}
function isPrime(n) {
for (let i = 2; i < n; i++) {
if (n % i == 0) return false;
}
return true;
}Good comments
程式碼架構
函式的運用
為什麼使用這個方法
Last updated
Was this helpful?