Coding Convention Rules

1. ArrayElementAccess(V) : 確保數組元素通過數組操作符[ ]獲取。

2.?AvoidEllipses(V):不要使用省略號。可以使用默認參數。

? ? 例如:int foo( int a, ... );// Violation?

? ? ? ? ? ? ? ?int foo( int a, int b = 0 );// OK

3.?AvoidFuncDefinitonInClass: 不要在類定義里定義成員函數. 確保接口和實現分離。函數定義在類定義內是默認inline, 而且不緊湊不利于閱讀。

4.?AvoidGotoStatement:不要使用goto

5.?AvoidInlineConstrDestr:不要把構造函數和析構函數定義為inline。function that invoke other inline function often become?too complex for the compiler, and the compiler cannot make them inline.

6.?AvoidStructWithMemberFunction:不要定義包含成員函數的struct。 函數應該包含在class內。

7.?AvoidTernaryOperator:不要使用三元操作符?: 影響可讀性

8.?AvoidVoidParameter: 如果函數沒有參數,使用()而非(void),聲明一致性

9.?BreakInForLoop: 不要在for循環內使用break

10.?CastFuncPtrToPrimPtr:?Do not type cast pointers to primitive types for variables that have

been declared as pointers to functions。

????This rule detects if you type case pointers to primitive types for variables that have been declared as

pointers to functions.

????Benefits:Following this rule helps you make code less error-prone; casting pointers to functions to pointers of

primitive types can result in errors.

Example:

void Foo(char *ptrC)

{

????*ptrC = 0;

????return;

}

void f()

{

????void *ptrV = 0;

????void (*funPtr) (char*) = 0;

????funPtr = &Foo;

????ptrV = (void*)funPtr; // Violation

????return;

}

Output:Pointer to a function is cast to a pointer of primitive type


11. CommaOperator:逗號運動算符僅僅用在變量聲明/定義或多語句預處理器宏定義。好處是提高可讀性和防止額外的使用逗號操作符

12.?ConditionCurlyBraces:所有if 和else?statements必須跟隨{ },識別與該語句相關的代碼。

13.?ConditionCurlyBraces2:確保所有條件語句都使用{} 來識別與之相關的代碼

14.?ConstParam: 盡可能聲明引用參數為常量引用。如果函數不打算改變引用的參數, 可以聲明為常量引用來避免函數返回值意外修改或防止無意中修改調用者的數據。

15.?ConstPointerFunctionCall:如果指針指向不被改變可以傳const指針

16.?DeclareArrayMagnitude:如果數組作為函數的參數時,不要聲明它的大小。單維數組的大小永遠不要在參數中聲明,多維數組大小不能全部聲明大小。因為不同函數調用可能傳參不同的大小。

17.?DeclDimArray:不要在數組初始化的時候聲明它的大小。因為維護性較差。

????#define SIZE 4

????int tab1[SIZE] = {1,2,3}; // Violation

? ? ?int tab2[]={1,2,3}; // OK

18.?DefaultInSwitch:不要省略switch 的default分支

19.?DoWhile:不要使用do while,應該使用while?

20.?EnumKeyword: 不要用enum關鍵字聲明一個變量。

? ??enum Colors { RED, BLUE, GREEN };

????enum Colors c; // Violation

????enum Colors { RED, BLUE, GREEN };

????Colors c; // OK

21.?EOSUsage:使用EOS define代替直接使用“\0”作為字符串的終止符。

? ??#define EOS '\0';

????char str[30] = "Sample text.";

????str[7] = '\0'; // Violation

? ??str[7] = EOS; // OK

22.?ExplicitEnumValues:確保enum的每個成員被明確聲明。

????enum my_enum1

????{

????????a, // Violation

????};;

????enum my_enum1

????{

????????a = 0 // OK

????};;

23.?ExplicitLogicalTests:在條件表達式中使用明確的邏輯比較。if (isvisible) // Violation

24.?ExplicitlyReturnType:明確函數返回類型,假如沒有,編譯器會認為是void或int。

????foo( ); // Violation

????int foo( );// OK

25.?ExprInSizeof:避免傳給表達式給sizeof(),例如賦值語句傳給它。

? ??iVar1 = sizeof(iVar2 = 1); // Violation

26.?FalseDefinition:如果FALSE沒有定義,那么 Definition FALSE為0

27.?FalseTypedef:如果FALSE沒有定義,那么typedef FALSE為0?

