Recommended coding standards

The following are the coding standards that the Cocos Creator development team use. They are included in the manual for game developers` and tool developers' reference.

Naming standards

```javascript // bad var FOOBar = {}; var foo_bar = {}; function FOOBar () {}

// good var fooBar = {}; function fooBar () {} ```

```javascript // bad var foobar = cc.Class({ foo: 'foo', bar: 'bar', }); var foobar = require('foo-bar');

// good var FooBar = cc.Class({ foo: 'foo', bar: 'bar', }); var FooBar = require('foo-bar'); ```

```javascript // bad this.firstName = 'foobar'; this.firstName_ = 'foobar';

// good this._firstName = 'foobar'; ```

```bash // bad fooBar.js FooBar.js

// good foo-bar.js ```

Grammar standards

```javascript // bad var obj = new Object();

// good var obj = {}; ```

```javascript // bad var array = new Array();

// good var array = []; ```

```javascript // bad var str = "Hello World";

// good var str = 'Hello World'; ```

```javascript // bad const errorMessage = 'This is a super long error that was thrown out because of Batman. When you stop to think about how Batman had anything to do with this, you would get nowhere fast.';

// bad
const errorMessage = 'This is a super long error that was thrown out because \
of Batman. When you stop to think about how Batman had anything to do \
with this, you would get nowhere \
fast.';

// good
const errorMessage = 'This is a super long error that was thrown out because ' +
  'of Batman. When you stop to think about how Batman had anything to do ' +
  'with this, you would get nowhere fast.';

```

Grammar standards

```javascript // bad function() { ∙var name; }

// very bad function() { ∙∙∙∙var name; }

// good function() { ∙∙var name; }

// good function() { ∙∙∙∙var name; } ```

```javascript // bad if ( isFoobar ) { }

// good if ( isFoobar ) { }

// bad function foobar() { }

// good function foobar() { }

// bad var obj = { foo: 'foo', bar: 'bar', }

// good var obj = { foo: 'foo', bar: 'bar', } ```

```javascript // bad function test(){ console.log('test'); }

// good function test() { console.log('test'); }

// bad dog.set('attr',{ age: '1 year', breed: 'Bernese Mountain Dog', });

// good dog.set('attr', { age: '1 year', breed: 'Bernese Mountain Dog', }); ```

```javascript // bad if(isJedi) { fight (); }

// good
if (isJedi) {
    fight();
}
```

```javascript // bad var x=y+5;

// good var x = y + 5; ```

```javascript // bad if (foo) { return bar; } return baz;

// good if (foo) { return bar; }

return baz;

// bad const obj = { foo() { }, bar() { }, }; return obj;

// good const obj = { foo() { },

   bar() {
   },

};

return obj;

// bad const arr = [ function foo() { }, function bar() { }, ]; return arr;

// good const arr = [ function foo() { },

   function bar() {
   },

];

return arr; ```

```javascript // bad var story = [ once , upon , aTime ];

// good var story = [ once, upon, aTime, ];

// bad var hero = { firstName: 'Ada' , lastName: 'Lovelace' , birthYear: 1815 , superPower: 'computers' };

// good var hero = { firstName: 'Ada', lastName: 'Lovelace', birthYear: 1815, superPower: 'computers', }; ```

Reference

airbnb/es5