告诉函数开始和结束

来源:互联网 发布:java怎么运行 编辑:程序博客网 时间:2024/06/14 03:21
基本格式


不像一些其他的语言,C++不执行任何一种格式对程序员的限制(记住,信任程序员!)。许多不同格式的C++程序的方法已经在多年的发展,你会发现分歧上哪个是最好的选择。我们的基本经验是,最好的样式是产生最可读代码,并提供最一致性的代码。


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


1)你的标签应设置为4位(大部份有此配置)。一些IDE默认为3位,这是好的。


使用空格代替制表符的原因是,如果在另一个查看器中打开代码,它将保留正确的缩进。


2)告诉函数开始和结束位置的括号应该与函数名对齐,并按自己的行排列:

#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)}
在本教程中,我们将遵循这些约定,它们将成为您的第二天性。当我们向您介绍新主题时,我们将介绍与这些特性一起使用的新样式建议。


最终,C++给你的权力,选择哪一种方式你最舒服,或认为是最好的。但是,我们强烈建议您使用与我们示例相同的样式。数千名程序员已经完成了数十亿行代码的测试,并成功地进行了优化。

12345678// std::cout and std::endl live in the iostream librarystd::cout << "Hello world!" << std::endl; // these comments are easier to readstd::cout << "It is very nice to meet you!" << std::endl; // when separated by whitespacestd::cout << "Yeah!" << std::endl;

这使得从第一行看,下一行将是一个延续更为明显。


6)使用空格使你的代码更容易阅读。


难以阅读:

#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;}


阅读全文
0 0
原创粉丝点击