-
Notifications
You must be signed in to change notification settings - Fork 4
Coding standards
This document describes style guidelines for writing Javascript code in Spider-Fish. You must follow these guidelines as closely as possible. It is very important to have consistent looking code because it makes it much easier for everyone to understand.
Each class belongs in it's own file. The file is the same name as the class. The class is upper camel case.
(In file Ship.js)
Ship = function()
{
// class content
};
Braces on their own line not next to the function or class name.
// good
Ship = function()
{
};
// bad
Ship.prototype.fire = function() {
return "pew!";
};
Use 2 spaces for indentation, no tabs
Ship = function()
{
// good
this.sound = "pew!";
};
Ship.prototype.fire = function()
{
// bad
return this.sound;
};
Always put spaces after a comma in a list (arguments, array, etc)
Ship = function(x, y, name) // good
{
};
Ship.prototype.turn = function(angle,amount) // bad
{
var arr = [angle, amount]; // good
arr.splice(0,1); // bad
};
Always put member functions outside the class scope and after the "obj.extends" call:
// Ship.js
Ship = function()
{
};
obj.extend(Ship, Entity);
// good
Ship.prototype.fire = function()
{
};
// Bullet.js
Bullet = function()
{
// bad
Bullet.prototype.sound = function()
{
};
};
// bad
Bullet.prototype.collide = function()
{
};
obj.extend(Bullet, Entity);
Put spaces before and after operators
Ship.prototype.fire = functon(amount)
{
var total = 2 + amount; // good
return "pew: "+total; // bad
};
However try to be reasonable. When its very cluttered try to use spaces for organization:
Ship.prototype.fire = function(direction, amount)
{
// good
var velo = Math.sin(direction + (Math.PI/2));
var complexMath = (2*velo.x) + (3*velo.y) + ((5*velo.y) / 2);
// bad
var target = velo.x + Math.cos(direction + Math.PI / (2 * amount));
var hardToLookAt = ((4 * (x + 2)) - (5 * (y + 2))) / (3* x + 2 * y);
};