-
Notifications
You must be signed in to change notification settings - Fork 259
Coding style guide
This document attempts to explain the basic styles and patterns that are used in the Jetpack codebase. While existing code may not always comply to this style, new code should try to conform to these standards so that it is as easy to maintain as existing code. Of course every rule has an exception, but it's important to know the rules nonetheless!
2-column indentation. 80-character limit for all lines.
Use single quotation symbol '
for strings as they're
easier to type. Although some files be authored in with double
quotes "
, stay consistent with the style use by a file when
making changes to it and use "
. Do not mix two styles in the
same file:
// Good!
let panel = require('panel');
let widget = require('widget');
// Acceptable
let panel = require("panel");
let widget = require("widget");
// Bad
let panel = require('panel');
let widget = require("widget");
Use dots at the end of lines, not the front:
// Good
let browsers = Cc['@mozilla.org/appshell/window-mediator;1'].
getService(Ci.nsIWindowMediator).
getEnumerator('navigator:browser');
// Bad
let browsers = Cc['@mozilla.org/appshell/window-mediator;1']
.getService(Ci.nsIWindowMediator)
.getEnumerator('navigator:browser');
Forget about var
and use let
instead. It ok to use var
if code is going to be used on other run times.
// Good
let n = 5;
// Bad
var n = 5;
For constants use const
and name them LIKE_THIS
.
// Good
const FOO = Math.random();
// Bad
const foo = Math.random();
const Foo = Math.random();
var FOO = Math.random();
let FOO = Math.random();
Do not declare function within blocks.
- Use
camelCase
for functions and variables. - Use capitalized
CamelCase
for classes / constructor functions. - Use all lowercase in order to avoid confusion on case-sensitive platforms.
Filenames should end in
.js
and should contain no punctuation except for-
.
A branch follows its conditional on a new line and is indented:
if (foo)
bar();
If all branches of a conditional are one-liner single statements, no braces needed:
if (foo)
bar();
else if (baz)
beep();
else
qux();
A single-statement branch that requires multiple lines also requires braces. The opening brace follows the conditional on the same line:
if (foo) {
if (bar)
baz();
}
if (foo) {
Cc['@mozilla.org/appshell/window-mediator;1'].
getService(Ci.nsIWindowMediator).
getEnumerator('navigator:browser').
doSomethingOnce();
}
If any branch requires braces, use them for all:
if (foo) {
bar();
}
else {
doThis();
andThat();
}
Do not cuddle else:
// Good
if (foo) {
bar();
baz();
}
else {
qux();
qix();
}
// Bad
if (foo) {
bar();
baz();
} else {
qux();
qix();
}
Use triple equal ===
instead of double ==
unless there
is a reason not to. If in a given case ==
is preferred add
a comment to explain why.
// Good
if (password === secret)
authorize()
else
deny()
// Bad
if (password == secret)
authorize()
else
deny()
Do not compare to booleans unless exactly true
or false
is expected (add comment if that's a case):
// Good
if (x)
doThis()
else if (!y)
doThat()
else
doSomethingElse()
// Bad
if (x === true)
doThis()
else if (y != false)
doThat()
else
doSomethingElse()
Conditional style also applies to loop style.
for (let i = 0; i < arr.len; i++)
arr[i] = 0;
for (let i = 0; i < arr.len; i++) {
if (i % 2)
arr[i] = 0;
}
Do not cuddle catch:
// Good
try {
bar();
}
catch (err) {
baz();
}
// Bad
try {
bar();
} catch (err) {
baz();
}
Reuse functions where possible, creating closures on every call has worse performance and generates more garbage to be GC-ed.
// Good
function isOdd(x) { return x % 2 }
function sum(x, y) { return x + y }
function foo(nums) {
return nums.filter(isOdd).reduce(sum);
}
// Bad
function foo(nums) {
return nums.filter(function(x) {
return x % 2;
}).reduce(function(a, b) {
return a + b;
});
}
This applies to in-source docs only, not nice docs meant for end users.
All exported functions should be documented in JSDoc style. Their purpose is to help people looking at your code.
/**
* This function registers given user.
* @param {String} name
* The name of the user.
* @param {String|Number} id
* Unique user ID.
* @param {String[]} aliases
* Array of aliases user
* @param {String} [accessLevel='user']
* Optional `accessLevel` for a user.
*/
function register(name, id, aliases, accessLevel) {
// ...
}
/**
* Registers user and returns associated ID.
* @param {Object} options
* Information about the user.
* @param {String} options.name
* The name of the user.
* @param {String} [options.aliases]
* Optional array of aliases
*/
function registerUser(options) {
// ...
}
Module internal utility functions don't need to be documented this way, but it's encouraged.
For all other comments use single line comments. If a comment is a full sentence, capitalize and punctuate it. If a comment is almost a full sentence, make it a full sentence. Full sentences are generally preferable to fragments, but fragments are sometimes more effective. Fragments should be very terse. Don't capitalize or punctuate fragments.
Quote identifiers with backticks:
// Returns string content under given `uri`. Throws
// exception if such `uri` does not exists.
function readURI(uri) {
}