We follow a variant of the javascript code conventions defined by Douglas Crockford: http://javascript.crockford.com/code.html
Variations and affirmations:
- Indentation:
- We use 2 spaces, and tabs are disused.
- Ensure that braces and parentheses are aligned to the line it opened from.
- Its better to limit a line to 100 characters.
- But when you break, end with an operator, preferably coma.
- The next line should be indented 4 spaces.
- Use the same operator to break a statement.
- If possible ensure that the statement is broken in all occurrences of the operator.
- Variable Declarations:
- Private properties and functions must start with an '_' (underscore). This improves readability.
- Constants and globals should be in all caps.
- Define all variables at the top of the function.
- It is preferred that each variable be given its own line.
- Comment after the variable in the same line using //, and comment where it makes sense.
Better if variables are logically grouped, separated by an empty line.
Comments in a group must start from the same column.
Remember, 4 spaces to intent on line break.
- Alphabetic order is not mandatory.
- Statements:
- Braces should be used around all statements, even single statements, when they are part of a control structure, such as an
if
orfor
statement. - Assignment statement that is assigning a function literal or object literal is still an assignment statement and must end with a semicolon.
- Braces should be used around all statements, even single statements, when they are part of a control structure, such as an
- Files & folders:
- All file & folder names must be in lowercase, with words separated by '-' (hyphen).
- No trailing spaces.
- All files must have apache license headers.
- No unused commented code.
- With js files, would be great if the file name, is similar to the class it represent. Eg: abc-def-controller.js for a file with class AbcDefController.
- Space:
- All binary operators except
.
(period) and(
(left parenthesis) and[
(left bracket) should be separated from their operands by a space. - No space should separate a unary operator and its operand except when the operator is a word such as typeof.
- a >= b, a + b, a ? a : b etc.
- Blank lines improve readability by setting off sections of code that are logically related.
- All binary operators except
- Others:
- Refrain from using with, labels & continue.
- Create jira tickets for each TODOs with the label TEZ-UI-TODO.
- Avoid eval
- Namespace must start with a capital letter.