This is not intended to be a comprehensive list. If a situation is not described in this guide, use common best practices such as the Google JavaScript Style Guide.
- No tabs. Two spaces.
- No trailing whitespace.
- Attempt to keep lines fewer than 120 characters.
- Always use semicolons and don't rely on implicit insertion.
- Single quotes. Double quotes are OK only when nested within single quotes.
# good
var foo = 'Hello, world.';
var bar = 'Hello, "world."';
# bad
var foo = "Hello, world.";
- Functions are followed by no space.
# good
function foo() {
...
}
# bad
function foo () {
...
}
- Argument definitions are followed by no spaces.
# good
function foo(a, b, c) {
...
}
# bad
function foo( a, b, c ) {
...
}
- A single space will always and should only follow semi-colons when defining object literals.
# good
var foo = {
bar: 1
}
# bad
var foo = {
bar : 1
}
var foo = {
bar:1
}
- A single space will always follow conditional statements.
# good
if (true) {
...
}
# bad
if(true) {
...
}
if(true){
...
}
- Never declare multiple variables with a single
var
statement.
# good
var foo = null;
var bar = null;
# bad
var foo, bar = null;
- Always throw errors, not strings.
# good
throw new Error('foo');
# bad
throw 'foo';
- Comments should explain "why" not "what".
- Less can be more with comments. Encourage yourself to re-read comments after writing them and see if you can convey the same thought more succinctly.
- Custom methods (private or public) should each have a summary unless their function is blindingly obvious.
- Backbone/Marionette method overrides should not have summary comments. For instance,
onRender
does not need a comment explaining that it is called when the view is rendered. - Hacks, workarounds, uncommon patterns, etc. should have an explanation of why they are necessary. Provide links for copy/pasted code source(s).
- Use proper sentences.
- Variable names must not be reserved words or identifiers.
- Method names must not be reserved Backbone/Marionette names, unless the intent is to override the method. Common method names collisions are:
_onCollectionAdd
,_onCollectionRemove
, and_ensureElement
. - Variable names should use camel-casing. Example:
coffeeMug
. - Only constructors and constants should begin with an upper-case letter. Example:
CoffeePot
. - Function names should describe the intent of the function, not how the function is called. Example:
makeCoffee
instead ofpushStartButton
. - Private properties/methods should be prefixed with
_
. Example:_brewCoffee
. - Methods that return a value should be prefixed with
get
. Example:getCoffee
. - Methods that set a property/attribute should be prefixed with
set
. Example:setCoffeeTemperature
. - Booleans should be prefixed with
is
,are
,has
, etc. Example:isCoffeeReady
. - Use "positive" names instead of "negative" names. Example:
isEnabled
instead ofisDisabled
. - jQuery objects should be prefixed with
$
. Example:$coffeeOrderForm
. - Event handlers should begin with
on
, then the event name, then the name of the object. Example:onDrinkCoffee
.
- Marionette Views should be postfixed with 'View'. Example:
CoffeeCupView
. - Marionette Behaviors should be postfixed with
Behavior
. Example:liquidBehavior
. - Marionette Regions should be postfixed with
Region
. Example:cupHolderRegion
. - Backbone Collections should be plural. Example:
CoffeeCups
. - Backbone Models should be singular. Example:
CoffeeBean
.
- Only define one module per file.
- Properties and methods should be private unless they need to be accessed by an external module.
- Prefer Lo-Dash methods over native implementations except when working inside content scripts (which do not have access to Lo-Dash).
- Do not commit blocks of commented-out code.
- Do not commit
console
statements norTODO
statements. Create GitHub issues for TODO statements and reference the issue at the given location.
- Backbone/Marionette modules should be organized in the following order:
- Backbone/Marionette properties.
- Custom properties.
- Backbone/Marionette methods, in the order in which they are called.
- Public methods.
- Private event handlers.
- Private methods.
- Event handlers named according to their event/object should not contain functional code. They should call methods named according to their function.
- Example:
onDrinkCoffee
should calltypeMuchCode
and thencrash
, rather than having the contents of those methods withinonDrinkCoffee
itself.
- Example:
- Custom instance-scope variables should be initialized in
initialize()
(or a custom method called frominitialize
). - Do not reference a view's options outside of
initialize
- Backbone model attributes should be defined in
defaults
. Use a function instead of object fordefaults
if the properties are non-primitive types. - Prefer Marionette Behaviors instead of mixins/inheritance for Marionette views.
- Keep Marionette templates as simple as possible. Never call a function from a template - mix the value into the template via
templateHelpers
- UI events should not directly affect view state. Instead, they should affect Backbone model state, which triggers changes to the view state.
- Use Marionette's
events
,modelEvents
, andcollectionEvents
. - Leverage
bindEntityEvents
when binding several events to a non-model/non-collection object. - Prefer using Backbone's
listenTo()
instead ofon()
. Models and collections may useon()
when listening to their own events. - Use
addEventListener
when binding towindow
. - Prefer declaring event bindings in
initialize
whenever it is practical. $.on()
andaddEventListener
should have a associated$.off()
andremoveEventListener
calls in destructors such asonBeforeDestroy
.- Custom events and triggers should be avoided if possible. There is probably a Backbone/Marionette event that suits your needs.
- Don't use
parseInt
without radix. Example:parseInt(x)
should beparseInt(x, 10)
or_.parseInt(x)
. - Use
_.isUndefined
and/or_.isNull
(and understand the difference). - Use objects as enumerations to define constants. Enumeration values should be strings, not integers, for readability.
- Avoid magic numbers. Assign the numbers to named variables.
- Never use the DOM's state for code flow. Store the state of the DOM in models and/or properties.
setter
methods should not return a value.getter
methods should not change any state.- Globals should never be used except when explicitly needed for interfacing with third-party libraries.
- Always oblige JSCS, JSHint, and Recess. If you disagree with a coding style rule then talk to @MeoMix.
- LESS/CSS style guidelines, inspired by: https://medium.com/@fat/mediums-css-is-actually-pretty-fucking-good-b8e2a6c78b06
- Example View/Models