Skip to content

Latest commit

 

History

History
104 lines (81 loc) · 2.59 KB

File metadata and controls

104 lines (81 loc) · 2.59 KB

Common

  • Use TypeScript language during development

  • All local variables, function parameters should be named in camelCase style, without any special prefixes

  • Specify local variables, parameters and return value types:

function add(firstParam: number, secondParam: number) : number {
    const localVariable: number = firstParam + secondParam;
    return localVariable;
}
  • All functions should be named in a camelCase style

  • Use const for local variables that woudn't changes:

function max(values: number[]) : number {
    let maxValue = values[0];
    for (let i = 0; i < values.length; ++i) {
        const v = values[i];
        if (v > maxValue)
            maxValue = v;
    }
    return maxValue;
}
  • Prevent importing all from module. Where it possible use:
import { Class1, Class2 } from './classes';
  • Don't miss ; symbol at the end of expressions

  • Throw exceptions when it goes to wrong logic behavior. Use throw 'string' to throw any exception:

if (...)
    throw "Undefined behavior"; // format string as you wish (but include some useful info about error)

Classes

  • All class members (not functions) should start with _ prefix. And has a camelCase style name:
class ExampleClass {
    private _exampleMember: number;
}
class ExampleClass {
    private _value: string;

    get value() : string {
        return this._value;
    }

    set value(newValue: string) {
        this._value = newValue;
    }
}
  • Name methods with a camelCase style:
class MyClass {
    myMethod() : void {

    }
}
  • Name classes in style ClassName

  • Use private, protected, public keywords to control methods and members visibility

  • Use export keyword with class definition when export it from module:

export class MyExportedClass {

}
  • Do not export classes from module, if they use only in the last one

  • For arguments in methods, that has the same name as properties use _argName names. In example, if we will use x name for argument in constructor, then it would be undefined behavior, because we have x getter. So use _x argument name:

class Vector {
    private _x;
    private _y;

    constructor(_x: number, _y: number) {
        this._x = _x;
        this._y = _y;
    }

    get x(): number {
        return this._x;
    }
}