From a4c69f9c8d7ad15245d8d25c8724d8ab0e68a188 Mon Sep 17 00:00:00 2001 From: idori Date: Sat, 14 Oct 2017 23:19:32 +0300 Subject: [PATCH] added some code styles --- README.md | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 67 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 15fa688d..13ef5372 100644 --- a/README.md +++ b/README.md @@ -102,19 +102,81 @@ Text here... ## ✔ Don't Forget the Semicolon -Text here... +Javascript's interpeter auto adds semicolon at the end of a statement if there isn't one. This can lead to some undesired results.

## ✔ Start a Codeblock's Curly Braces in the Same Line -Text here... +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. + +Recommended: +```javascript +function doSomthing() { + // code here +} +``` + +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 will 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 this, the curly braces should be right after it and not in a new line: + +```javascript +function doSomething() { + return { + key : "value" + }; +} +```

## ✔ Name Your Functions -Text here... +This is especially useful when profiling a node app. Naming all functions will allow you to easily understand what you're looking at when cheking a memory dump. + +Recommended: +```javascript +getSomeData(aParameter, function handleRespone(response) { + // ... +}); +``` + +Avoid: +```javascript +getSomeData(parameters, function(response) { + // ... +}); +```

@@ -124,9 +186,9 @@ Text here...

-## ✔ Put All `Require`s at the top +## ✔ Put All Requires at the top -Text here... +This simple best practice will help you easily and quickly tell the dependencies of a file right at the beginning.