--- slideOptions: transition: slide # spotlight: # enabled: true --- 國際板 - coding style === ###### tags: `博發` [TOC] ## 命名規範 #### 變數 > 使小寫駝峰 + 型態 + 初始值 ``` // bad private fooBar; private FOOBar: string = ""; private foo_bar: string = ""; ``` ``` // good private fooBar: string = ""; ``` #### 常數 > 使小寫駝峰 ``` // bad const FOOBAR = ""; const foo_bar = ""; ``` ``` // good const fooBar = ""; ``` #### 列舉 > 大寫駝峰 (內部參數全大寫) ``` // bad enum direction { up = "UP", down = "DOWN", left = "LEFT", right = "RIGHT", } enum Direction { Up = "UP", Down = "DOWN", Left = "LEFT", Right = "RIGHT", } ``` ``` // good enum Direction { UP = "UP", DOWN = "DOWN", LEFT = "LEFT", RIGHT = "RIGHT", } enum CurveType { CUBIC_IN = 0, CUBIC_OUT = 1, CUBIC_INOUT = 2, LINE = 3, } ``` #### 靜態變數 ``` // Good public static version: string = "1.0"; // Good public static VERSION: string = "1.0"; ``` #### 介面 > 前面加I ``` // bad interface Thing { } // good interface IThing { } ``` #### 函數 > 小寫駝峰 + 回傳值 (沒有回傳的可以不用寫) ``` // bad private Function_Name (): stirng{ return "A"; } private FunctionName (): string{ return "A"; } private functionName (){ return "A"; } ``` ``` // good private functionName (): string{ return "A"; } private functionName (){ } ``` #### 類別 > 使大寫駝峰 ``` // bad export class Game_Manager extends Component { } export class gameManager extends Component { } export class GAMEMANAGER extends Component { } ``` ``` // good export class GameManager extends Component { } ``` ## 語言規範 #### 使用 [] 创建一个数组 ``` // bad let array = new Array(); ``` ``` // good let array = []; ``` #### 使用'' 定義string ``` // bad let str = "Hello World"; // good let str = "Hello World"; ``` #### 多行 string 定义时, 尽可能使用 + 定义 ## 書寫規範 #### { 和表達式放在同一行 ``` // bad if ( isFoobar ) { } // good if ( isFoobar ) { } ``` ``` // bad function foobar () { } // good function foobar () { } ``` #### if else ``` //bad if (a) { //do something } else { //do something } if (a) { //do something } else { //do something } ``` ``` // good if (a) { //do something }else { //do something } ``` #### switch case ``` // bad switch (expr) { case 0: doThing("First case, with a break"); break; case 1: doThing("Second case, which falls through"); // no break case 2: case 3: case 4: doThing("Third case, return instead of break"); return; default: doThing("Default case"); break; } ``` ``` // good switch (expr) { case 0: doThing("First case, with a break"); break; case 1: doThing("Second case, which falls through"); // no break case 2: case 3: case 4: doThing("Third case, return instead of break"); return; default: doThing("Default case"); break; } ``` ## 註解 > 使用 /** */ ``` // bad // 取得玩家名稱 protected getPlayerName(): string { return this.playerName; } //設定資料 public setData(id: number, type: number){ this.id = id; this.type = type; } ``` ``` // good /** * 取得玩家名稱 * @returns 玩家名稱 */ protected getPlayerName(): string { return this.playerName; } /** * 設定資料 * @param id 玩家ID * @param type 玩家類型 */ public setData(id: number, type: number){ this.id = id; this.type = type; } /** 初始化 */ public init(){ //so something } ``` ## 引擎層級管理器命名 #### 有Label的組件前面加Txt ``` // bad PlayerName ``` ``` // good TxtPlayerName ```