This repository was 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 and enum values.
- Use camelCase for function names, property names and local variables.
- Use UPPER_CASE for global constants.
- Use "_" as a prefix for private properties and "I" as a prefix for interface names.
- Use whole words in names when possible.
- Do not export types/functions unless you need to share them across multiple components.
- Within a file, type definitions should come first.
- More than 2 Boolean properties on a type should be turned into bit flags (see example below).
- 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 { }
- Use a single declaration per variable statement
(i.e. usevar x = 1; var y = 2;
overvar x = 1, y = 2;
). -
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();
}
- Bit flags:
export
enum WidgetFlag {
/**
* The widget is attached to the DOM.
*/
IsAttached = 0x1,
}
export
class Widget {
/**
* Test whether the given widget flag is set.
*/
testFlag(flag: WidgetFlag): boolean {
return (this._wflags & flag) !== 0;
}
/**
* Set the given widget flag.
*/
setFlag(flag: WidgetFlag): void {
this._wflags |= flag;
}
/**
* Clear the given widget flag.
*/
clearFlag(flag: WidgetFlag): void {
this._wflags &= ~flag;
}
private _wflags = 0;
}