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/02-coding-style/article.md
+54-64
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,12 @@
1
-
# Coding style
1
+
# Coding Style
2
2
3
3
Our code must be as clean and easy to read as possible.
4
4
5
-
That is actually an art of programming -- to take a complex task and code it in a way that is both correct and human-readable.
6
-
7
-
One thing to help is the good code style.
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.
8
6
9
7
## Syntax
10
8
11
-
A cheatsheet with the rules (more details below):
9
+
Here is a cheatsheet with some suggested rules (see below for more details):
12
10
13
11

14
12
<!--
@@ -38,13 +36,13 @@ if (n < 0) {
38
36
39
37
Now let's discuss the rules and reasons for them in detail.
40
38
41
-
Nothing is "carved in stone" here. Everything is optional and can be changed: these are coding rules, not religious dogmas.
42
-
43
-
### Curly braces
39
+
```warn header="Irony Detected"
40
+
Nothing is set in stone here. These are style preferences, not religious dogmas.
41
+
```
44
42
45
-
In most JavaScript projects curly braces are written on the same line as the corresponding keyword, not on the new line, a so-called "Egyptian" style. There's also a space before an opening bracket.
43
+
### Curly Braces
46
44
47
-
Like this:
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:
48
46
49
47
```js
50
48
if (condition) {
@@ -56,7 +54,7 @@ if (condition) {
56
54
57
55
A single-line construct is an important edge case. Should we use brackets at all? If yes, then where?
58
56
59
-
Here are the annotated variants, so you can judge about their readability on your own:
57
+
Here are the annotated variants so you can judge their readability for yourself:
60
58
61
59
<!--
62
60
```js no-beautify
@@ -74,21 +72,21 @@ if (n < 0) {
74
72
-->
75
73

76
74
77
-
As a summary:
78
-
- For a really short code, one line is acceptable: like`if (cond) return null`.
79
-
- But a separate line for each statement in brackets is usually better.
75
+
In summary:
76
+
- For very short code, one line is acceptable. For example:`if (cond) return null`.
77
+
- But a separate line for each statement in brackets is usually easier to read.
80
78
81
-
### Line length
79
+
### Line Length
82
80
83
-
The maximal line length should be limited. No one likes to eye-follow a long horizontal line. It's better to split it.
81
+
No one likes to read a long horizontal line of code. It's best practice to split them up and limit the length of your lines.
84
82
85
-
The maximal line length is agreed on the team-level. It's usually 80 or 120 characters.
83
+
The maximum line length should be agreed upon at the team-level. It's usually 80 or 120 characters.
86
84
87
85
### Indents
88
86
89
87
There are two types of indents:
90
88
91
-
-**A horizontal indent: 2(4) spaces.**
89
+
-**Horizontal indents: 2 or 4 spaces.**
92
90
93
91
A horizontal indentation is made using either 2 or 4 spaces or the "Tab" symbol. Which one to choose is an old holy war. Spaces are more common nowadays.
94
92
@@ -107,9 +105,9 @@ There are two types of indents:
107
105
}
108
106
```
109
107
110
-
-**A vertical indent: empty lines for splitting code into logical blocks.**
108
+
-**Vertical indents: empty lines for splitting code into logical blocks.**
111
109
112
-
Even a single function can often be divided in logical blocks. In the example below, the initialization of variables, the main loop and returning the result are split vertically:
110
+
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:
113
111
114
112
```js
115
113
functionpow(x, n) {
@@ -125,21 +123,21 @@ There are two types of indents:
125
123
126
124
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.
127
125
128
-
### A semicolon
126
+
### Semicolons
129
127
130
-
A semicolon should be present after each statement. Evenif it could possibly be skipped.
128
+
A semicolon should be present after each statement, evenif it could possibly be skipped.
131
129
132
-
There are languages where a semicolon is truly optional. It's rarely used there. But in JavaScriptthere are few cases when a line break is sometimes not interpreted as a semicolon. That leaves a place for programming errors.
130
+
There are languages where a semicolon is truly optional and it is rarely used. InJavaScript, though, there are cases where a line break is not interpreted as a semicolon, leaving the code vulnerable to errors.
133
131
134
-
As you become more mature as a programmer, you may choose a no-semicolon style, like [StandardJS](https://standardjs.com/), but that's only when you know JavaScript well and understand possible pitfalls.
132
+
As you become more mature as a programmer, you may choose a no-semicolon style like [StandardJS](https://standardjs.com/). Until then, it's best to use semicolons to avoid possible pitfalls.
135
133
136
-
### Nesting levels
134
+
### Nesting Levels
137
135
138
-
There should not be too many nesting levels.
136
+
Try to avoid nesting code too many levels deep.
139
137
140
-
Sometimes it's a good idea to use the ["continue"](info:while-for#continue) directive in the loop to evade extra nesting in `if(..) { ... }`:
138
+
Sometimes it's a good idea to use the ["continue"](info:while-for#continue) directive in a loop to avoid extra nesting.
141
139
142
-
Instead of:
140
+
For example, instead of adding a nested `if` conditional like this:
143
141
144
142
```js
145
143
for (let i = 0; i < 10; i++) {
@@ -162,7 +160,7 @@ A similar thing can be done with `if/else` and `return`.
162
160
163
161
For example, two constructs below are identical.
164
162
165
-
The first one:
163
+
Option 1:
166
164
167
165
```js
168
166
function pow(x, n) {
@@ -180,7 +178,7 @@ function pow(x, n) {
180
178
}
181
179
```
182
180
183
-
And this:
181
+
Option 2:
184
182
185
183
```js
186
184
function pow(x, n) {
@@ -199,13 +197,13 @@ function pow(x, n) {
199
197
}
200
198
```
201
199
202
-
...But the second one is more readable, because the "edge case" of `n < 0` is handled early on, and then we have the "main" code flow, without an additional nesting.
200
+
The second one is more readable because the "edge 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.
203
201
204
-
## Functions below the code
202
+
## Function Placement
205
203
206
-
If you are writing several "helper" functions and the code to use them, then there are three ways to place them.
204
+
If you are writing several "helper" functions and the code that uses them, there are three ways to organize the functions.
207
205
208
-
1. Functions above the code that uses them:
206
+
1. Functions declared above the code that uses them:
209
207
210
208
```js
211
209
// *!*function declarations*/!*
@@ -235,7 +233,6 @@ If you are writing several "helper" functions and the code to use them, then the
235
233
walkAround();
236
234
237
235
// --- *!*helper functions*/!* ---
238
-
239
236
function createElement() {
240
237
...
241
238
}
@@ -248,55 +245,54 @@ If you are writing several "helper" functions and the code to use them, then the
248
245
...
249
246
}
250
247
```
251
-
3. Mixed: a function is described where it's first used.
248
+
3. Mixed: a function is declared where it's first used.
252
249
253
250
Most of time, the second variant is preferred.
254
251
255
-
That's because when reading a code, we first want to know "what it does". If the code goes first, then it provides that information. And then maybe we won't need to read functions at all, especially if their names are adequate to what they're doing.
252
+
That's because when reading code, we first want to know *what it does*. If the code goes first, then it provides that information. Then, maybe we won't need to read the functions at all, especially if their names are descriptive of what they actually do.
256
253
257
-
## Style guides
254
+
## Style Guides
258
255
259
-
A style guide contains general rules about "how to write": which quotes to use, how many spaces to indent, where to put line breaks, etc. A lot of minor things.
256
+
A style guide contains general rules about "how to write" code, e.g.which quotes to use, how many spaces to indent, where to put line breaks, etc. A lot of minor things.
260
257
261
-
In total, when all members of a team use the same style guide, the code looks uniform. No matter who of the team wrote it, it's still the same style.
258
+
When all members of a team use the same style guide the code tends looks uniform, regardless ofwhich team member wrote it.
262
259
263
-
Surely, a team may think out a style guidethemselves. But as ofnow, there's no need to. There are many tried, worked-out style guides, which are easy to adopt.
260
+
Of course, a team can always write their own style guide. Mostofthe time though, there's no need to. There are many existing tried and true options to choose from, so adopting one of these is usually your best bet.
If you're a novice developer, then you could start with the cheatsheet above inthe chapter, and later browse the style guides to pick up the common principles and maybe choose one.
270
+
If you're a novice developer, start with the cheatsheet at the beginning ofthischapter. Once you've mastered that you can browse other style guides to pick up common principles and decide which one you like best.
274
271
275
-
## Automated linters
272
+
## Automated Linters
276
273
277
-
There are tools that can check the code style automatically. They are called "linters".
274
+
Linters are tools that can automatically check the style of your code and make suggestions for refactoring.
278
275
279
-
The great thing about them is that style-checking also finds some bugs, like a typo ina variable or functionname.
276
+
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, installing a linter is recommended even if you don't want to stick to one particular "code style".
280
277
281
-
So it's recommended to install one, even if you don't want to stick to a "code style". They help to find typos -- and that's already good enough.
282
-
283
-
Most well-known tools are:
278
+
Here are the most well-known linting tools:
284
279
285
280
- [JSLint](http://www.jslint.com/) -- one of the first linters.
286
281
- [JSHint](http://www.jshint.com/) -- more settings than JSLint.
287
282
- [ESLint](http://eslint.org/) -- probably the newest one.
288
283
289
284
All of them can do the job. The author uses [ESLint](http://eslint.org/).
290
285
291
-
Most linters are integrated with editors: just enable the plugin in the editor and configure the style.
286
+
Most linters are integrated withmany popular editors: just enable the plugin in the editor and configure the style.
292
287
293
288
For instance, for ESLint you should do the following:
294
289
295
290
1. Install [Node.JS](https://nodejs.org/).
296
291
2. Install ESLint with the command `npm install -g eslint` (npm is a JavaScript package installer).
297
292
3. Create a config file named `.eslintrc`in the root of your JavaScript project (in the folder that contains all your files).
293
+
4. Install/enable the plugin for your editor that integrates withESLint. The majority of editors have one.
298
294
299
-
Here's an example of `.eslintrc`:
295
+
Here's an example of an `.eslintrc` file:
300
296
301
297
```js
302
298
{
@@ -313,22 +309,16 @@ Here's an example of `.eslintrc`:
313
309
}
314
310
```
315
311
316
-
Here the directive `"extends"` denotes that we base on the "eslint:recommended" set of settings, and then we specify our own.
317
-
318
-
Then install/enable the plugin for your editor that integrates with ESLint. The majority of editors have it.
319
-
320
-
It is 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.
321
-
322
-
Using a linter has a great side-effect: linters catch typos. For instance, when an undefined variable is accessed, a linter detects it and (if integrated with an editor) highlights it. In most cases that's a mistype. So we can fix it right ahead.
312
+
Here the directive `"extends"` denotes that the configuration is based on the "eslint:recommended" set of settings. After that, we specify our own.
323
313
324
-
For that reason even if you're not concerned about styles, using a linter is highly recommended.
314
+
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.
325
315
326
-
Also certain IDEs support built-in linting, that also may be good, but not so tunable as ESLint.
316
+
Also certain IDEs have built-in linting, which is convenient but not as customizable as ESLint.
327
317
328
318
## Summary
329
319
330
-
All syntax rules from this chapter and the style guides aim to increase readability, so all of them are debatable.
320
+
All syntax rules described in this chapter (and in the style guides referenced) aim to increase the readability of your code, but all of them are debatable.
331
321
332
-
When we think about "how to write better?", the sole criterion is "what makes the code more readable and easier to understand? what helps to avoid errors?" That's the main thing to keep in mind when choosing the style or discussing which one is better.
322
+
When we think about writing "better" code, the questions we should ask 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.
333
323
334
-
Read style guides to see the latest ideas about that and follow those that you find the best.
324
+
Reading popular style guides will allow you to keep up to date with the latest ideas about code style trends and best practices.
0 commit comments