DevTools/CodingStandards
Be consistent within a file!
Historical Codestyle
- 80 character JS. Go longer if you need to, but try to keep it to 80.
- 2 spaces for indents (no tabs!)
- aim for 24 lines max per function
- aArguments aAre the aDevil (don't use them please)
- Would take a patch to bulk delete aArguments
/**
* Javadoc Comments
* @param Object with a little prose describing type in more detail
* avoid {Object|arg} syntaxy descriptions
* - javadoc type ordering suggestion?
* - I suggest type first
* - been using closure compiler style:
* @param {Type} varname Description
* - https://developers.google.com/closure/compiler/docs/js-for-compiler#tag-param
*/
Now the Good Stuff
"use strict"; !
semicolons; // use them
Function names (tags, signatures) sparingly (function declarations vs. function expressions: http://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/) ...vs expression closures, vs. arrow expressions, vs life
- let's dead this discussion ^
Promise.jsm (use that, some bugs still exist?) - bug 881050
- DOM Promises are the longterm winner, we should consider migrating to them
- If you're using SDK promises, make sure usage works when resolution runs on next tick (like Promise.jsm)
HOW DO WE FORMAT .THENS? return foo().then(v => {
return foo;
}).then(v2 => {
return thing;
}).then(v3 => {
return thing;
}).then(null, console.error); return foo.promise
.then(() => ... ) .then(() => ... ) .then(null, console.error) ?
Task.jsm (great for tests) yield ...; yield ...;
DON'T use Mozilla extensions when ES6 alternative is available.
- no for each (dcamp will accept patches)
- this also affects function() ... syntax (function expressions)
- don't use moz-specific `function () implicitReturnVal`
- no get foo() this.bar
- it does NOT affect arrow function syntax
- getters / setters require { }!
Use maps instead of hashes when possible.
new code should be common.js modules not jsms.
comma first lololol no. (go to hell) comma middle (gt,fo)
Imports
const { foo, bar } = require("foo/bar"); const { foo, bar } = Cu.import("…", {}); ++
to import only specific, explicitly-declared symbols into your namespace from a JavaScript Module (JSM)
function setBreakpoint({url, line, column}) { ... } is the shizz++ `(...args) => { }` rest args are awesome, no need for `arguments` destructuring needs () in arrow functions but don't go crazy with function foo({ bar: { baz: { lol: wtf } } }) Maybe just use an automatic code formatter and be done with it?