Merge pull request #1762 from NativeScript/hdeshev/code-convention-else

Update coding conventions doc for `else/throw` clauses
This commit is contained in:
Hristo Deshev
2016-03-15 16:45:21 +02:00

View File

@@ -72,7 +72,7 @@ Your opening braces go on the same line as the statement.
~~~ {.javascript} ~~~ {.javascript}
if (true) { if (true) {
console.log('winning'); console.log('winning');
} }
~~~ ~~~
@@ -81,12 +81,35 @@ if (true) {
~~~ {.javascript} ~~~ {.javascript}
if (true) if (true)
{ {
console.log('losing'); console.log('losing');
} }
~~~ ~~~
Also, notice the use of whitespace before and after the condition statement. Also, notice the use of whitespace before and after the condition statement.
Follow the JavaScript convention of stacking `else/catch` clauses on the same line as the previous closing brace.
*Right:*
~~~ {.javascript}
if (i % 2 === 0) {
console.log('even');
} else {
console.log('odd');
}
~~~
*Wrong:*
~~~ {.javascript}
if (i % 2 === 0) {
console.log('even');
}
else {
console.log('odd');
}
~~~
## Variable declarations ## Variable declarations
Declare one variable per var statement. Try to put those declarations at the beginning of each scope. Declare one variable per var statement. Try to put those declarations at the beginning of each scope.
@@ -244,7 +267,7 @@ Use the [strict comaprison operators][comparisonoperators]. The triple equality
~~~ {.javascript} ~~~ {.javascript}
var a = 0; var a = 0;
if (a === '') { if (a === '') {
console.log('winning'); console.log('winning');
} }
~~~ ~~~
@@ -254,7 +277,7 @@ if (a === '') {
~~~ {.javascript} ~~~ {.javascript}
var a = 0; var a = 0;
if (a == '') { if (a == '') {
console.log('losing'); console.log('losing');
} }
~~~ ~~~
@@ -286,7 +309,7 @@ Always use curly braces even in the cases of one line conditional operations.
~~~ {.javascript} ~~~ {.javascript}
if (a) { if (a) {
return 'winning'; return 'winning';
} }
~~~ ~~~
@@ -295,8 +318,8 @@ if (a) {
~~~ {.javascript} ~~~ {.javascript}
if (a) if (a)
return 'winning'; return 'winning';
if (a) return 'winning'; if (a) return 'winning';
~~~ ~~~
@@ -309,11 +332,11 @@ if (a) return 'winning';
~~~ {.javascript} ~~~ {.javascript}
if(condition) { if(condition) {
console.log('winning'); console.log('winning');
} }
if (!condition) { if (!condition) {
console.log('winning'); console.log('winning');
} }
~~~ ~~~
@@ -323,15 +346,15 @@ if (!condition) {
~~~ {.javascript} ~~~ {.javascript}
if(condition === true) { if(condition === true) {
console.log('losing'); console.log('losing');
} }
if(condition !== true) { if(condition !== true) {
console.log('losing'); console.log('losing');
} }
if(condition !== false) { if(condition !== false) {
console.log('losing'); console.log('losing');
} }
~~~ ~~~
@@ -349,7 +372,7 @@ Do not use the **Yoda Conditions** when writing boolean expressions:
~~~ {.javascript} ~~~ {.javascript}
var num; var num;
if(num >= 0) { if(num >= 0) {
console.log('winning'); console.log('winning');
} }
~~~ ~~~
@@ -358,14 +381,14 @@ if(num >= 0) {
~~~ {.javascript} ~~~ {.javascript}
var num; var num;
if(0 <= num) { if(0 <= num) {
console.log('losing'); console.log('losing');
} }
~~~ ~~~
**NOTE** It is OK to use constants on the left when comparing for a range. **NOTE** It is OK to use constants on the left when comparing for a range.
~~~ {.javascript} ~~~ {.javascript}
if(0 <= num && num <= 100) { if(0 <= num && num <= 100) {
console.log('winning'); console.log('winning');
} }
~~~ ~~~
@@ -385,22 +408,22 @@ as possible. In certain routines, once you know the answer, you want to return i
~~~ {.javascript} ~~~ {.javascript}
function getSomething(val) { function getSomething(val) {
if (val < 0) { if (val < 0) {
return false; return false;
} }
if (val > 100) { if (val > 100) {
return false; return false;
} }
var res1 = doOne(); var res1 = doOne();
var res2 = doTwo(); var res2 = doTwo();
var options = { var options = {
a: 1, a: 1,
b: 2 b: 2
}; };
var result = doThree(res1, res2, options); var result = doThree(res1, res2, options);
return result; return result;
} }
~~~ ~~~