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 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.
- Always surround loop and conditional bodies with curly braces.
- Open curly braces always go on the same line as whatever necessitates them.
-
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 and class signatures can exceed 80 chars; use 80 chars for all other lines.
- Avoid public attributes - use setters/getters.
- Do not use the
public
keyword for public methods - it is implied. - 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;
}