C++ 自学教程 LearnCPP 第1.6章 留白与C++格式化基础

C++ 自学教程 第1.6章 留白与C++格式化基础


留白指代那些用于将代码格式化的空白字符。在C++里,这些字符主要由空格,Tab,和空白行组成的。除了一些例外情况,C++编译器通常忽略留白。

因此,下面这些叙述语句在C++中是等价的:

std::cout << "Hello world!";

std::cout               <<            "Hello world!";

        std::cout <<        "Hello world!";

std::cout
    << "Hello world!";

甚至最后一个包括空白行的叙述语句也可以被正确编译。

下面这些函数也是等价的:

int add(int x, int y) { return x + y; }

int add(int x, int y) {
    return x + y; }

int add(int x, int y)
{    return x + y; }

int add(int x, int y)
{
    return x + y;
}

有一种留白C++编译器不会忽略,就是文本内部的空白,比如说:“Hello world!”

“Hello world!”

“Hello world!” 是不同的。
文本(被引号包含的)内部是不允许存在新行的。

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

另外一个例外是用“//”定义的单行注释。单行注释只对本行有效。所以下面的程序无法正常编译:

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

格式化基础

与其他很多编程语言不同, C++并没有硬性的格式限制(记得之前说的无条件信赖程序员吗?)。自C++被发明以来,人们渐渐开发了许多将C++程序格式化的方法。你可能会发现多数情况是公说公有理婆说婆有理:各种方法有各自的优缺。我们这里定义的黄金定律就是:能够得到可读性最强的代码,并且能在尽可能多的情况保持一致。

下面使我们推荐的基础格式:

1) 你的Tab应该被设置为4个空白(多数IDE遵从这个设定)。有的IDE设置为3个空白,这样有点违反常规不过还可以。
你可以使用tabs和空格缩进。多数大企业和格式指南都会推荐使用空格因为其结果一致性最高。不过我个人不觉得这一点很有说服力,并更愿意使用tab来缩进。
(关于tab和空格哪个更好的争论已经持续了50多年之久,这个问题没有正确答案,只要和你自己的,还有你的公司的代码保持一致就可以了。)

2) 定义函数起止位置的花括号应该与函数名对准,并且独立地占据一行位置:

尽管有些程序员习惯不一样的风格,这样写代码的可读性最强,并且最不容易犯错。因为两个括号的对准位置一致,调试代码的时候很容易检查左右括弧数目是否匹配。

int main()
{
}

3) 花括弧内的每一个叙述语句应距离括号位置一个tab(四个空格)。比方说:

#include <iostream>
int main()
{
    std::cout << "Hello world!" << std::endl; // tabbed in one tab (4 spaces)
    std::cout << "Nice to meet you." << std::endl; // tabbed in one tab (4 spaces)
}

4) 每一行代码都不应该太长。通常情况下,72,78,或者80个字符为上限。如果某行需要更长,那应该在中间把它断开:下半行前面再加一个tab。如果两个半行内容很近似,那么应该上下对齐。举例:

#include <iostream>
int main()
{
    std::cout << "This is a really, really, really, really, really, really, really, " <<
        "really long line" << std::endl; // one extra indentation for continuation line

    std::cout << "This is another really, really, really, really, really, really, really, " <<
                 "really long line" << std::endl; // text aligned with the previous line for continuation line

    std::cout << "This one is short" << std::endl;
}

5) 如果断行位置前后有运算符号,那么运算符号应该放在上一行的末端,而不是新行的起始位置:

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

而不是:

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

这样让人比较容易看出下半行跟上半行的链接关系。

6) 活用留白,让你的代码更容易被看懂。

错误示范:

nCost = 57;
nPricePerItem = 24;
nValue = 5;
nNumberOfItems = 17;

优秀示范:

nCost          = 57;
nPricePerItem  = 24;
nValue         = 5;
nNumberOfItems = 17;

错误示范:

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

优秀示范:

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

错误示范:

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

优秀示范:

// std::cout and std::endl live in the iostream library
std::cout << "Hello world!" << std::endl;

// these comments are easier to read
std::cout << "It is very nice to meet you!" << std::endl;

// when separated by whitespace
std::cout << "Yeah!" << std::endl;

在这份教程里,我们会遵守以上这些准则,你也会渐渐习惯这些做法。后面介绍新内容的时候,也会向读者盆友们介绍相应的推荐编写格式。

虽然说C++让程序员自由选择自己最喜欢的,或者最佳的格式。但是,我们强烈建议你遵循上面给出的这些格式。千千万万的程序员先辈们针对哪些格式最佳打了无数嘴仗,(希望)以上智慧结晶能够助你编程顺利,然后“升职加薪、当上总经理、出任CEO、迎娶白富美、走上人生巅峰,想想还有点小激动呢”。


说明:这系列笔记是基于网上一个英文教程LearnCPP

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值