C语言入门(19)——C语言的编码风格


代码风格好不好就像字写得好不好看一样,如果一个公司招聘秘书,肯定不要字写得难看的,同理,代码风格糟糕的程序员肯定也是不称职的。虽然编译器不会挑剔难看的代码,照样能编译通过,但是和你一个团队进行协作的其他程序员肯定受不了,甚至写完代码几天之后再来看,自己都不知道自己写的是什么。

代码和语言文字一样是为了表达思想、记载信息,所以一定要写得清楚整洁才能有效地表达。正因为如此,在一个软件项目中,代码风格一般都用文档规定死了,所有参与项目的人不管他自己原来是什么风格,都要遵守统一的风格。

下面列出一些代码风格方面的要点供大家参考:

1、每一个函数都必须有注释,即使函数短到可能只有几行。一般的函数说明,应该包含以下几个方面:

名称、函数原型、功能描述、输入参数及类型、输出参数及类型、返回及类型、实现描述、数据库相关表(如果用到的话)。头部说明需要包含包含的内容和次序如下:

/************************************************************************
* Function Name : nucFindThread
* Create Date : 2014/06/07
* Author/Corporation : your name/yourcompany name
**
Description : Find a proper thread inthread array.
* If it’s a new then search an empty.
*
* Param : ThreadNo: someParamdescription
* ThreadStatus: someParamdescription
**
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 2014/06/21 your name … …
************************************************************************/
static unsigned char nucFindThread(unsignedchar 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个空格的和8个空格的规定。其中有不少的大公司使用4个的。但是也有不少使用8个的。其实4个的看起来比较舒服。个人推荐使用8个的。(不使用TAB缩进)。每个编辑器的TAB键定义的空格数不一致,可能导致在别的编辑器打开你的代码乱成一团糟。

#include "stdio.h"
int main()
{
       float a;
       printf("Please input a number: /n");
       scanf("%f",&a);
       printf("%6.2f /n",a);
       return (1);
}


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

 9、同层次的代码在同层次的缩进层上

10、代码行最大长度宜控制在80 个字符以内,较长的语句、表达式等要分成多行书写。

11、长表达式要在低优先级操作符处划分新行,操作符放在新行之首(以便突出操作符)。拆分出的新行要进行适当的缩进,使排版整齐,语句可读。例如:

if ((very_longer_variable1 >=very_longer_variable12)
     &&(very_longer_variable3 <=very_longer_variable14)
     &&(very_longer_variable5 <=very_longer_variable16))
    {
       dosomething();
    }
    for (very_longer_initialization;
         very_longer_condition;
         very_longer_update)
    {
        dosomething();
    }


12、如果函数中的参数较长,则要进行适当的划分。例如:

void function(float very_longer_var1,
float very_longer_var2,
float very_longer_var3)

13、用正确的反义词组命名具有互斥意义的变量或相反动作的函数等。例如:

int aiMinValue;
int aiMaxValue;
int niSet_Value(…);
int niGet_Value(…);


14、如果代码行中的运算符比较多,用括号确定表达式的操作顺序,避免使用默认的优先级。例如:

  leap_year = ((year % 4 == 0) && (year % 100 != 0)) || (year %400 == 0);

15、不要编写太复杂的复合表达式。例如下面的复合表达式过于复杂:

   i= a >= b&&c < d && c + f <= g + h; 

16、不要有多用途的复合表达式。例如:

   d= (a = b + c) + r;


该表达式既求a 值又求d 值。应该拆分为两个独立的语句:

   a= b + c;
   d= a + r;

 

17、尽量避免含有否定运算的条件表达式。例如:

   if(!(num >= 10))
应改为:
   if(num < 10)

18、参数的书写要完整,不要贪图省事只写参数的类型而省略参数名字。如果函数没有参数,则用void 填充。

  

19、变量命名一般使用匈牙利命名规则:就是在变量名前添加表示变量类型的缩写来命名变量。比如int型的变量命名可以用iLoop来表示,string类型的用strName。不过这些已经不一定被采用了。大概是因为现在的编译器的改进和其命名的繁琐。如今的Microsoft .net下你如果将鼠标停留在变量上一段时间就可以见到相关提示。Eclipse下的同样如此。

    在局部变量时可以使用l前缀。而全局变量科研使用g前缀加以区分。如果没有这样的机制,在程序编程时有可能出现同名。从而局部变量将会覆盖全局变量带来麻烦等。

    常量可以使用全部大写以区别。 变量应该使用具有具体意思的命名。

原文地址:https://www.cnblogs.com/new0801/p/6177127.html