Skip to content

Coding Standards

Lydia edited this page Feb 20, 2018 · 3 revisions

CODING STANDARDS

Casing

As typical with most Javascript applications, we use camel case - first letter of first word is lowercase, and the first letter of every word thereafter is uppercase.

e.g. example, or thisIsMyVariableName.

Typing & Constants

Give your variables a type where possible & predictable, and use the let keyword for variables who we know will change and const for variables we know will not.

e.g. const one: number = 1, or let result: number = someNumber * someOtherNumber

Indentation & spacing

Configure your VSCode editor to indent with tabs, and set the size to 2 spaces. You can do this by clicking the menu on the bottom right-hand side of the window, and selecting Indent using tabs and then 2 as you go through the menus.

When writing a block of code, i.e. a method or class definition, put the opening curly brace on the same line as the name and give one line of padding

export class myImportantClass {

    private const someVariable: string = 'foo bar';	

    public getVar: string = () => {

        return this.someVariable;

    }

}

When passing parameters into methods, or logging something to the console, pad the brackets with one space either side, and leave a space after each parameter e.g.

console.log( 'example' );

public addAAndB = ( a: number, b: number ) => {
         
    return a + b;      

};

addAAndB( 1, 2 ); // -> 3

Commenting

Methods should be commented with a brief description using the JSDoc style comments

/**
 * Return the sum of a & b
 * @param a - some number value
 * @param b - some number value
 */
public addAAndB = ( a: number, b: number ) => {

    return a + b;

}

Complex/ambiguous code should either be rethought and refactored to be simplified and self-explanatory or should be preceded by a standard Javascript style comment to explain it. Comments should be preceded by one space after the slashes and should be at the same indentation level as the code it is explaining.

/**
 * Return the sum of a & b
 * @param a - some number value
 * @param b - some number value
 */
public addAAndB = ( a: number, b: number ) => {
   
    // return the sum of a & b
    return a + b;

}
Clone this wiki locally