明风的博客

1 通用 Rule 1 编译的Warnings不能被忽略掉Rule 2 在已有Code或者三方的code基础上的修改,允许使用原来的coding standardRule 3 如果允许C和C++都访问的相同的C的header 文件, extern C 必须在header文件里 #ifdef __cplusplusextern "C" {#endif/* body of header */#ifdef __cplusplus}#endif2 命名规则 Rule4 所有名字的定义在16个字节范围内,最多不要超过31个Rule5 名字的定义只有字母数字和下划线Rule6 不要使用保留名字Rule7 代码文件以.h, .c 结尾Rule8 代码的文件格式必须保持UNIX file formatRule9 文件名小写,当超过一个单词描述时,用下划线分隔structured_data_display.hstructured_data_display.cRule10 Typedefs, and enumerated types, structure types and union types 小写并用下划线分隔多个单词描述, 结尾应以type结尾typedef UINT8 user_data_struct_typeRule11 宏定义, Enum的值,必须大写并以下划线分隔多个单词 #define USER_DATA_MAX_ERROR_CODES 15enum 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_interface

Rule14 函数的参数小写单词void start_engine(engine_type*myengine,UINT8engine_id,const UINT32* protected_data);Rule15 structure的成员变量有以下规则

名字标记示例

statics_static UINT8 s_some_number

statics_static UINT8 s_some_number

constantc_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*/#endifRule17 相对路径的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 ReservedRevision History:Bug/Feature IDAuthorModification DateDescription—————————————————————————-BugID/FeatureIDdeveloper nameYYYY/MM/DDbrief discription———————————————————————————-*/Rule22 在任何改动或者新建的文件都需要加上公司confidential的标识 Rule23 Revision History 在Feature开发完成或Bug解决完成后必须更新Rule24 所有的Comments必须用英文填写, 并且不能嵌入Rule25 代码缩进的时候不要用Tab,用4个空格代替(跟编辑器有关,可设置)void func(){if (something bad){…if (another thing bad){} }Rule26 Switch 必须有defaultswitch (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;

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 ONE1#define TWO2 (Meets requirements)#define NUM_LOOPS_FOR_AD_READ4#define NUM_SAMPLES_TAKEN8Rule34 一个constant不能付给另外一个constantRule35 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);}#endifExample 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要有 //注释对应的#ifdefRule38 用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 NULLRule34 函数参数是单列数组不需要明确大小 (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_CONSTANTANOTHER_CONSTANT + 1a = ACONSTANT * 2;Flow control, expressionsRule36 Switch case必须要有break结束Rule37 Switch default必须要有来控制以外情况Rule38 不要使用GotoRule39 在比较的时候 左边要放常量// NOT RECOMMENDED, imagine you forget one of the “=” signsIf (PHONE == IS_RESET){}// GOOD, the following statement eliminates the possible errors explained above // and easier to follow the value that you are looking forIf (IS_RESET == PHONE){}Rule40 在使用else if的时候,else的部分需要加上Rule41 if/else/while/for/do的区块无论一行还是多行,一定要加{}// NOT RECOMMENDEDif (something) if (something else)doThis(); elsewhile (input)doThat();// GOODif (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细数门前落叶,倾听窗外雨声,

明风的博客

相关文章:

你感兴趣的文章:

标签云: