Coding Convention - imonology/ImonCloud-Doc GitHub Wiki

Imonology Coding Convention

意門科技 程式碼撰寫規範
ver 1.1 (2016-02-19)

Ex. indicates "examples"

Variable Naming

Global Variables

(NOTE: should be prevented whenever possible)

g_ + lowercase word
g_ + 首字小寫變數名稱

Ex. g_size;

javascript:

use all uppercases
使用大寫命名

Ex. SIZE

Internal Member Variables

Use language-specific way to identify internal member variables
要用語言方式可判讀是物件所屬變數

Ex. this.size;

Common Variables

Begin with lowercase non-verb word
開頭小寫, 首字必須是非動詞

Ex. time_limit

Function Naming

External Functions

First word is lowercase "verb"
第一個字為動詞, 且用小寫

Ex. getMaxStudents()

perl:

can use non-verb, but should be understandable
可不用動詞, 但要易了解

Internal/Local Functions

Start with underscore 'l_'
用小寫 l 加底線當開頭 l_

Ex. l_storeSomething()

Callbacks

on + function name.
on + 函式名稱

Underscore '_' or uppercase letters can follow 'on', but should not mix
on 後面可接底線或大寫字, 但不可混用

Ex. onTaskFinished()

Class Naming

Class Names

First letter is uppercase
首字大寫

Ex. GradeList

Layout Convention

Indention

All indentions are tabs (do NOT use spaces)
所有空白都用 tab (不可用 spaces)

perl:

use 8-space tab

Parenthesis Spacing

No space before function parenthesis,
在函式括號前後不可有空格

Ex.

myobj->myFunc();        // correct
myobj->myFunc ();       // incorrect

Keyword Spacing (if, for, else, ...)

One space after keyword
關鍵字後須有空格

Ex.

if (x == 1)      // correct
if(x == 1)       // incorrect

Brackets for Single Line Statements

Brackets should be added even if the statement is only one line
即使單行 statment 亦該加大括號

Ex.

// correct
if (age >= 25) {
	console.log('too old');
}

// incorrect
if (age >= 25)
	console.log('too old');

Bracket Position

Brackets are aligned AFTER function name.
大括號須放在函式後面 (非下面)

Ex.

// correct
void foo(void) {
	...
}

// incorrect
void foo(void)
{
	...
}

Else Position

Else statements should be on the same line as the end bracket instead of below
Else 要放在結束括號同一行而非下一行

Ex.

// correct
if (i == 0) {
	...
} else {
	...
}

// incorrect
if (i == 0) {
	...
} 
else {
	...
}

Break Indention in Switch

Break statement should be indented in switch statements
Switch 裡面的 Break 需要內縮

Ex.

// correct
switch (err_code) {
	case 0:
		break;
	
	case 1:
		break;
	...
}

// incorrect
switch (err_code) {
	case 0:
	break;
	
	case 1:
	break;

	...
}

Default Switch Case

Default case should always be added in a switch (to allow default behavior)
Default 的處理在 Switch 中一定要加

Ex.

// correct
switch (err_code) {
	case 0:
		...
		break;
	
	case 1:
		...
		break;
		
	default:
		...
		break;
}

// incorrect
switch (err_code) {
	case 0:
	...
	break;
	
	case 1:
	...
	break;
}

Operator/Assignment Spacing

Add spaces around all operators, assignments.
所有語言運算子前後要有空格

Ex.

// correct
if (width < 3 && height > 5)

// incorrect
if (width<3 && height>5)

Trailing Spaces

Do not have trailing spaces at end of each line
每行結尾不可有空白