1.6 空白符和基本格式化

from http://www.learncpp.com/cpp-tutorial/16-whitespace-and-basic-formatting/

空白符是被用于格式化的字符。在C++中,空白符主要有空格,制表符,换行。在C++编译器中往往使用一些小的处理将空白符忽略。

因此,下面这些语句都是一样的:

   1: cout << "Hello world!";
   2:  
   3: cout               <<            "Hello world!";
   4:  
   5:         cout <<       "Hello world!";
   6:  
   7: cout
   8:     << "Hello world!";

 

即使最后一条语句中有新行也是没有问题的。

下面这些函数的功能是一样的:

   1: int add(int x, int y) { return x + y; }
   2:  
   3: int add(int x, int y) {
   4:     return x + y; }
   5:  
   6: int add(int x, int y)
   7: {    return x + y; }
   8:  
   9: int add(int x, int y)
  10: {
  11:     return x + y;
  12: }

 

有一点需要说明的是,C++编译器会注意到引用中的内容,如"Hello world!"与"Hello       world!"是不同的。

在引号中直接的换行是不允许的,如:

   1: cout << "Hello
   2:      world!" << endl; // Not allowed

另一个需要C++编译器额外注意的是//注释中的空白符。单行的注释,仅仅持续到这一行的结束。像下面这样的做法会使你陷入麻烦中:

   1: cout << "Hello world!" << endl; // Here is a single-line comment
   2: this is not part of the comment

 

基本格式化

不想其他的一些语言,C++并没有强迫程序员任何格式化的限制(记住,仅仅是程序员)。几年下来,有很多不同的C++程序格式化的方法。你会在哪些是最好的问题上发现分歧。我们的基本的经验法则是能够产生易读的,有很好的一致性的代码是最好的风格。

下面是我们对于基本格式话的建议:

1) 制表符设置成4个空格

2) 告诉函数开始与结束的括号拥有独立的一行:

   1: int main()
   2: {
   3: }

 

3) 括号内的每个语句都以一个制表符开始。如:

   1: int main()
   2: {
   3:     cout << "Hello world!" << endl;
   4:     cout << "Nice to meet you." << endl;
   5: }

 

4) 行不要太长。一般情况下,不要超过72,78,或80个字符一行。如果一行太长,它可以被分解为多行。

   1: int main()
   2: {
   3:     cout << "This is a really, really, really, really, really, really, really, " <<
   4:             "really long line" << endl;
   5:     cout << "This one is short" << endl;
   6: }

 

5) 如果被分解的长行中带有操作符,如<<或+,操作符应该放置在行的结尾,而不是行的开头:

   1: cout << "This is a really, really, really, really, really, really, really, " <<
   2:         "really long line" << endl;

 

而不是

   1: cout << "This is a really, really, really, really, really, really, really, " 
   2:         << "really long line" << endl;

 

那样从第一行中很容易看出第二行是继续的。

6) 使用空白符使你的代码变得更加易读。

难读:

   1: nCost = 57;
   2: nPricePerItem = 24;
   3: nValue = 5;
   4: nNumberOfItems = 17;

 

易读

   1: nCost          = 57;
   2: nPricePerItem  = 24;
   3: nValue         = 5;
   4: nNumberOfItems = 17;

 

难读

   1: cout << "Hello world!" << endl; // cout and endl live in the iostream library
   2: cout << "It is very nice to meet you!" << endl; // these comments make the code hard to read
   3: cout << "Yeah!" << endl; // especially when lines are different lengths

 

易读

   1: cout << "Hello world!" << endl;                  // cout and endl live in the iostream library
   2: cout << "It is very nice to meet you!" << endl;  // these comments are easier to read
   3: cout << "Yeah!" << endl;                         // especially when all lined up

 

难读

   1: // cout and endl live in the iostream library
   2: cout << "Hello world!" << endl;
   3: // these comments make the code hard to read
   4: cout << "It is very nice to meet you!" << endl;
   5: // especially when all bunched together
   6: cout << "Yeah!" << endl;

 

易读

   1: // cout and endl live in the iostream library
   2: cout << "Hello world!" << endl;
   3:  
   4: // these comments are easier to read
   5: cout << "It is very nice to meet you!" << endl;
   6:  
   7: // when separated by whitespace
   8: cout << "Yeah!" << endl;

我们会在这个教程中遵循这些惯例,它们会成为你的习惯。当我们介绍一些新的主题时,我们会介绍新的相关风格。

 

从根本上来说,C++给了你选择最适合自己风格的权利。但是,我们强烈建议你使用我们在例子中说明的相同的风格,它是在成千上万的程序员的战斗中成功存活下来的。

posted @ 2012-04-17 08:43  grassofsky  阅读(365)  评论(0编辑  收藏  举报