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_interface
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;
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如果确定要利用肯定的逻辑
C Coding Standard的更多相关文章
- Integration_Unit test coding standard
Integration & Unit test coding standard 命名规则 好的命名规则,直接从命名就可以清楚的知道该测试方法测试的内容和目的,而不用额外的添加注释说明.对于MV ...
- 16/7/8_PHP-书写规范 PHP Coding Standard
变量命名规范这里感觉 打算采用 匈牙利命名法+驼峰法命名,因为 PHP是弱类型语言,很多时间因为忽略了变量类型而导致犯一些低级错误.所以在前面加上类型名有助于更好的理解代码. 下载是转载 PHP书写规 ...
- C# Coding Conventions(译)
C# Coding Conventions C#编码规范 Naming Conventions 命名规范Layout Conventions 布局规范Commenting Conventions 注释 ...
- coding规约的网站, 从sonar中链接过去
一个coding规约的网站, 从sonar中链接过去的. 挺好. https://wiki.sei.cmu.edu/confluence/display/seccode/SEI+CERT+Coding ...
- Change - Why we need coding standards
Change - Why we need coding standards I have the idea of coding standards when I have to review my t ...
- Deep Learning-Based Video Coding: A Review and A Case Study
郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! 1.Abstract: 本文主要介绍的是2015年以来关于深度图像/视频编码的代表性工作,主要可以分为两类:深度编码方案以及基于传统编码方 ...
- C# Coding Conventions, Coding Standards & Best Practices
C# Coding Conventions, Coding Standards & Best Practices Cui, Chikun Overview Introduction This ...
- 个人作业-Week2
第一部分 调研, 评测 运行平台 win 8 软件版本:微软必应词典桌面版 3.5.2 BUG标题:必应背单词无法发音 BUG详细描述:如图,左边为必应词典该单词的搜索,可以发音,而右边必应背单词中 ...
- 谈谈PHP代码规范
[转] http://www.syyong.com/php/Talk-about-PHP-code-specification.html 我向往这样一个php世界,里面没有代码规范之争.你我都一样,都 ...
随机推荐
- 项目优化经验分享(六)SVN冲突和处理
上一篇博客我们分享了新增需求的确定思想<站在全局看问题>.今天我们来分享项目开发中SVN冲突的解决经验:SVN冲突和处理! 引言 开发过项目的人都知道,公司开发一个项目都会使用到版本号控制 ...
- PHP - 字符串操作
第8章 字符串处理 学习要点: 1.字符串格式化 2.操作子字符串 3.字符串比较 4.查找替换字符串 5.处理中文字符 在每天的编程工作中,处理.调整以至最后控制字符串是很重要的一部分,一般也认为这 ...
- FileStream -- 复制文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- c语言,数组和字符串
1. “数组名代表了数组的存储首地址,是一个地址常量”. 对于char *p1 = "A String."; 和 char p2[] = "Another String. ...
- lua语法 - 基础篇
1. 注释 单行注释:--,类似于C++的// 多行注释:--[[ ... ]],类似于C++的/*...*/ 2. 语句 分隔符:分号或者空格,一般多行写一起,建议用分号 语句块:do ... en ...
- QWidget类中默认是忽略inputMethodEvent事件(要获取输入的内容就必须使用这个事件)
因为项目的需要以及主管的要求,准备将工程移植到Qt中,这样就可以比较容易的实现跨平台了.因为之前工程是在windows下开发的,第一个平台又是mobile所以除了底层框架之外其他的都是使用的windo ...
- c:foreach如何嵌套循环,求指教,求优化
java类: public class PopedomItem { private String id; private String pid; private String name; privat ...
- 设置MAVEN_OPTS环境变量
运行mvn命令实际上是执行了Java命令,既然是运行Java,那么运行Java命令可用的参数当然也应该在运行mvn命令时可用.这个时候,MAVEN_OPTS环境变量就能派上用场. 通常需要设置MAVE ...
- oc 可变參数传递
- (id)initWithFrame:(CGRect)frame delegate:(id<SGFocusImageFrameDelegate>)delegate focusImageI ...
- linux命令:scp
有时候ftp被禁用了, 就用scp替代; 命令行: scp from to_user@to_ip:dir_to/file_name 执行该命令之后,按照提示输入to_host的登陆密码即可. scp ...