From b987a9945cf11c0b6ff05614d77529dea2cd6262 Mon Sep 17 00:00:00 2001 From: Janne Kuuskeri Date: Thu, 27 Feb 2014 15:19:07 +0200 Subject: [PATCH 1/3] Updated styleguide to better match our customs --- README.md | 69 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index b7d4043297..0ff38a8975 100644 --- a/README.md +++ b/README.md @@ -635,6 +635,7 @@ ## Comments - Use `/** ... */` for multiline comments. Include a description, specify types and values for all parameters and return values. + - Use [JSDoc](http://usejsdoc.org) ```javascript // bad @@ -655,8 +656,8 @@ * make() returns a new element * based on the passed in tag name * - * @param tag - * @return element + * @param {String} tag + * @return {Element} element */ function make(tag) { @@ -696,21 +697,7 @@ } ``` - - Prefixing your comments with `FIXME` or `TODO` helps other developers quickly understand if you're pointing out a problem that needs to be revisited, or if you're suggesting a solution to the problem that needs to be implemented. These are different than regular comments because they are actionable. The actions are `FIXME -- need to figure this out` or `TODO -- need to implement`. - - - Use `// FIXME:` to annotate problems - - ```javascript - function Calculator() { - - // FIXME: shouldn't use a global here - total = 0; - - return this; - } - ``` - - - Use `// TODO:` to annotate solutions to problems + - Use `@todo` to annotate todos ```javascript function Calculator() { @@ -727,7 +714,7 @@ ## Whitespace - - Use soft tabs set to 2 spaces + - Use soft tabs set to 4 spaces ```javascript // bad @@ -742,7 +729,7 @@ // good function() { - ∙∙var name; + ∙∙∙∙var name; } ``` @@ -771,6 +758,20 @@ breed: 'Bernese Mountain Dog' }); ``` + + - Place 1 space after keywords (such as `if`, `for` and `function`). + + ```javascript + // bad + var foo = function() { + alert('hello'); + }; + + // good + var foo = function () { + alert('hello'); + }; + ``` - Set off operators with spaces. @@ -1066,7 +1067,7 @@ this._firstName = 'Panda'; ``` - - When saving a reference to `this` use `_this`. + - When saving a reference to `this` use `me`. ```javascript // bad @@ -1079,26 +1080,44 @@ // bad function() { - var that = this; + var _this = this; return function() { - console.log(that); + console.log(_this); }; } // good function() { - var _this = this; + var me = this; return function() { - console.log(_this); + console.log(me); }; } ``` + - When implementing the JavaScript module pattern use `that` for referencing + the "public" interface. + + ```javascript + + var mymodule = (function mymodule() { + var that = {}, + privateCounter = 0; + + that.increment = function increment() { + privateCounter += 1; + return privateCounter; + }; + + return that; + })(); + ``` + - Name your functions. This is helpful for stack traces. ```javascript // bad - var log = function(msg) { + var log = function (msg) { console.log(msg); }; From ed7c0e52ad8447c3de665e795d2ed18dc0aa1d22 Mon Sep 17 00:00:00 2001 From: Janne Kuuskeri Date: Tue, 1 Apr 2014 19:52:43 +0300 Subject: [PATCH 2/3] Added conventions for `else` and `catch` blocks. --- README.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/README.md b/README.md index f8c0be49f2..7db0f850ad 100644 --- a/README.md +++ b/README.md @@ -624,6 +624,42 @@ } ``` + - Put `else` and `catch` on the same line with closing brace. + + ```javascript + // bad + if (test) { + return true; + } + else { + return false; + } + + // good + if (test) { + return 1; + } else if (anotherTest) { + return 0; + } else { + return -1; + } + + // bad + try { + dragonsBeHere(); + } + catch (e) { + log.error('very bad: ' + e); + } + + // good + try { + dragonsBeHere(); + } catch (e) { + log.error('very bad: ' + e); + } + ``` + **[⬆ back to top](#table-of-contents)** From 921ca56fa107e0a0248c41663fffaa34a334a37e Mon Sep 17 00:00:00 2001 From: Janne Kuuskeri Date: Wed, 23 Apr 2014 17:35:09 +0300 Subject: [PATCH 3/3] removed else if as suggested --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 425b7ba952..3e2bba6486 100644 --- a/README.md +++ b/README.md @@ -638,8 +638,6 @@ // good if (test) { return 1; - } else if (anotherTest) { - return 0; } else { return -1; }