C++ Coding Standard

1 常見

Rule 1 編譯的Warnings不能被忽略掉

Rule 2 在已有Code或者三方的code基礎上的改動,贊成使用原來的coding standard

Rule 3 假設贊成C和C++都訪問的一樣的C的header 文件, extern C 必須在header文件中

#ifdef __cplusplus
extern "C" {
#endif

/* body of header */

#ifdef __cplusplus
}
#endif

2 命名規則

Rule4 所有名字的定義在16個字節範圍內。最多不要超過31個

Rule5 名字的定義僅僅有字母數字和下劃線

Rule6 不要使用保留名字

Rule7 代碼文件以.h, .c 結尾

Rule8 代碼的文件格式必須保持UNIX file format

Rule9 文件名稱小寫,當超過一個單詞描寫敘述時,用下劃線分隔

structured_data_display.h	
structured_data_display.c

Rule10 Typedefs, and enumerated types, structure types and union types 小寫並用下劃線分隔多個單詞描寫敘述, 結尾應以type結尾

typedef UINT8 user_data_struct_type

Rule11 宏定義, Enum的值,必須大寫並下面劃線分隔多個單詞

 #define USER_DATA_MAX_ERROR_CODES    15
enum user_data_struct_type
{
    USER_DATA_ERROR_INVALID_DATA,
    USER_DATA_ERROR_FAILURE
 };

Rule12 函數名必須是動詞並且小寫和下劃線分隔多個單詞

handle_error 
check_for_error               /* NOT errorCheck */

Rule13 全局變量必須以g開頭並下面劃線分隔單詞

g_default_interfacelinux

Rule14 函數的參數小寫單詞

void start_engine(engine_type*        myengine, 
                        UINT8          engine_id,
                        const UINT32*  protected_data);

Rule15 structure的成員變量有下面規則

名字 標記 演示樣例
static s_ static UINT8 s_some_number
static s_ static UINT8 s_some_number
constant c_ const UINT8 c_some_number
  cp_ const user_data_type* cp_user_data

3 Files

Rule16 每個include的必須有機制防止多重包括

#ifndef FOO_H
#define FOO_H
        
/* The rest of the file*/
  	      
#endif

Rule17 相對路徑的linux文件不一樣意出現在#include裏

// NOT ALLOWED
#include <../foo/include/foo.h>

Rule18 頭文件不一樣意有實現的代碼

Rule19 每個頭文件僅僅include 其它有依賴的,包括相關定義的頭文件,不include 全局性的頭文件

Rule20 代碼文件(.c)需要include 全局頭文件以及相關頭文件

4 代碼風格和間隔

Rule21 在不論什麼修改的文件都需要加上公司confidential的標識

/* -------------------------------------------------------------------------------
     Copyright (C) 2011, Nollec Wireless  CO. LTD. All Rights Reserved

     Revision History:
       Bug/Feature ID         Author                   Modification Date                      Description 
     ------------------       -------------------         ------------------            ---------------------
      BugID/FeatureID      developer name       YYYY/MM/DD                          brief discription

----------------------------------------------------------------------------------*/

Rule22 在不論什麼修改或者新建的文件都需要加上公司confidential的標識

Rule23 Revision History 在Feature開發完畢或Bug解決完畢後必須更新

Rule24 所有的Comments必須用英文填寫, 並且不能嵌入

Rule25 代碼縮進的時候不要用Tab。用4個空格取代

