This repository has been archived by the owner on Nov 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 168
Style Guide
Steven Silvester edited this page Jul 30, 2015
·
17 revisions
- Use PascalCase for type names.
- Use "I" as a prefix for interface names.
- Use PascalCase for enum values.
- Use camelCase for function names.
- Use camelCase for property names and local variables.
- Use "_" as a prefix for private properties.
- Use whole words in names when possible.
- User UPPERCASE for global constants.
- Do not export types/functions unless you need to share it across multiple components.
- Within a file, type definitions should come first.
- More than 2 Boolean properties on a type should be turned into flags.
- Use JSDoc style comments for functions, interfaces, enums, and classes.
- Use arrow functions over anonymous function expressions.
- Only surround arrow function parameters when necessary.
For example,(x) => x + x
is wrong but the following are correct: x => x + x
(x,y) => x + y
<T>(x: T, y: T) => x === y
- Always surround loop and conditional bodies with curly braces.
- Open curly braces always go on the same line as whatever necessitates them.
- Parenthesized constructs should have no surrounding whitespace.
A single space follows commas, colons, and semicolons in those constructs. For example: for (var i = 0, n = str.length; i < 10; i++) { }
if (x < 10) { }
function f(x: number, y: string): void { }
-
else
goes on the same line as the closing curly brace. - Use two (2) spaces for tabs.
- The
export
keyword should be on its own line. - Function declarations are allowed to wrap lines, but prefer 80 characters for other lines.
- Avoid public attributes - use setters/getters.
- Use
_
for private/protected variable names. - Initialize all private variables to a sentinel value or
null
. - Order should be:
- Static members
- Static methods
- Public methods
- Protected methods
- Private methods
- Protected members
- Private members
- Interface declaration:
/**
* The base message object which can be sent to a message handler.
*/
export
interface IMessage {
/**
* The type of the message.
*/
type: string;
}
- If-Else block:
if (parent) {
this._parent = null;
} else if (this.isAttached) {
this.detach();
}