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
Our code must be as clean and easy to read as possible.
3
+
Kode kita harus sebisa mungkin bersih dan mudah dibaca.
4
4
5
-
That is actually the art of programming -- to take a complex task and code it in a way that is both correct and human-readable. A good code style greatly assists in that.
5
+
Ini sebenarnya seni pemrograman -- ambil tugas rumit dan mengkodenya dengan cara yang benar dan dapat dibaca manusia. Gaya kode yang baik punya andil besar di situ.
6
6
7
7
## Syntax
8
8
9
-
Here is a cheat sheet with some suggested rules (see below for more details):
9
+
Ini adalah satu cheatsheet dengan beberapa aturan yang disarankan (lihat bawah untuk lebih detil):
10
10
11
11

12
12
<!--
@@ -34,65 +34,65 @@ if (n < 0) {
34
34
35
35
-->
36
36
37
-
Now let's discuss the rules and reasons for them in detail.
37
+
Sekarang kita bahas aturannya dan alasannya secara detil.
38
38
39
-
```warn header="There are no \"you must\" rules"
40
-
Nothing is set in stone here. These are style preferences, not religious dogmas.
39
+
```warn header="Tak ada aturan yang memaksa"
40
+
Tak ada yang diset sekeras batu di sini. Mereka cuma preferensi gaya saja, buka dogma agama.
41
41
```
42
42
43
-
### Curly Braces
43
+
### Kurung Kurawal
44
44
45
-
In most JavaScript projects curly braces are written in "Egyptian" style with the opening brace on the same line as the corresponding keyword -- not on a new line. There should also be a space before the opening bracket, like this:
45
+
Di kebanyakan proyek JavaScript kurung kurawal ditulis dalam gaya "Egyptian" dengan kurawal buka di baris yang sama dengan katakunci -- bukan di baris baru. Juga harus ada spasi sebelum bracket pembuka, seperti ini:
46
46
47
47
```js
48
48
if (condition) {
49
-
//do this
50
-
// ...and that
51
-
// ...and that
49
+
//lakukan ini
50
+
// ...dan itu
51
+
// ...dan itu
52
52
}
53
53
```
54
54
55
-
A single-line construct, such as `if (condition) doSomething()`, is an important edge case. Should we use braces at all?
55
+
Konstruksi sebaris, seperti `if (condition) doSomething()`, ialah hal penting sampingan. Haruskah kita memakai kurawal sama sekali?
56
56
57
-
Here are the annotated variants so you can judge their readability for yourself:
57
+
Ini adalah varian yang teranotasi jadi kamu bisa menilai sendiri keterbacaan mereka:
58
58
59
-
1. 😠 Beginners sometimes do that. Bad! Curly braces are not needed:
59
+
1. 😠 Pemula kadang melakukan itu. Buruk! Kurung kurawal tak dibutuhkan:
60
60
```js
61
61
if (n <0) *!*{*/!*alert(`Power ${n} is not supported`);*!*}*/!*
62
62
```
63
-
2. 😠 Split to a separate line without braces. Never do that, easy to make an error when adding new lines:
63
+
2. 😠 Pisahkan baris berbeda tanpa kurawal. Jangan pernah lakukan, mudah membuat galat saat menambah baris baru:
64
64
```js
65
65
if (n <0)
66
66
alert(`Power ${n} is not supported`);
67
67
```
68
-
3. 😏 One line without braces - acceptable, if it's short:
68
+
3. 😏 Sebaris tanpa kurawal - bisa diterima, jika pendek:
69
69
```js
70
70
if (n <0) alert(`Power ${n} is not supported`);
71
71
```
72
-
4. 😃 The best variant:
72
+
4. 😃 Varian terbaik:
73
73
```js
74
74
if (n <0) {
75
75
alert(`Power ${n} is not supported`);
76
76
}
77
77
```
78
78
79
-
For a very brief code, one line is allowed, e.g.`if (cond) returnnull`. But a code block (the last variant) is usually more readable.
79
+
Untuk kode ringkas, sebaris dibolehkan, misal`if (cond) returnnull`. Tapi blok kode (varian terakhir) biasanya lebih dapat terbaca.
80
80
81
-
### Line Length
81
+
### Panjang Baris
82
82
83
-
No one likes to read a long horizontal line of code. It's best practice to split them.
83
+
Tak ada orang suka membaca kode horizontal yang panjang. Memisahkan mereka ialah praktik yang baik.
84
84
85
-
For example:
85
+
Misalnya:
86
86
```js
87
-
// backtick quotes ` allow to split the string into multiple lines
87
+
// backtick quote ` memperbolehkan memecah string jadi beberapa baris
88
88
let str =`
89
89
Ecma International's TC39 is a group of JavaScript developers,
90
90
implementers, academics, and more, collaborating with the community
91
91
to maintain and evolve the definition of JavaScript.
92
92
`;
93
93
```
94
94
95
-
And, for `if` statements:
95
+
Dan, untuk pernyataan `if`:
96
96
97
97
```js
98
98
if (
@@ -104,23 +104,23 @@ if (
104
104
}
105
105
```
106
106
107
-
The maximum line length should be agreed upon at the team-level. It's usually 80 or 120 characters.
107
+
Panjang baris maximum sebaiknya disepakati di level-tim. Biasanya 80 atau 120 karakter.
108
108
109
-
### Indents
109
+
### Indent
110
110
111
-
There are two types of indents:
111
+
Ada dua tipe indent:
112
112
113
-
- **Horizontal indents: 2 or 4 spaces.**
113
+
- **Indent horizontal: 2 atau 4 spasi.**
114
114
115
-
A horizontal indentation is made using either 2 or 4 spaces or the horizontal tab symbol (key`key:Tab`). Which one to choose is an old holy war. Spaces are more common nowadays.
115
+
Indentasi horizontal dibuat menggunakan 2 atau 4 spasi atau simbol tab horizontal (kunci`key:Tab`). Pemilihan ini sudah lama jadi perang suci. Spasi jadi lebih umum sekarang ini.
116
116
117
-
One advantage of spaces over tabs is that spaces allow more flexible configurations of indents than the tab symbol.
117
+
Satu keutamaan spasi dari tab ialah konfigurasi indent spasi lebih flexibel dari simbol tab.
118
118
119
-
For instance, we can align the arguments with the opening bracket, like this:
119
+
Misalnya, kita bisa mengalinea argumen dengan bracket pembuka, seperti ini:
120
120
121
121
```js no-beautify
122
122
show(parameters,
123
-
aligned, // 5 spaces padding at the left
123
+
aligned, // 5 padding spasi di kiri
124
124
one,
125
125
after,
126
126
another
@@ -129,9 +129,9 @@ There are two types of indents:
129
129
}
130
130
```
131
131
132
-
- **Vertical indents: empty lines for splitting code into logical blocks.**
132
+
- **Indent vertikal: baris kosong untuk memecah kode menjadi blok logika.**
133
133
134
-
Even a single function can often be divided into logical blocks. In the example below, the initialization of variables, the main loop and returning the result are split vertically:
134
+
Bahkan satu fungsi pun sering bisa dibagi jadi blok logika. Di contoh berikut, inisialisasi variabel, loop utama dan hasil kembalian dipecah secara vertikal:
135
135
136
136
```js
137
137
functionpow(x, n) {
@@ -145,46 +145,46 @@ There are two types of indents:
145
145
}
146
146
```
147
147
148
-
Insert an extra newline where it helps to make the code more readable. There should not be more than nine lines of code without a vertical indentation.
148
+
Sisipkan extra baris baru di mana ini membantu membuat kode lebih terbaca. Tak boleh lebih dari sembilan baris kode tanpa indentasi vertikal.
149
149
150
-
### Semicolons
150
+
### Semicolon
151
151
152
-
A semicolon should be present after each statement, even if it could possibly be skipped.
152
+
Semicolon sebaiknya ada di tiap ujung pernyataan, bahkan meski jika itu bisa dilewati.
153
153
154
-
There are languages where a semicolon is truly optional and it is rarely used. In JavaScript, though, there are cases where a line break is not interpreted as a semicolon, leaving the code vulnerable to errors. See more about that in the chapter <info:structure#semicolon>.
154
+
Ada bahasa di mana semicolon benar-benar opsional dan jarang dipakai. Tapi di JavaScript, ada kasus di mana line break tidak diinterpretasi sebagai semicolon, membuat kode rentan galat. Lihat lebih lanjut tentang itu di bab <info:structure#semicolon>.
155
155
156
-
If you're an experienced JavaScript programmer, you may choose a no-semicolon code style like [StandardJS](https://standardjs.com/). Otherwise, it's best to use semicolons to avoid possible pitfalls. The majority of developers put semicolons.
156
+
Jika kamu programmer JavaScript berpengalaman, kamu bisa pilih gaya kode tanpa semicolon seperti [StandardJS](https://standardjs.com/). Atau, lebih baik memakai semicolon untuk menghindari kemungkinan jurang nista. Mayoritas pengembang menaruh semicolon.
157
157
158
-
### Nesting Levels
158
+
### Level Bersarang
159
159
160
-
Try to avoid nesting code too many levels deep.
160
+
Coba hindari kode bersarang dengan level terlalu dalam.
161
161
162
-
For example, in the loop, it's sometimes a good idea to use the [`continue`](info:while-for#continue) directive to avoid extra nesting.
162
+
Misalnya, dalam loop, kadang ide bagus memakai directive [`continue`](info:while-for#continue) untuk menghindari sarang extra.
163
163
164
-
For example, instead of adding a nested `if`conditional like this:
164
+
Misalnya, ketimbang menambah kondisional `if`bersarang seperti ini:
165
165
166
166
```js
167
167
for (let i =0; i <10; i++) {
168
168
if (cond) {
169
-
...// <- one more nesting level
169
+
...// <- satu lagi level bersarang
170
170
}
171
171
}
172
172
```
173
173
174
-
We can write:
174
+
Kita bisa tulis:
175
175
176
176
```js
177
177
for (let i =0; i <10; i++) {
178
178
if (!cond) *!*continue*/!*;
179
-
...// <- no extra nesting level
179
+
...// <- tak ada extra level bersarang
180
180
}
181
181
```
182
182
183
-
A similar thing can be done with `if/else`and`return`.
183
+
Hal serupa bisa dilakukan dengan `if/else`dan`return`.
184
184
185
-
For example, two constructs below are identical.
185
+
Misalnya, dua konstruksi berikut identik.
186
186
187
-
Option 1:
187
+
Opsi 1:
188
188
189
189
```js
190
190
functionpow(x, n) {
@@ -202,7 +202,7 @@ function pow(x, n) {
202
202
}
203
203
```
204
204
205
-
Option 2:
205
+
Opsi 2:
206
206
207
207
```js
208
208
functionpow(x, n) {
@@ -221,16 +221,16 @@ function pow(x, n) {
221
221
}
222
222
```
223
223
224
-
The second one is more readable because the "special case" of `n <0`is handled early on. Once the check is done we can move on to the "main" code flow without the need for additional nesting.
224
+
Yang kedua lebih terbaca karena "kasus spesial" `n <0`ditangani segera. Sekali pengecekan selesai kita bisa pindah ke aliran kode "utama" tanpa sarang tambahan.
225
225
226
-
## Function Placement
226
+
## Penempatan Fungsi
227
227
228
-
If you are writing several "helper" functions and the code that uses them, there are three ways to organize the functions.
228
+
Jika kamu menulis beberapa fungsi "pembantu" dan kode yang menggunakan mereka, ada tiga cara untuk mengorganisir fungsi.
229
229
230
-
1. Declare the functions *above* the code that uses them:
230
+
1. Deklarasi fungsi *di atas* kode yang menggunakan mereka:
231
231
232
232
```js
233
-
// *!*function declarations*/!*
233
+
// *!*deklarasi fungsi*/!*
234
234
functioncreateElement() {
235
235
...
236
236
}
@@ -243,20 +243,20 @@ If you are writing several "helper" functions and the code that uses them, there
243
243
...
244
244
}
245
245
246
-
// *!*the code which uses them*/!*
246
+
// *!*kode yang menggunakan mereka*/!*
247
247
let elem =createElement();
248
248
setHandler(elem);
249
249
walkAround();
250
250
```
251
-
2. Code first, then functions
251
+
2. Kode pertama, lalu fungsi
252
252
253
253
```js
254
-
// *!*the code which uses the functions*/!*
254
+
// *!*kode yang menggunakan mereka*/!*
255
255
let elem =createElement();
256
256
setHandler(elem);
257
257
walkAround();
258
258
259
-
// --- *!*helper functions*/!* ---
259
+
// --- *!*fungsi pembantu*/!* ---
260
260
functioncreateElement() {
261
261
...
262
262
}
@@ -269,54 +269,54 @@ If you are writing several "helper" functions and the code that uses them, there
269
269
...
270
270
}
271
271
```
272
-
3. Mixed: a function is declared where it's first used.
272
+
3. Campuran: fungsi dideklarasi di mana ia pertama dipakai.
273
273
274
-
Most of time, the second variant is preferred.
274
+
Seringnya, varian kedua jadi pilihan.
275
275
276
-
That's because when reading code, we first want to know *what it does*. If the code goes first, then it becomes clear from the start. Then, maybe we won't need to read the functions at all, especially if their names are descriptive of what they actually do.
276
+
Itu karena saat membaca kode, kita pertama mau tahu *itu bisa apa*. Jika kode mulai duluan, maka jadi lebih jelas dari awal. Lalu, mungkin kita tak mau membaca fungsinya sama sekali, terutama jika nama mereka deskriptif, sesuai dengan kelakuan mereka.
277
277
278
-
## Style Guides
278
+
## Panduan Gaya
279
279
280
-
A style guide contains general rules about "how to write" code, e.g. which quotes to use, how many spaces to indent, the maximal line length, etc. A lot of minor things.
280
+
Panguan gaya berisi aturan umum tentang "bagaimana menulis" kode, misal quote mana yang dipakai, berapa spasi indent, panjang baris maximal, dll. Banyak hal remeh.
281
281
282
-
When all members of a team use the same style guide, the code looks uniform, regardless of which team member wrote it.
282
+
Saat semua anggota tim menggunakan panduan gaya yang sama, kodenya terlihat seragam, tak peduli anggota tim mana yang menulisnya.
283
283
284
-
Of course, a team can always write their own style guide, but usually there's no need to. There are many existing guides to choose from.
284
+
Tentu saja, tim bisa saja menulis panduan gaya mereka sendiri, tapi biasanya mereka tak butuh. There are many existing guides to choose from.
If you're a novice developer, start with the cheat sheet at the beginning of this chapter. Then you can browse other style guides to pick up more ideas and decide which one you like best.
294
+
Jika kamu pengembang pemula, mulai dengan cheatsheet di awal bab ini. Lalu kamu bisa menjelajah panduan gaya lain untuk mencari ide lebih dan menentukan mana yang terbaik.
295
295
296
-
## Automated Linters
296
+
## Linter Terotomasi
297
297
298
-
Linters are tools that can automatically check the style of your code and make improving suggestions.
298
+
Linter ialah tool yang bisa otomatis mengecek gaya kodemu dan memberi saran improvisasi.
299
299
300
-
The great thing about them is that style-checking can also find some bugs, like typos in variable or function names. Because of this feature, using a linter is recommended even if you don't want to stick to one particular "code style".
300
+
Yang keren dari ini ialah pengecekan gaya sekaligus menemukan bug., like typos in variable or function names. Because of this feature, using a linter is recommended even if you don't want to stick to one particular "code style".
301
301
302
-
Here are some well-known linting tools:
302
+
Berikut beberapa linting tool terkenal:
303
303
304
304
- [JSLint](http://www.jslint.com/) -- one of the first linters.
305
305
- [JSHint](http://www.jshint.com/) -- more settings than JSLint.
306
306
- [ESLint](http://eslint.org/) -- probably the newest one.
307
307
308
-
All of them can do the job. The author uses [ESLint](http://eslint.org/).
308
+
Semuanya bisa melakukan itu. Penulis ini menggunakan [ESLint](http://eslint.org/).
309
309
310
-
Most linters are integrated with many popular editors: just enable the plugin in the editor and configure the style.
310
+
Kebanyakan linter terintegrasi dengan banyak editor populer: cuma mengaktifkan plugin di editor dan mengkonfigurasi gayanya.
311
311
312
-
For instance, for ESLint you should do the following:
312
+
Misalnya, untuk ESLint kamu harus melakukan hal ini:
313
313
314
-
1. Install [Node.js](https://nodejs.org/).
315
-
2. Install ESLint with the command `npm install -g eslint` (npm is a JavaScript package installer).
316
-
3. Create a config file named `.eslintrc`in the root of your JavaScript project (in the folder that contains all your files).
317
-
4. Install/enable the plugin for your editor that integrates with ESLint. The majority of editors have one.
314
+
1. Instal [Node.js](https://nodejs.org/).
315
+
2. Instal ESLint dengan command `npm install -g eslint` (npm ialah installer package JavaScript).
316
+
3. Buat file konfig bernama `.eslintrc`di root proyek JavaScriptmu (di folder yang berisi semua filemu).
317
+
4. Instal/aktifkan plugin untuk editormu yang berintegrasi dengan ESLint. Mayoritas editor punya satu.
318
318
319
-
Here's an example of an `.eslintrc` file:
319
+
Berikut contoh file `.eslintrc`:
320
320
321
321
```js
322
322
{
@@ -333,16 +333,16 @@ Here's an example of an `.eslintrc` file:
333
333
}
334
334
```
335
335
336
-
Here the directive `"extends"`denotes that the configuration is based on the "eslint:recommended" set of settings. After that, we specify our own.
336
+
Di sini directive `"extends"`menunjukkan bahwa konfigurasinya berdasarkan set pengaturan "eslint:recommended". Setelah itu, kita spesifikasikan punya kita sendiri.
337
337
338
-
It is also possible to download style rule sets from the web and extend them instead. See <http://eslint.org/docs/user-guide/getting-started> for more details about installation.
338
+
Selain itu, dimungkinkan juga mengunduh set aturan gaya dari web dan mengextend mereka. Lihat <http://eslint.org/docs/user-guide/getting-started> untuk detil lebih tentang instalasi.
339
339
340
-
Also certain IDEs have built-in linting, which is convenient but not as customizable as ESLint.
340
+
Juga IDE tertentu punya linting built-in, yang nyaman tapi tak bisa dikustomisasi seperti ESLint.
341
341
342
-
## Summary
342
+
## Kesimpulan
343
343
344
-
All syntax rules described in this chapter (and in the style guides referenced) aim to increase the readability of your code. All of them are debatable.
344
+
Semua aturan syntax yang dideskripsikan di bab ini (dan di referensi panduan gaya) bertujuan untuk meningkatkan keterbacaan kodemu. Mereka dapat diperdebatkan.
345
345
346
-
When we think about writing "better" code, the questions we should ask ourselves are: "What makes the code more readable and easier to understand?" and "What can help us avoid errors?" These are the main things to keep in mind when choosing and debating code styles.
346
+
Saat kita berpikir tentang menulis kode "lebih baik", pertanyaannya ialah haruskah kita tanya diri kita sendiri: "Apa yang membuat kode dapat lebih terbaca dan lebih mudah dipahami?" dan "Apa yang bisa membantu kita menghindari galat?" Inilah hal utama yang harus dipikirkan saat memilih dan memperdebatkan gaya kode.
347
347
348
-
Reading popular style guides will allow you to keep up to date with the latest ideas about code style trends and best practices.
348
+
Membaca panduan gaya populer akan membuatmu selalu terupdate dengan ide terbaru tentang tren gaya kode dan praktik terbaiknya.
0 commit comments