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
+24-18
Original file line number
Diff line number
Diff line change
@@ -56,21 +56,27 @@ A single-line construct, such as `if (condition) doSomething()`, is an important
56
56
57
57
Here are the annotated variants so you can judge their readability for yourself:
58
58
59
-
<!--
60
-
```js no-beautify
61
-
if (n < 0) {alert(`Power ${n} is not supported`);}
62
-
63
-
if (n < 0) alert(`Power ${n} is not supported`);
64
-
65
-
if (n < 0)
66
-
alert(`Power ${n} is not supported`);
59
+
1. 😠 Beginners sometimes do that. Bad! Curly braces are not needed:
60
+
```js
61
+
if (n <0) *!*{*/!*alert(`Power ${n} is not supported`);*!*}*/!*
62
+
```
63
+
2. 😠 Split to a separate line without braces. Never do that, easy to make an error when adding new lines:
64
+
```js
65
+
if (n <0)
66
+
alert(`Power ${n} is not supported`);
67
+
```
68
+
3. 😏 One line without braces - acceptable, if it's short:
69
+
```js
70
+
if (n <0) alert(`Power ${n} is not supported`);
71
+
```
72
+
4. 😃 The best variant:
73
+
```js
74
+
if (n <0) {
75
+
alert(`Power ${n} is not supported`);
76
+
}
77
+
```
67
78
68
-
if (n < 0) {
69
-
alert(`Power ${n} is not supported`);
70
-
}
71
-
```
72
-
-->
73
-

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.
74
80
75
81
### Line Length
76
82
@@ -106,9 +112,9 @@ There are two types of indents:
106
112
107
113
- **Horizontal indents: 2 or 4 spaces.**
108
114
109
-
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.
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.
110
116
111
-
One advantage of spaces over tabs is that spaces allow more flexible configurations of indents than the "Tab" symbol.
117
+
One advantage of spaces over tabs is that spaces allow more flexible configurations of indents than the tab symbol.
112
118
113
119
For instance, we can align the arguments with the opening bracket, like this:
114
120
@@ -153,7 +159,7 @@ If you're an experienced JavaScript programmer, you may choose a no-semicolon co
153
159
154
160
Try to avoid nesting code too many levels deep.
155
161
156
-
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
+
For example, in the loop, it's sometimes a good idea to use the [`continue`](info:while-for#continue) directive to avoid extra nesting.
157
163
158
164
For example, instead of adding a nested `if` conditional like this:
159
165
@@ -271,7 +277,7 @@ That's because when reading code, we first want to know *what it does*. If the c
271
277
272
278
## Style Guides
273
279
274
-
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.
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.
275
281
276
282
When all members of a team use the same style guide, the code looks uniform, regardless of which team member wrote it.
0 commit comments