MathLib JS-Styleguide

Code formatting

Curly braces
Their bodies should have their own lines. There is a space between keyword and ) as well as between ) and {. if (arr[i] < 0) { // Do something } else { // Do something else }
else statements start on a new line.
if (n = 0) { // Do something } else { // Do something else } instead of if (n = 0) { // Do something } else { // Do something else }

Semicolons, Commas, Braces and Quotes

Use semicolons. asi: false
Always use semicolons, even if they can be left away due to automatic semicolon insertion.
Commas at the end "requireCommaBeforeLineBreak": true
A comma should be the laste toke in a line, if the line break is in a list. Use a = { a: 1, b: 2 } instead of a = { a: 1 , b: 2 }
Curly braces requireCurlyBraces: ["if", "else", "for", "while", "do", "switch"]
Loops and conditionals (if, else, for, while, do, switch) always have curly braces. Use if (n === 0) { // Single statement } instead of if (n === 0) // Single statement
Spaces in function expression "requireSpacesInFunctionExpression": {...},
Function expressions have a space before the opening round brace and before the opening curly brace. Use function name () { return 42; } instead of function name(){ return 42; }
Use single quotes for JavaScript strings validateQuoteMarks: "'"
Double quotes are only used for wrapping HTML attributes: function toContentMathML(n) { return '' + n.toString() + ''; }

White space

Indentation with tabs. validateIndentation: "\t"
Every tab character equals two spaces.
Don't mix spaces and tabs disallowMixedSpacesAndTabs: true
No end of line whitespace disallowTrailingWhitespace: true
This also means, that there is no whitespace on blank lines.
Space after keywords requireSpaceAfterKeywords: ["if", "else", "for", "while", "do", "switch", "return"]
The keywords if, else, for, while, do, switch and return are always followed by a space. Use if (n === 0) { // Do something } instead of if(n === 0) { // Do something }
No space after object keys disallowSpaceAfterObjectKeys: true
Don't put a space after the key in an object. Use a = { a: 1, b: 2 }; instead of a = { a : 1, b : 2 };
No space inside object brackets disallowSpacesInsideObjectBrackets: true
Don't put a space after the opening object bracket and before the closing object bracket. Use a = {a: 1, b: 2}; instead of a = { a : 1, b : 2 };
No space inside array brackets disallowSpacesInsideArrayBrackets: true
Don't put a space after the opening array bracket and before the closing array bracket. Use a = [1, 2]; instead of a = [ 1, 2 ];

Operators

No space after prefix unary operators disallowSpaceAfterPrefixUnaryOperators: ["++", "--", "-", "+", "~", "!"]
Don't put a space after one of the prefix operators "++", "--", "-", "+", "~", "!". Use --a instead of -- a
No space before postfix unary operators disallowSpaceBeforePostfixUnaryOperators: ["++", "--"]
Don't put a space after one of the postfix operators "++", "--". Use a-- instead of a --

Naming Conventions

Class names have a their first letter capitalized requireCapitalizedConstructors: true
For example Rational, Complex or Matrix.
Variable and function names are written in camelCase. camelcase: true
For example .sin() or .scalarProduct() (instead of .scalar_product())
looping variables
Variables in loops are called i, j, k, l, ... from the outermost to the innermost loop. The length is cached in the variables ii, jj, kk, ll, ... for (i = 0, ii = arr.length; i < ii; i++) { for (j = 0, jj = brr.length; j < jj; j++) { } }

Declarations

Use just one var statement whenever possible.
Declare all variables at the top of their scope.
This applies also to variables just used in a loop.
Declarations with assignments have their own lines.
Explicitly declare all variables undef: true
The variable MathLib should be the only global variable.
Don't declare variables that aren't used. unused: true

Comments

Function describing comments validateJSDoc: {...}
MathLib uses the JSDoc syntax to describe the methods. /** * A description of the method * * @param {ClassOfParameter} name - A short description of the parameter. * @param {ClassOfParameter} [options=defaultValue] - A optional parameter has the name in square brackets. * @return {ClassOfOutput} */ function (name, options = defaultValue) { // Algorithm }
General comments
These short comments are written in the line above the lines they are referring to. // Comment describing the next few lines for (i = 0, ii = arr.length; i < ii; i++) { // A not obvious algorithm }