Skip to content

Coding Style Guidelines

SungRim Huh edited this page Oct 31, 2013 · 7 revisions

File Names

File names and directory names should be all lower-case with an underscore (_) to separate words.
Example: my_file.js

Documentation

Every code file should contain a header comment clearly explaining the purpose of the file. Example: physics.js

/*!
 * physics.js
 * Math and physics functions used by the game engine.
 */

Major functions and methods should have header comments explaining what the function does, it's parameters, and what it returns. Example:

/**
 * Sets the width of the canvas.
 * width: (int) The width to set the canvas to.
 */
function setWidth(width) { }

Indentation

Use tab for indentation, and use space for aligning things.

var a     = 1 // Spaces
var b     = 1 // Spaces
var c     = 1 // Spaces
var hello = 1

while (true) {
    console.log("Hello World!"); // Tab
}

Spacing

Spacing is your friend. It makes everything more readable. Put a space before and after operators (+, +=, -, -=, *, *=, etc.) Put a space before the opening parentheses and after the closing parentheses in control statements (if ( ... ) {, for ( ... ) {, while ( ... ) {, etc.)

There should only be one variable declaration/initialization per line. Example:

var x = 2, // Good
    y = 3, // Good
    z = 4, w = 5; // Bad