(跟編輯器有關,可設置)
void func()
{
      if (something bad)
      {
          ...
          if (another thing bad)
         {

         }
 }

Rule26 Switch 必須有default

switch (testValue)
{
    case VALUE_1:
        /* Body for case 1. */
        break;
    case VALUE_2:
        /* Body for case 2. */
        break;
    default:
        /* Body for default. */
        break;
}

Rule27 不要在 . 或-> 先後有空格

Rule28 變量的聲明應儘可能控制到小的範圍內

Rule29 指針 * 應該緊跟類型以後。 地址 &應該在變量名以前

Rule30 變量常量定義必定要有意義。不要用magic number

x = 100*10+1; //NOT ALLOWEDnumber = 100*10+1; express

Rule31 每個變量定義都要分行寫,而且必定要給初值

UINT8 i, j;              /* NOT RECOMMENDED */
UINT8 i = INIT_VALUE;    /* GOOD */
UINT8 j = INIT_VALUE;    /* GOOD */

Rule31 所有狀態變量或者enum的變量要明白賦予其值

/* INCORRECT*/
if (cpCurrentState == CP_L3_STATE_TYPE_D1)
{
    cpCurrentState++;
} 
/*CORRECT*/
if (cp_currentState == CP_L3_STATE_TYPE_D1)
{
    cp_currentState = CP_L3_STATE_TYPE_D2;
}

Rule32 所有global的聲明都要在一個header文件中

Rule33 常量聲明需要有明白含義

(Does not meet requirements)
#define ONE			1
#define TWO			2

 (Meets requirements)
#define NUM_LOOPS_FOR_AD_READ	4
#define NUM_SAMPLES_TAKEN		8

Rule34 一個constant不能付給另一個constant

Rule35 NULL僅僅用於指針的初始化和比較

Rule36 常量用在編譯開關時候,必定要在編譯控制的區域內

#define DEBUG 4  
 /* if undefined, disables debugging */
/* set to 1 - 4 for desired debugging level */
/* This usage of DEBUG in the for loop control statement is  
 * allowed since the statement is fully enclosed within the
 * conditionally compiled section of code.
 */
#ifdef DEBUG 
for (i = 0; i < DEBUG; i++)  
{
    printf("i = %d\n", i);
}
#endif

Example 31 -	Example (Incorrect Usage)
#define DEBUG 4
#ifdef DEBUG       /* usage here is fine */ 
    /* do something special */
#endif
/* the code statement below is outside the segment controlled by 
 * the #ifdef and therefore should NOT use DEBUG.
 */
for (i = 0; ((i < 5) && DEBUG); i++) 
{
    printf("i = %d\n", i);
}

Rule37 #ifdef #endif 用於編譯時要有足夠的凝視,而且#endif要有 //凝視相應的#ifdef

Rule38 用typedef 而不是#define來定義類型

Rule39 The following types shall be defined in a global header file and shall be used in place of the C language int types:

•	INT8: This type stores 8-bit signed numeric values.
•	UINT8: This type stores 8-bit unsigned numeric values.
•	INT16: This type stores 16-bit signed numeric values.
•	UINT16: This type stores 16-bit unsigned numeric values.
•	INT32: This type stores 32-bit signed numeric values.
•	UINT32: This type stores 32-bit unsigned numeric values.
•	BOOLEAN: Ideally, this type is an enumerated type that names the boolean values TRUE (1) and FALSE (0). However, some compilers may not store this efficiently. An alternative is typedef UINT8 BOOLEAN;
That simply defines BOOLEAN as an 8-bit unsigned value which is distinct from UINT8. In this case, the constants TRUE and FALSE will have to be defined constants.

Rule31 一個函數應該僅僅有一個return出口

Rule32 不要使用沒有明白定義的參數

Rule33 函數return指針假設failure。則需要return NULL

Rule34 函數參數是單列數組不需要明白大小

 (Correct Usage)
void functionName
(
    UINT8 x[],  /* array with "size" elements */
    UINT8 size  /* number of elements in x */
);
(Incorrect Usage)
void functionName (UINT8 x[10]);

Rule35 不論什麼宏定義的拓展需要加上括號來限定

 (Correct):
#define A_CONSTANT	(ANOTHER_CONSTANT + 1)
a = ACONSTANT * 2;
(Incorrect):
#define A_CONSTANT	ANOTHER_CONSTANT + 1
a = ACONSTANT * 2;

Flow control, expressions

Rule36 Switch case必須要有break結束

Rule37 Switch default必須要有來控制之外狀況

Rule38 不要使用Goto

Rule39 在比較的時候 左邊要放常量

// NOT RECOMMENDED, imagine you forget one of the 「=」 signs
If (PHONE == IS_RESET)
{
}
// GOOD, the following statement eliminates the possible errors explained above 
// and easier to follow the value that you are looking for
If (IS_RESET == PHONE)
{
}

Rule40 在使用else if的時候,else的部分需要加上

Rule41 if/else/while/for/do的區塊無論一行仍是多行,必定要加{}

// NOT RECOMMENDED
if (something)
   if (something else)
       doThis();
   else
       while (input)
           doThat();

// GOOD
if (something)
{
   if (something else)
   {
       doThis();
   }
   else
   {
       while (input)
       {
           doThat();
       }
   }
}

Memory

Rule42 必定保證從調用函數分配的內存在使用完成後釋放內存, 必定在此調用函數凝視提醒

Dangerous memory management   
error_code_type* myfunction(void)
{
    error_code_type* p_temp = malloc (sizeof(error_code_type));
    return p_temp;
    /* p_temp is never de-allocated and the user of myFunc cannot de-allocate*/
    /* because a temporary copy of that instance is returned.*/
    /* Calling user code shall take of the memory deallocating which will create*/
    /* complexity and confusion*/
}

Rule43 在deference引用指針以前必定要對指針斷定是否爲NULL

UINT32* p_number = NULL;if (NULL == p_number){}數組

其它

Rule44 編譯器相關的擴展在有必要的時候使用

#ifdef _HC11_
#define DIRECT _direct_ram
#else /* not _HC11_ */
#define DIRECT
#endif

/* nSize located in direct RAM in embedded, normal RAM in UNIX */
DIRECT UINT8 nSize;

Rule45 編譯器相關的擴展在有必要的時候使用

Rule46 所有丟失性的類型轉換要明白cast

INT8   signed_value;
UINT16 long_value;
UINT8  short_value;

/* Loss here is in accuracy going from signed to unsigned */
signed_value= (UINT8) short_value;
/* Loss here is in size, going from a 16 bit value to an 8 bit */
signed_value= (INT8) long_value;

Rule47 數組的訪問僅僅能用方括號的形式訪問,不要使用deference * 來訪問

/*INCORRECT*/
*(masterList + 10) = 0;

/*CORRECT*/
masterList[10] = 0;

Rule48 於condition若是肯定要利用確定的邏輯

版權聲明:本文博主原創文章,博客,未經贊成不得轉載。less

相關文章
相關標籤/搜索