diff --git a/README.md b/README.md index 3409c68ccd..3e2bba6486 100644 --- a/README.md +++ b/README.md @@ -624,12 +624,47 @@ } ``` + - 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 { + 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)** ## 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 @@ -650,8 +685,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) { @@ -691,21 +726,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() { @@ -722,7 +743,7 @@ ## Whitespace - - Use soft tabs set to 2 spaces + - Use soft tabs set to 4 spaces ```javascript // bad @@ -737,7 +758,7 @@ // good function() { - ∙∙var name; + ∙∙∙∙var name; } ``` @@ -766,6 +787,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. @@ -1061,7 +1096,7 @@ this._firstName = 'Panda'; ``` - - When saving a reference to `this` use `_this`. + - When saving a reference to `this` use `me`. ```javascript // bad @@ -1074,26 +1109,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); };