Recommended Coding Standards

    • When naming the variables, functions and instances, we use camelCase nomenclature:

    • When variable, function, and instance naming involves abbreviations, the abbreviations are all lowercase at the beginning, and all uppercase in subsequent words:

      1. const Id = 0;
      2. const iD = 0;
      3. function requireId () {}
      4. // Good
      5. const id = 0;
      6. const uuid = '';
      7. function requireID () {}
      8. class AssetUUID {}
    • When naming types or modules, use PascalCase nomenclature:

      1. // Bad
      2. const foobar = cc.Class({
      3. foo: 'foo',
      4. bar: 'bar',
      5. });
      6. const foobar = require('foo-bar');
      7. // Good
      8. const FooBar = cc.Class({
      9. foo: 'foo',
      10. bar: 'bar',
      11. });
      12. const FooBar = require('foo-bar');
    • It is recommended to use full uppercase underline to name “constants”:

      1. // Bad
      2. const PRIVATE_VARIABLE = 'should not be unnecessarily uppercased within a file';
      3. // Bad
      4. var THING_TO_BE_CHANGED = 'should obviously not be uppercased';
      5. // Bad
      6. let REASSIGNABLE_VARIABLE = 'do not use let with uppercase variables';
      7. // ---
      8. // Allowed but does not supply semantic value
      9. export const apiKey = 'SOMEKEY';
      10. // Better in most cases
      11. export const API_KEY = 'SOMEKEY';
      12. // ---
      13. // Bad - unnecessarily uppercases key while adding no semantic value
      14. export const MAPPING = {
      15. KEY: 'value'
      16. };
      17. // Good
      18. export const Type = {
      19. SIMPLE: 'value'
      20. };
    • Use underscores _ when naming private attributes:

      1. // Bad
      2. this.__firstName__ = 'foobar';
      3. this.firstName_ = 'foobar';
      4. // Good
      5. this._firstName = 'foobar';
    • Use dash nomenclature for files:

      1. // bad
      2. fooBar.js
      3. FooBar.js
      4. // good
      5. foo-bar.js
    • When a class has a property declaration without initialization style, declare should be declared, otherwise may face performance problems. Please refer to this issue for details.

      1. // Bad
      2. class A {
      3. public a: number;
      4. constructor (a : number) {
      5. // This is equivalent to another sentence ‘this.a = void 0;’ here.
      6. // Be aware that may face performance problems!
      7. this.a = a;
      8. }
      9. }
      10. // Good
      11. class A {
      12. public a: number = 0; // Ok.
      13. constructor (a : number) {
      14. // This is equivalent to another sentence ‘this.a = 0;’ here.
      15. // Does not cause major performance problems.
      16. this.a = a;
      17. }
      18. }
      19. // Best
      20. class A {
      21. public declare a: number;
      22. public b: undefined | object; // OK: b No secondary assignment in the constructor.
      23. public declare c: object | null;
      24. constructor (a: number, c: object) {
      25. this.a = a;
      26. this.c = c;
      27. }
      28. }
    • Use Object.create(null) to create an object:

      1. // Bad
      2. const obj = new Object();
      3. // Bad
      4. const obj = {};
      5. // Good
      6. const obj = Object.create(null);
    • Try your best to use single quotation marks '' to define a string in TypeScript code:

      1. // Bad
      2. const str = "Hello World";
      3. // Good
      4. const str = 'Hello World';
    • When defining multi-lines string, try your best to use +:

      1. // Bad
      2. 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.';
      3. // Bad
      4. const errorMessage = 'This is a super long error that was thrown out because \
      5. of Batman. When you stop to think about how Batman had anything to do \
      6. with this, you would get nowhere \
      7. fast.';
      8. // Good
      9. const errorMessage = 'This is a super long error that was thrown out because ' +
      10. 'of Batman. When you stop to think about how Batman had anything to do ' +
      11. 'with this, you would get nowhere fast.';
    • Use === and !== rather than == and !=.

    • Do not leave spaces at the end of the line. Leave an empty line at the bottom of the file:

      1. // Bad
      2. function () {∙
      3. ∙∙∙∙const name;∙
      4. }
      5. /* EOF */
      6. // Good
      7. function () {
      8. ∙∙∙∙const name;
      9. }
      10. /* EOF */
    • Please add ; at the end of the statement:

      1. // Bad
      2. proto.foo = function () {
      3. }
      4. // Good
      5. proto.foo = function () {
      6. };
      7. // Bad
      8. function foo () {
      9. return 'test'
      10. }
      11. // Very bad
      12. // Returns `undefined` instead of the value on the next line,
      13. // Always happens when `return` is on a line by itself because of Automatic Semicolon Insertion!
      14. function foo () {
      15. return
      16. 'test'
      17. }
      18. // Good
      19. function foo () {
      20. return 'test';
      21. }
      22. // Bad
      23. function foo () {
      24. };
      25. // Good, this is not the end of the statement
      26. function foo () {
      27. }
    • Try to put { and the expression in the same line:

      1. // Bad
      2. if ( isFoobar )
      3. {
      4. }
      5. // Good
      6. if ( isFoobar ) {
      7. }
      8. // Bad
      9. function foobar()
      10. {
      11. }
      12. // Good
      13. function foobar() {
      14. }
      15. // Bad
      16. const obj =
      17. {
      18. foo: 'foo',
      19. bar: 'bar',
      20. }
      21. // Good
      22. const obj = {
      23. foo: 'foo',
      24. bar: 'bar',
      25. }
    • Put a space before {:

      1. // Bad
      2. if (isJedi){
      3. fight();
      4. }
      5. else{
      6. escape();
      7. }
      8. // Good
      9. if (isJedi) {
      10. fight();
      11. } else {
      12. escape();
      13. }
      14. // Bad
      15. dog.set('attr',{
      16. age: '1 year',
      17. breed: 'Bernese Mountain Dog',
      18. });
      19. // Good
      20. dog.set('attr', {
      21. age: '1 year',
      22. breed: 'Bernese Mountain Dog',
      23. });
    • Leave one space between the binary ternary operators:

      1. // Bad
      2. const x=y+5;
      3. const left = rotated? y: x;
      4. // Good
      5. const x = y + 5;
      6. const left = rotated ? y : x;
      7. for (let i=0; i< 10; i++) {
      8. // Good
      9. for (let i = 0; i < 10; i++) {
      10. }
    • The way some functions are declared:

      1. // Bad
      2. const test = function () {
      3. console.log('test');
      4. };
      5. // Good
      6. function test () {
      7. console.log('test');
      8. }
      9. // Bad
      10. function divisibleFunction () {
      11. return DEBUG ? 'foo' : 'bar';
      12. }
      13. // Good
      14. const divisibleFunction = DEBUG ?
      15. function () {
      16. return 'foo';
      17. } :
      18. function () {
      19. return 'bar';
      20. };
      21. // Bad
      22. function test(){
      23. }
      24. // Good
      25. function test () {
      26. }
      27. // Bad
      28. const obj = {
      29. foo: function () {
      30. }
      31. };
      32. // Good
      33. const obj = {
      34. foo () {
      35. }
      36. };
      37. // Bad
      38. array.map(x=>x + 1);
      39. array.map(x => {
      40. return x + 1;
      41. });
      42. // Good
      43. array.map(x => x + 1);
    • Put a space between Block definitions:

      1. // Bad
      2. if (foo) {
      3. return bar;
      4. }
      5. return baz;
      6. // Good
      7. if (foo) {
      8. return bar;
      9. }
      10. return baz;
      11. // Bad
      12. const obj = {
      13. x: 0,
      14. y: 0,
      15. foo() {
      16. },
      17. bar() {
      18. },
      19. };
      20. return obj;
      21. // Good
      22. const obj = {
      23. x: 0,
      24. y: 0,
      25. foo() {
      26. },
      27. bar() {
      28. },
      29. };
      30. return obj;
    • Do not use a comma to define:

      1. // Bad
      2. const story = [
      3. once
      4. , upon
      5. , aTime
      6. ];
      7. // Good
      8. const story = [
      9. once,
      10. upon,
      11. aTime,
      12. ];
      13. // Bad
      14. const hero = {
      15. firstName: 'Ada'
      16. , lastName: 'Lovelace'
      17. , birthYear: 1815
      18. , superPower: 'computers'
      19. };
      20. // Good
      21. const hero = {
      22. firstName: 'Ada',
      23. lastName: 'Lovelace',
      24. birthYear: 1815,
      25. superPower: 'computers',
      26. };
    • Single line comments, please add a space after the slash:

      1. // Bad
      2. // Good
    • Multiline comments:

      1. /*
      2. * Good
      3. */
    • A multiline comments that needs to be exported to the API document:

      1. /**
      2. * Good
      3. */