Skip to content

Latest commit

 

History

History
371 lines (280 loc) · 10.2 KB

CONTRIBUTING.md

File metadata and controls

371 lines (280 loc) · 10.2 KB

CONTRIBUTING

HOW TO CONTRIBUTE

  1. Looking for ideas? Check out "good first PR" label.
  2. Or start a conversation in Issues to get help and advice from community on PR ideas.
  3. Read the coding standards below.
  4. Keep PR simple and focused - one PR per feature.
  5. Make a Pull Request.
  6. Complete the Contributor License Agreement.
  7. Happy Days! 🎉

Tips

Feel free to contribute bug fixes or documentation fixes as pull request.

If you are looking for ideas what to work on, head to Issues and checkout out open tickets or start a conversation. It is best to start conversation if you are going to make major changes to the engine or add significant features to get advice on how to approach it. Forum is good place to have a chat with community as well.

Try to keep PR focused on a single feature, small PR's are easier to review and will get merged faster. Too large PR's are better be broken into smaller ones so they can be merged and tested on its own.

CODING STANDARDS

General

Our coding standards are derived from the Google JavaScript Coding Standards which are based on ES5 (ECMA2009). Also we have a whitelist of modern JavaScript features (ES6+), explicitly listed below.

Keep it simple

Simple code is always better. Modular (horizontal dependencies) code is easier to extend and work with, than with vertical dependencies.

Use International/American English spelling

For example, use "Initialize" instead of "Initialise", and "color" instead of "colour".

Permitted ES6+ features:

You may use the following JavaScript language features in the engine codebase:

Opening braces should be on the same line as the statement

For example:

// Notice there is no new line before the opening brace
function inc() {
    x++;
}

Also use the following style for 'if' statements:

if (test) {
    // do something
} else {
    // do something else
}

If condition with body is small and is two-liner, can avoid using braces:

if (test === 0)
    then();

Use spaces in preference to tabs

Ensure that your IDE of choice is set up to insert '4 spaces' for every press of the Tab key and replaces tabs with spaces on save. Different browsers have different tab lengths and a mixture of tabs and spaces for indentation can create funky results.

Remove all trailing spaces and ending line

On save, set your text editor to remove trailing spaces and ensure there is an empty line at the end of the file.

Use spaces between operators

let foo = 16 + 32 / 4;

for (let i = 0; i < list.length; i++) {
    // ...
}

Leave a space after the function keyword for anonymous functions

let fn = function () {

};

No spaces between () brackets

foo();
bar(1, 2);

Use spaces between [ ] and { } brackets, unless they are empty

let a = {};
let b = { key: 'value' };
let c = [];
let d = [ 32, 64 ];

let and const instead of var (ES6)

for (let i = 0; i < items.length; i++) {
    const item = items[i];
}

var a = 10; // not good

For of loop (ES6)

// ok
for (let i = 0; i < items.length; i++) {
    const item = items[i];
}

// more readable but generally slower
for (const item of items) {

}

Exit logic early

In functions exit early to simplify logic flow and avoid building indention-hell:

let foo = function (bar) {
    if (! bar)
        return;

    return bar + 32;
};

Same for iterators:

for (let i = 0; i < items.length; i++) {
    if (! items[i].test)
        continue;

    items[i].bar();
}

Naming

Capitalization

// Namespace should have short lowercase names
let namespace = { };

// Classes should be CamelCase
class MyClass { }

// Variables should be mixedCase
let mixedCase = 1;

// Function are usually variables so should be mixedCase
// (unless they are class constructors)
let myFunction = function () { };
let myFunction = () => { };

// Constants should be ALL_CAPITALS separated by underscores.
// Note, ES5 doesn't support constants,
// so this is just convention.
const THIS_IS_CONSTANT = "well, kind of";

// Enum constants follow similar rules as normal constants. In general,
// the enum consists of the type, and its values.
// In other languages, this is implemented as
// enum CubeFace {
//     PosX: 0,
//     PosY: 1
// }
// Due to the lack of native enum support by JavaScript, the enums are
// represented by constants. The constant name contains the enum name without
// the underscores, followed by the values with optional underscores as
// needed to improve the readibility. This is one possible implementation:
const CUBEFACE_POSX = 0;
const CUBEFACE_POSY = 1;
// and this is also acceptable
const CUBEFACE_POS_X = 0;
const CUBEFACE_POS_Y = 1;

// Private variables should start with a leading underscore.
// Note, you should attempt to make private variables actually private using
// a closure.
let _private = "private";
let _privateFn = function () { };

Acronyms should not be upper-case, they should follow coding standards

Treat acronyms like a normal word. e.g.

let json = ""; // not "JSON";
let id = 1; // not "ID";

function getId() { }; // not "getID"
function loadJson() { }; // not "loadJSON"

new HttpObject(); // not "HTTPObject";

Use common callback names: 'success', 'error', (possibly 'callback')

function asyncFunction(success, error) {
  // do something
}
function asyncFunction(success) {
  // do something
}
function asyncFunction(callback) {
  // do something
}

Cache the 'this' reference as 'self'

It is often useful to be able to cache the 'this' object to get around the scoping behavior of JavaScript. If you need to do this, cache it in a variable called 'self'.

let self = this;

Avoid using function.bind(scope)

setTimeout(function() {
    this.foo();
}.bind(this)); // don't do this

Instead use self reference in upper scope:

let self = this;
setTimeout(function() {
    self.foo();
});

Default function parameters (ES6)

Use this notation for function default parameters:

// good
function foo(a, b = 10) {
    return a + b;
}

// not good
function foo(a, b) {
    if (b === undefined)
        b = 10;
    return a + b;
}

Privacy

Make variables private if used only internally

Variables that should be accessible only within class should start with _:

class Item {
    constructor() {
        this._a = "private";
    }

    bar() {
        this._a += "!";
    }
}

let foo = new Item();
foo._a += "?"; // not good

Object Member Iteration

The hasOwnProperty() function should be used when iterating over an object's members. This is to avoid accidentally picking up unintended members that may have been added to the object's prototype. For example:

for (let key in values) {
    if (! values.hasOwnProperty(key))
        continue;

    doStuff(values[key]);
}

Source files

Filenames should contain only class name

Filenames should be all lower case with words separated by dashes. The usual format should be {{{file-name.js}}}

e.g.

asset-registry.js
graph-node.js

Namespaces and Classes (ES6)

The entire PlayCanvas API must be declared under the pc namespace. This is handled by build script, so ES6 notation of import/export should be used. The vast majority of the PlayCanvas codebase is made up of class definitions. These have the following structure (which should be adhered to):

class Class {
    someFunc(x) { }
}

export { Class };

You can also extend existing classes:

import { Class } from './class.js';

class SubClass extends Class {
    constructor() {
        // call parent class constructor
        super();
    }

    someFunc(x) {
        // if method is overriden
        // this is the way to call parent class method
        super.someFunc(x);
    }
}

export { SubClass };

Use class instead of prototype for defining Classes:

// good
class Class {
    someFunc() { }
}

// not good
function Class() { }
Class.prototype.someFunc = function() { };