设为首页 加入收藏

TOP

C语言编码风格
2012-11-05 11:37:03 来源: 作者: 【 】 浏览:367
Tags:语言 编码 风格
很多人不重视这点,认为无所谓,甚至国内的绝大多数教材也不讨论这个话题,导致学生入公司后仍要进行编码风格的教育。我接触过很多学生,发现他们由于平时缺乏这种意识,养成了不好的习惯,导致很难改正过来。代码没有注释,变量、函数等命名混乱,过两天自己都看不懂自己的代码。下面是一些我见过的比较好的做法,希望读者能有所收获。

1、每一个函数都必须有注释,即使函数短到可能只有几行。头部说明需要包含包含的内容和次序如下:
/************************************************************************
* Function Name : nucFindThread
* Create Date : 2000/01/07
* Author/Corporation : your name/your company name
**
Description : Find a proper thread in thread array.
* If it’s a new then search an empty.
*
* Param : ThreadNo: someParam description
* ThreadStatus: someParam description
**
Return Code : Return Code description,eg:
ERROR_Fail: not find a thread
ERROR_SUCCEED: found
*
* Global Variable : DISP_wuiSegmentAppID
* File Static Variable : naucThreadNo
* Function Static Variable : None
*
*------------------------------------------------------------------------
* Revision History
* No. Date Revised by Item Description
* V0.5 2008/01/07 your name … …
************************************************************************/
static unsigned char nucFindThread(unsigned char ThreadNo,unsigned char ThreadStatus)
{

   }

2、每个函数定义结束之后以及每个文件结束之后都要加一个或若干个空行。例如:
/************************************************************************
* ………
* Function1 Description
* ………
************************************************************************/
void Function1(……)
{
   …
}
//Blank Line
/************************************************************************
* ………
* Function2 Description
* ………
************************************************************************/
void Function2(……)
{
   …
}
//Blank Line
/************************************************************************
* ………
* Function3 Description
* ………
************************************************************************/
void Function3(……)
{
   …
}
//Blank Line

3、在一个函数体内,变量定义与函数语句之间要加空行。例如:
/************************************************************************
* ………
* Function Description
*………
************************************************************************/
void Function1()
{
   int n;
   //Blank Line
   statement1
   …….
}

4、逻揖上密切相关的语句之间不加空行,其它地方应加空行分隔。例如:
//Blank Line
while (condition)
{
   statement1;
   //Blank Line
   if (condition)
   {
      statement2;
   }
   else
   {
      statement3;
   }
   //Blank Line
   statement4
}

5、复杂的函数中,在分支语句,循环语句结束之后需要适当的注释,方便区分各分支或循环体。
while (condition)
{
   statement1;
   if (condition)
   {
      for(condition)
      {
         Statement2;
      }//end “for(condition)”
   }
   else
   {
      statement3;
   }//”end if (condition)”
   statement4
}//end “while (condition)”

6、修改别人代码的时候不要轻易删除别人的代码,应该用适当的注释方式。例如:
while (condition)
{
   statement1;
   //////////////////////////////////////
   //your name , 2008/01/07 delete
   //if (condition)
   //{
   // for(condition)
   // {
   // Statement2;
   // }
   //}
   //else
   //{
   // statement3;
   //}
   ////////////////////////////////////////
   ///////////////////////////////////////
   // your name , 2000/01/07 add
   …
   new code
   …
   ///////////////////////////////////////
   statement4
}

7、用缩行显示程序结构,使排版整齐,缩进量统一使用4个字符(不使用TAB缩进)。
每个编辑器的TAB键定义的空格数不一致,可能导致在别的编辑器打开你的代码乱成一团糟。

8、在函数体的开始、结构/联合的定义、枚举的定义以及循环、判断等语句中的代码都要采用缩行。

9、同层次的代码在同层次的缩进层上。例如:
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言函数设计的一般原则和技巧 下一篇C语言函数的由来与好处

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: