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
Copy file name to clipboardExpand all lines: 1-js/03-code-quality/03-comments/article.md
+25-25Lines changed: 25 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,38 +1,38 @@
1
-
# Comments
1
+
# Komentar
2
2
3
-
As we know from the chapter<info:structure>, comments can be single-line: starting with`//`and multiline: `/* ... */`.
3
+
Seperti yang kita tahu dari bab<info:structure>, komentar bisa sebaris tunggal: mulai dari`//`dan baris-ganda: `/* ... */`.
4
4
5
-
We normally use them to describe how and why the code works.
5
+
Normalnya kita pakai mereka untuk menjelaskan bagaimana dan kenapa kode bekerja.
6
6
7
-
At first sight, commenting might be obvious, but novices in programming often use them wrongly.
7
+
Di awal pandangan, berkomentar itu sudah jelas, tapi pemula sering salah memakainya dalam pemrograman.
8
8
9
-
## Bad comments
9
+
## Komentar jelek
10
10
11
-
Novices tend to use comments to explain "what is going on in the code". Like this:
11
+
Pemula cenderung memakai komentar untuk menjelaskan "ada apa di dalam kode". Seperti ini:
12
12
13
13
```js
14
-
//This code will do this thing (...) and that thing (...)
15
-
// ...and who knows what else...
14
+
//Kode ini akan melakukan ini (...) dan itu (...)
15
+
// ...dan entah apa lagi...
16
16
very;
17
17
complex;
18
18
code;
19
19
```
20
20
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
+
Tapi di dalam kode yang baik, jumlah komentar "penjelasan" seperti ini sebaiknya minimal. Seriusnya, kode harus bisa dimengerti tanpa mereka.
22
22
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
+
Ada aturan besar tentang itu: "jika kode begitu tak jelas hingga ia butuh komentar, mungkin malah ia harus ditulis ulang".
24
24
25
-
### Recipe: factor out functions
25
+
### Resep: fungsi faktor keluar
26
26
27
-
Sometimes it's beneficial to replace a code piece with a function, like here:
27
+
Kadang menguntungkan untuk mengganti sepotong kode dengan fungsi, seperti ini:
28
28
29
29
```js
30
30
functionshowPrimes(n) {
31
31
nextPrime:
32
32
for (let i =2; i < n; i++) {
33
33
34
34
*!*
35
-
//check if i is a prime number
35
+
//cek apakah i angka prima
36
36
for (let j =2; j < i; j++) {
37
37
if (i % j ==0) continue nextPrime;
38
38
}
@@ -43,7 +43,7 @@ function showPrimes(n) {
43
43
}
44
44
```
45
45
46
-
The better variant, with a factored out function`isPrime`:
46
+
Varian lebih baiknya, dengan fungsi faktor keluar`isPrime`:
47
47
48
48
49
49
```js
@@ -65,21 +65,21 @@ function isPrime(n) {
65
65
}
66
66
```
67
67
68
-
Now we can understand the code easily. The function itself becomes the comment. Such code is called *self-descriptive*.
68
+
Sekarang kita mudah mengerti kodenya. Fungsinya itu sendiri menjadi komentar. Kode macam ini disebut *menjelaskan-diri-sendiri*.
69
69
70
-
### Recipe: create functions
70
+
### Resep: membuat fungsi
71
71
72
-
And if we have a long "code sheet" like this:
72
+
Dan jika kita punya "code sheet" sangat panjang seperti ini:
73
73
74
74
```js
75
-
//here we add whiskey
75
+
//kita tambah \whiskey di sini
76
76
for(let i =0; i <10; i++) {
77
77
let drop =getWhiskey();
78
78
smell(drop);
79
79
add(drop, glass);
80
80
}
81
81
82
-
//here we add juice
82
+
//kita tambah jus di sini
83
83
for(let t =0; t <3; t++) {
84
84
let tomato =getTomato();
85
85
examine(tomato);
@@ -90,7 +90,7 @@ for(let t = 0; t < 3; t++) {
90
90
// ...
91
91
```
92
92
93
-
Then it might be a better variant to refactor it into functions like:
93
+
Maka mungkin varian lebih baiknya ialah merefaktornya menjadi fungsi seperti:
94
94
95
95
```js
96
96
addWhiskey(glass);
@@ -111,13 +111,13 @@ 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.
114
+
Sekali lagi, fungsi mereka sendiri menjelaskan apa yang terjadi. Tak ada yang dikomentari. Dan juga struktur kodenya lebih baik saat dipisah. Kelakuan tiap fungsi jadi lebih jelas, apa yang ia ambil dan yang ia kembalikan.
115
115
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
+
Relitanya, kita tak bisa menghindar total dari komentar "penjelasan". Ada algoritma rumit. Dan ada "tweak" pintar dengan tujuan optimisasi. Tapi umumnya kita harus coba menjaga kodenya simpel dan menjelaskan-diri-sendiri.
117
117
118
-
## Good comments
118
+
## Komentar baik
119
119
120
-
So, explanatory comments are usually bad. Which comments are good?
120
+
Jadi, explanatory comments are usually bad. Which comments are good?
121
121
122
122
Describe the architecture
123
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.
@@ -160,7 +160,7 @@ Why is the task solved this way?
160
160
Any subtle features of the code? Where they are used?
161
161
: If the code has anything subtle and counter-intuitive, it's definitely worth commenting.
162
162
163
-
## Summary
163
+
## Kesimpulan
164
164
165
165
An important sign of a good developer is comments: their presence and even their absence.
0 commit comments