代码改变世界

C语言编码风格

2013-06-27 13:40  fvdre  阅读(180)  评论(0)    收藏  举报

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、同层次的代码在同层次的缩进层上。例如:


10、代码行最大长度宜控制在80 个字符以内,较长的语句、表达式等要分成多行书写。
转载请注明圣安娜娱乐http://www.leruntoys.com