# CONTRIBUTING
- Looking for ideas? Check out "good first PR" label.
- Or start a conversation in Issues to get help and advice from community on PR ideas.
- Read the coding standards below.
- Keep PR simple and focused - one PR per feature.
- Make a Pull Request.
- Complete the Contributor License Agreement.
- Happy Days! :)
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.
These coding standards are based on the Google Javascript Coding Standards. If something is not defined here, use this guide as a backup.
Simple code is always better. Modular (horizontal dependencies) code is easier to extend and work with, than with vertical dependencies.
For example, use "Initialize" instead of "Initialise", and "color" instead of "colour".
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();
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.
On save, set your text editor to remove trailing spaces and ensure there is an empty line at the end of the file.
var foo = 16 + 32 / 4;
for (var i = 0, len = list.length; i < len; i++) {
// ...
}
var fn = function () {
};
foo();
bar(1, 2);
var a = { };
var b = { key: 'value' };
var c = [ ];
var d = [ 32, 64 ];
Semicolons are not needed to delimit the ends of functions. Follow the convention below:
function class() {
} // Note the lack of semicolon here
Semicolons are needed if you're function is declared as a variable
var fn = function () {
}; // Note the semicolon here
Variable declarations should all be placed first or close to the top of functions. This is because variables have a function-level scope.
Variables should be declared one per line.
function fn() {
var a = 0;
var b = 1;
var c = 2;
}
function fn() {
var i;
var bar = 0;
for(i = 0; i < 32; ++i) {
bar += i;
}
for(var i = 0; i < 32; i++) { } // don't do this, as i is already defined
}
In functions exit early to simplify logic flow and avoid building indention-hell:
var foo = function (bar) {
if (! bar)
return;
return bar + 32;
};
Same for iterators:
for(var i = 0; i < items.length; i++) {
if (! items[i].test)
continue;
items[i].bar();
}
// Namespace should have short lowercase names
var namespace = { };
// Classes (or rather Constructors) should be CamelCase
var MyClass = function () { };
// Variables should be mixedCase
var mixedCase = 1;
// Function are usually variables so should be mixedCase
// (unless they are class constructors)
var myFunction = function () { };
// Constants should be ALL_CAPITALS separated by underscores.
// Note, ES5 doesn't support constants,
// so this is just convention.
var THIS_IS_CONSTANT = "well, kind of";
// Private variables should start with a leading underscore.
// Note, you should attempt to make private variables actually private using
// a closure.
var _private = "private";
var _privateFn = function () { };
Treat acronyms like a normal word. e.g.
var json = ""; // not "JSON";
var id = 1; // not "ID";
function getId() { }; // not "getID"
function loadJson() { }; // not "loadJSON"
new HttpObject(); // not "HTTPObject";
function asyncFunction(success, error) {
// do something
}
function asyncFunction(success) {
// do something
}
function asyncFunction(callback) {
// do something
}
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'.
var self = this;
setTimeout(function() {
this.foo();
}.bind(this)); // don't do this
Instead use self
reference in upper scope:
var self = this;
setTimeout(function() {
self.foo();
});
Variables that should be accessible only within class should start with _
:
var Item = function () {
this._a = "private";
};
Item.prototype.bar = function() {
this._a += "!";
};
var foo = new Item();
foo._a += "?"; // not good
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 (var key in values) {
if (! values.hasOwnProperty(key))
continue;
doStuff(values[key]);
}
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
Use library function pc.extend to add additional Classes, methods and variables on to an existing namespace. Private functions and variables should be declared inside the namespace. Avoid declaring multiple Classes and Methods extending namespace per file.
pc.extend(pc, function() {
var Class = function () {
};
// optionally can inherit
Class = pc.inherits(Class, pc.Base);
Class.prototype.derivedFn = function () {
};
return {
Class: Class
};
}());