Skip to content

Commit e6f576b

Browse files
fixing TOC adding missing topics
1 parent 8efea72 commit e6f576b

File tree

1 file changed

+56
-49
lines changed

1 file changed

+56
-49
lines changed

Readme.md

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
# Node.js Style Guide
22

3-
This is guide was forked from [Felix Geisendörfer node styleguide](https://github.com/felixge/node-style-guide)
4-
, I adapted it to meet my personal taste and opinion. Please check his original guide
5-
for a view more neutral and less opinionated than mine.
3+
This is a guide for writing consistent and aesthetically pleasing node.js code.
4+
It is inspired by what is popular within the community, and flavored with some
5+
personal opinions.
66

7+
There is a .jshintrc which enforces these rules as closely as possible. You can
8+
either use that and adjust it, or use
9+
[this script](https://gist.github.com/kentcdodds/11293570) to make your own.
710

811
This guide was created by [Felix Geisendörfer](http://felixge.de/) and is
912
licensed under the [CC BY-SA 3.0](http://creativecommons.org/licenses/by-sa/3.0/)
1013
license. You are encouraged to fork this repository and make adjustments
1114
according to your preferences.
12-
![Creative Commons License](http://i.creativecommons.org/l/by-sa/3.0/88x31.png)
1315

16+
![Creative Commons License](http://i.creativecommons.org/l/by-sa/3.0/88x31.png)
1417

1518
## Table of contents
1619

@@ -20,21 +23,27 @@ according to your preferences.
2023
* [Use Semicolons](#use-semicolons)
2124
* [80 characters per line](#80-characters-per-line)
2225
* [Use single quotes](#use-single-quotes)
23-
* [Opening braces go on the line below](#opening-braces-go-on-the-line-below)
26+
* [Opening braces go on the same line](#opening-braces-go-on-the-same-line)
2427
* [Method chaining](#method-chaining)
2528
* [Declare one variable per var statement](#declare-one-variable-per-var-statement)
2629
* [Use lowerCamelCase for variables, properties and function names](#use-lowercamelcase-for-variables-properties-and-function-names)
2730
* [Use UpperCamelCase for class names](#use-uppercamelcase-for-class-names)
28-
* [Use UPPERCASE wrapped by "__" for Constants](#use-uppercase-wrapped-by-__-for-constants)
31+
* [Use UPPERCASE for Constants](#use-uppercase-for-constants)
2932
* [Object / Array creation](#object--array-creation)
30-
* [Use === and == operators wisely](#use-===-and-==-operators-wisely)
31-
* [Avoid to extend built-in prototypes](#avoid-to-extend-built-in-prototypes)
33+
* [Use the === operator](#use-the--operator)
34+
* [Use multi-line ternary operator](#use-multi-line-ternary-operator)
35+
* [Do not extend built-in prototypes](#do-not-extend-built-in-prototypes)
3236
* [Use descriptive conditions](#use-descriptive-conditions)
3337
* [Write small functions](#write-small-functions)
3438
* [Return early from functions](#return-early-from-functions)
3539
* [Name your closures](#name-your-closures)
3640
* [No nested closures](#no-nested-closures)
3741
* [Use slashes for comments](#use-slashes-for-comments)
42+
* [Object.freeze, Object.preventExtensions, Object.seal, with, eval](#objectfreeze-objectpreventextensions-objectseal-with-eval)
43+
* [Getters and setters](#getters-and-setters)
44+
45+
46+
3847

3948
## 2 Spaces for indention
4049

@@ -84,29 +93,29 @@ var foo = 'bar';
8493
var foo = "bar";
8594
```
8695

87-
## Opening braces go on the first line below
96+
## Opening braces go on the same line
97+
98+
Your opening braces go on the same line as the statement.
8899

89100
*Right:*
90101

91102
```js
92-
if (true)
93-
{
103+
if (true) {
94104
console.log('winning');
95105
}
96-
97-
98-
if(true) console.log('ok for one liners');
99-
100106
```
101107

102108
*Wrong:*
103109

104110
```js
105-
if (true){
111+
if (true)
112+
{
106113
console.log('losing');
107114
}
108115
```
109116

117+
Also, notice the use of whitespace before and after the condition statement.
118+
110119
## Method chaining
111120

112121
One method per line should be used if you want to chain methods.
@@ -151,9 +160,12 @@ User.findOne({ name: 'foo' }).populate('bar')
151160
});
152161
````
153162
154-
## Variable declaration
163+
## Declare one variable per var statement
155164
156-
Declare variables as close as possible of the first time you will use it.
165+
Declare one variable per var statement, it makes it easier to re-order the
166+
lines. However, ignore [Crockford][crockfordconvention] when it comes to
167+
declaring variables deeper inside a function, just put the declarations wherever
168+
they make sense.
157169
158170
*Right:*
159171
@@ -182,14 +194,14 @@ while (keys.length) {
182194
}
183195
```
184196

197+
[crockfordconvention]: http://javascript.crockford.com/code.html
185198

186199
## Use lowerCamelCase for variables, properties and function names
187200

188201
Variables, properties and function names should use `lowerCamelCase`. They
189202
should also be descriptive. Single character variables and uncommon
190203
abbreviations should generally be avoided.
191204

192-
193205
*Right:*
194206

195207
```js
@@ -220,7 +232,7 @@ function bank_Account() {
220232
}
221233
```
222234

223-
## Use UPPERCASE wrapped by __ for Constants
235+
## Use UPPERCASE for Constants
224236

225237
Constants should be declared as regular variables or static class properties,
226238
using all uppercase letters.
@@ -236,7 +248,7 @@ var SECOND = 1 * 1000;
236248

237249
function File() {
238250
}
239-
File.__FULL_PERMISSIONS__ = 0777;
251+
File.FULL_PERMISSIONS = 0777;
240252
```
241253

242254
*Wrong:*
@@ -247,7 +259,6 @@ const SECOND = 1 * 1000;
247259
function File() {
248260
}
249261
File.fullPermissions = 0777;
250-
File.FULL_PERMISSIONS = 0777;
251262
```
252263

253264
[const]: https://developer.mozilla.org/en/JavaScript/Reference/Statements/const
@@ -278,34 +289,19 @@ var b = {"good": 'code'
278289
};
279290
```
280291

281-
## Use === and == operators wisely
292+
## Use the === operator
282293

283-
Programming is not about remembering [stupid rules][comparisonoperators].
294+
Programming is not about remembering [stupid rules][comparisonoperators]. Use
295+
the triple equality operator as it will work just as expected.
284296

285297
*Right:*
286298

287299
```js
288-
var a = component.getRandomUserInput();
289-
if (a !== 0) {
290-
console.log('winning');
291-
}
292-
293300
var a = 0;
294-
if (a == 0) {
295-
console.log('winning');
296-
}
297-
298-
//If I'm using Number built-in type, it's ok to use the == operator
299-
var a = new Number('0');
300-
if (a == 0) {
301+
if (a !== '') {
301302
console.log('winning');
302303
}
303304

304-
//Scenario: the value o "a" comes from user input data and we want to test it
305-
var a = parseInt(input, 10);
306-
if (!isNaN(a) && a == 0) {
307-
console.log('winning');
308-
}
309305
```
310306

311307
*Wrong:*
@@ -315,11 +311,6 @@ var a = 0;
315311
if (a == '') {
316312
console.log('losing');
317313
}
318-
319-
var a = 0;
320-
if (a !== 0) {
321-
console.log('losing');
322-
}
323314
```
324315

325316
[comparisonoperators]: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Comparison_Operators
@@ -342,9 +333,10 @@ var foo = (a === b)
342333
var foo = (a === b) ? 1 : 2;
343334
```
344335

345-
## Avoid to extend built-in prototypes
336+
## Do not extend built-in prototypes
346337

347-
Prefer to not extend the prototype of native JavaScript objects.
338+
Do not extend the prototype of native JavaScript objects. Your future self will
339+
be forever grateful.
348340

349341
*Right:*
350342

@@ -438,7 +430,8 @@ further:
438430

439431
```js
440432
function isPercentage(val) {
441-
return (val >= 0 && val <= 100);
433+
var isInRange = (val >= 0 && val <= 100);
434+
return isInRange;
442435
}
443436
```
444437

@@ -532,3 +525,17 @@ if (isSessionValid) {
532525
// ...
533526
}
534527
```
528+
529+
## Object.freeze, Object.preventExtensions, Object.seal, with, eval
530+
531+
Crazy shit that you will probably never need. Stay away from it.
532+
533+
## Getters and setters
534+
535+
Do not use setters, they cause more problems for people who try to use your
536+
software than they can solve.
537+
538+
Feel free to use getters that are free from [side effects][sideeffect], like
539+
providing a length property for a collection class.
540+
541+
[sideeffect]: http://en.wikipedia.org/wiki/Side_effect_(computer_science)

0 commit comments

Comments
 (0)