mirror of
https://github.com/goldbergyoni/nodebestpractices.git
synced 2025-10-31 01:28:30 +08:00
Update README.md
This commit is contained in:
54
README.md
54
README.md
@ -24,7 +24,7 @@
|
|||||||
1. [Project structure Practices (5)](#1-project-structure-practices)
|
1. [Project structure Practices (5)](#1-project-structure-practices)
|
||||||
2. [Error Handling Practices (11) ](#2-error-handling-practices)
|
2. [Error Handling Practices (11) ](#2-error-handling-practices)
|
||||||
3. [Code Style Practices (9) ](#3-code-style-practices)
|
3. [Code Style Practices (9) ](#3-code-style-practices)
|
||||||
4. [Testing And Overall Quality Practices (6) ](#4-testing-practices)
|
4. [Testing And Overall Quality Practices (8) ](#4-testing-and-overall-quality-practices)
|
||||||
5. [Going To Production Practices (17) ](#5-going-to-production-practices)
|
5. [Going To Production Practices (17) ](#5-going-to-production-practices)
|
||||||
6. Security Practices (soon)
|
6. Security Practices (soon)
|
||||||
7. Performance Practices (soon)
|
7. Performance Practices (soon)
|
||||||
@ -228,13 +228,57 @@ Javascript's interpeter auto adds semicolon at the end of a statement if there i
|
|||||||
|
|
||||||
<br/><br/>
|
<br/><br/>
|
||||||
|
|
||||||
## ✔ 3.4 Don't start a codeblock in a new line
|
## ✔ 3.4 Start a Codeblock's Curly Braces in the Same Line
|
||||||
|
|
||||||
**TL;DR:** The opening curly braces of a code block should be in the same line of the opening statement. This is the common practice in pretty much every JS style guide, including Douglas Crockford's [Code Conventions for the JavaScript Programming Language](http://javascript.crockford.com/code.html).
|
The opening curly braces of a code block should be in the same line of the opening statement.
|
||||||
|
Javascript's interpeter auto adds semicolon at the end of a statement if there isn't one. This can lead to some undesired results.
|
||||||
|
|
||||||
**Otherwise:** Javascript's interpeter auto adds semicolon at the end of a statement if there isn't one. This can lead to some undesired results.
|
Recommended:
|
||||||
|
```javascript
|
||||||
|
function doSomthing() {
|
||||||
|
// code here
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
🔗 [**Further reading: "Why does a results vary based on curly brace placement?" (Stackoverflow)](https://stackoverflow.com/questions/3641519/why-does-a-results-vary-based-on-curly-brace-placement)
|
Avoid:
|
||||||
|
```javascript
|
||||||
|
function doSomthing()
|
||||||
|
{
|
||||||
|
// code here
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Example:
|
||||||
|
See the following code:
|
||||||
|
```javascript
|
||||||
|
function doSomething() {
|
||||||
|
return
|
||||||
|
{
|
||||||
|
key : "value"
|
||||||
|
};
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
In this example, you would expect the `doSomething()` function to return the object `{key: "value"}`. However, the function will actually not return anything! This is why:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
function doSomething() {
|
||||||
|
return; // <<= this semicolon is inserted autumatically
|
||||||
|
{
|
||||||
|
key : "value"
|
||||||
|
};
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
A semicolong is inserted automatically after the `return`. To avoid that, the opening curly brace should be right after it and not in a new line:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
function doSomething() {
|
||||||
|
return {
|
||||||
|
key : "value"
|
||||||
|
};
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<br/><br/>
|
<br/><br/>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user