28.?ForLoopVarAssign:從for語句的body中去除循環控制變量的賦值。循環控制變量應該僅被修改在for循環語句的初始化和條件表達式。在for循環內修改它們使得循環條件很難理解和可能出現邏輯缺陷。

29.?FuncModifyGlobalVar:避免函數修改global變量

30.?GlobalVarFound:避免聲明global變量

31.?HardCodeValue:使用“#define” 或enum常量代替hard coded values(魔幻數字)

32.?HardCodeValueWithZero:

33.?IfElseWhileFollowBlock:循環語句即使為空也要加大括號(with block)

34.?MacroWithinInclude: 不要在include聲明語句中使用宏定義()

? ??#define HEADER_FILE(nr) "myHeader" #nr ".h"

????#include HEADER_FILE(12) //Violation

35.?ModifyInCondition: 不要在if while switch條件表達式中使用++ -- 操作符

36.?NullDefinition: 定義NULL “(void*)0”

37.?OnlyOneReturn:函數最多有一個return 語句

38.?PartialStatementDefinition:不要定義生命的一部分

? ??#define PARTIAL(a)? ? ((a) * // Violation

39.?PassByValue:

40.?RedefControlStatements:不要重新定義循環控制語句if while try

41.?RedefPrimitiveTypes: 不要redefine原始數據類型char?

????#define T30 char //Violation

????#define T31 unsigned char // OK

42.?RedefTypes:

43.?SepareLogicalTests:使用明確的邏輯表達式

44.?SingularSwitchStatement:避免switch只有一條case語句,如果這樣應該改寫成if else。

45.SourceFileSize:避免source file超過500行

46.?StructKeyword:不要使用struct關鍵字來聲明一個變量

????Example:

????struct Position_t {};

????struct Position_t Pos; // Violation

????Repair:

????struct Position_t {};

????Position_t Pos; // OK

47.?TrueDefinition:如果TRUE需要被定義,那么應該定義為1

48.?TrueTypedef:

49.?TypeModifiersAfterTypes:?Ensure that storage type modifiers are associated with the type, not the variable

? ??int static i; //Violation

? ??static char j; //OK

50.?UnnecessaryEqual:在bool函數內去掉不必要的==true

????bool foo()

????{

????????return isPositive(5) == true; // Violation

????????return isPositive(5); // OK

????}

51.?UseParenthesisInMacros: 在宏定義表達式中, 應該在乘除號之前和之后加上括號。

? ??#define MULTI_1(x)? ? (x * (x)) //Violation

? ??#define MULTI_3(x)? ? ?((x) * (x)) //OK

52.?UsePositiveLogic:使用正邏輯而非賦邏輯

53.?UseTypedefForFuncPointers: 函數指針應該typedef

54.?StructureTypedef:所有結構體應該有typedefs

? ??Example

????struct A// Violation - no typedef

????{

????????int i;

????};

????Repair

????typedef struct A // OK

????{

????????int i;

????} A_t;

????or

????struct A // OK

????{

????????int i;

????};

????typedef struct A A_t;

????note:這個rule僅僅是用于c。

55.?UsingVoid:void should be used when a function is passed or returns no values。

56.?CFilesIncludingCFiles:?.c files may not include other .c files。Many build environments are set up to automatically compile every .c file. If in addition a .c file is included in another .c file, this will result in link errors.

57.?SelectorOperatorsShouldBeConst:Conversion operator, operator->, operator(), operator[] should be const。 操作符重載應該聲明為const。

class A {

public:

????A& operator()( int x ); // Violation

????A& operator[]( int x ); // Violation

????A& operator->( int x ); // Violation

????operator int( ); // Violation

};

Repair

class A {

public:

????A& operator()( int x ) const; // OK

????A& operator[]( int x ) const; // OK

????A& operator->( int x ) const; // OK

????operator int( ) const; // OK

};

60.?OperatorsShouldBeConst:Bitwise operators, comparison operators, logical operators, comma?operator should be const。?presumes that they do not change the internals of the object they are called on. Therefore, it is a good practice to declare them const.

61.?LocalAndMemberNamesDifferByCaseOnly: local 變量/參數 和class/parent class/parent struct 成員變量應該是不同的名字。

62.?LocalAndMemberNamesDifferByOneCharOnly:

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,622評論 6 544
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,716評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,746評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,991評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,706評論 6 413
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 56,036評論 1 329
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,029評論 3 450
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,203評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,725評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,451評論 3 361
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,677評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,161評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,857評論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,266評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,606評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,407評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,643評論 2 380

推薦閱讀更多精彩內容