- Spacing
- Objects and Array Expressions
- Multi-line Statements
- Chained Method Calls
- Full File Closures
- Constructors
- Equality
- Type Checks
- Comments
- Quotes
- Semicolons
- Naming Conventions
- Global Variables
- Switch Statements
- Indent using tabs.
- No whitespace at the end of line or on blank lines.
- Lines should be no longer than 80 characters, and must not exceed 100 (counting tabs as 4 spaces). There are 2 exceptions, both allowing the line to exceed 100 characters:
- If the line contains a comment with a long URL.
- If the line contains a regex literal. This prevents having to use the regex constructor which requires otherwise unnecessary string escaping.
if/else/for/whiledo not need to have braces if it's scope content is on one line.tryalways have braces and go on multiple lines.- The
?and:in a ternary conditional must have space on both sides. - New line at the end of each file.
- If the entire file is wrapped in a closure, the function body is not indented.
Object and array expressions can be on one line if they are short (remember the line length limits). When an expression is too long to fit on one line, there must be one property or element per line. Property names only need to be quoted if they are reserved words or contain special characters.
When a statement is too long to fit on one line, line breaks must occur after an operator. When a conditional is too long to fit on one line, successive lines must be indented one extra level to distinguish them from the body.
When a chain of method calls is too long to fit on one line, there must be one call per line, with the first call on a separate line from the object the methods are called on. If the method changes the context, an extra level of indentation must be used.
When a entire file is wrapped in a closure / AMD wrapper, the body of the closure is not indented.
Constructor functions should always be invoked with argument lists, even when such lists are empty.
Strict equality checks (===) must be used in favor of abstract equality checks (==).
The only exception is when checking for undefined and null by way of null.
- String
typeof object === 'string' - Number
typeof object === 'number' - Boolean
typeof object === 'boolean' - Object
typeof object === 'object' - Plain Object
$.isPlainObject(object) - Function
typeof object === 'function' - Array
$.isArray(object) - null
object === null - null or undefined
object == null - undefined
- Global variables
typeof object === 'undefined' - Local variables
object === undefined - Properties
object.prop === undefined
- Global variables
Comments start with a capital first letter and don't require a period at the end. There must be a single space between the comment token and the comment text.
Single line comments go over the line they refer to.
- The Plug-It team uses single quotes.
- Strings that require inner quoting must use single outside and double inside.
- Always use semicolons.
Variable and function names should be full words, using camel case with a lowercase first letter.
Names should be descriptive but not excessively so.
Exceptions are allowed for iterators, such as the use of i to represent the index in a loop.
Each project may expose at most one global variable.
Only use switch statements if there are a large number of cases or when fall-through would give an advantage.
When using switch statements:
- Use a
breakorreturnfor each case other thandefault. - Indent
casestatements from theswitchstatement.