Tab-size独立的源代码的格式

Executive summary概要

Indentation and alignment should be tab-size independent. 应该tab-size压痕和走向独立。Use one Tab for each indentation level. 使用一个标签,每个凹坑的水平。Use spaces for aligning portions of code.使用空格为调整部分代码。

Explanation and examples解释和实例

Indentation and alignment are two related, but different things. 压痕和对齐是两个有关,但是不同的事情。Toindent缩进means to set a section of code in from the left margin, to facilitate distinguishing “contained” sections (such as a sequence of lines inside an方法建立一组代码,从左边边缘,以促进明显的“包含”[中译文]部分(如在一系列行 ifstatement) from their “containers” (in this case, the声明)从他们的“容器”(在这种情况下, ifstatement itself). 声明本身)。For nested constructs, indentation is carried out in several levels:构建为嵌套、凹坑是进行了在不同的级别。

No indentation
Begin 1
        One level of indentation
        Begin 2
                Two levels of indentation
                etc.
        End 2
End 1

Toalign对齐, on the other hand, means to make parts of the text start on the same column on the screen, to make it look more tidy and clear.另一方面,就是使那些文字部分开始同列在屏幕上,使它看起来更整洁和清晰。

Here is an example that features both indentation and alignment:这里是一个例子,两个压痕和对齐特征:

class person
{
        string name;  // person's full name
        int age;      // age in years
};

The inner part of the class definition (the part between the braces) is indented by one level. 内部类定义(之间的部分牙套)是缩进一个水平。The comments at the end of these two lines are aligned to put them directly below each other on the screen.评论经过这两条线对齐,把它们直接在对方在屏幕上。

Now why are we making such a fuss about the difference between these two? 现在我们为什么做这样一个做文章,这两者之间的区别吗?This is where the tab-size independent part comes in.这是在独立的部分tab-size进来了。

Both indentation and alignment are often done with Tab characters (两个压痕和对准通常需要用制表符('\t'or ASCII或ASCII 0x09). )。And in theory, that would be the best and logical choice. 在理论,那将会是最好的和逻辑的选择。Unfortunately, there is no general agreement about the placement of the tabulator stops in effect.不幸的是,我们没有一般达成了协议tabulator停在就业中的效果。

Traditionally, tab stops are every 8 characters. 传统上,标签止损是每8个字符。Many programmers indent with Tabs because it's only one keypress, but they feel that a tab-size of 8 pushes the code too far to the right, so they change it to 4 characters (or some other value) in their editors. 许多程序员和制表符缩进,因为它只是一个键盘,但他们认为tab-size 8推到正确的代码太远,所以他们换到四个字符(或其他价值)在他们的编辑。When alignment is also performed with tabs this results in misaligned code unless the tab-size is set to the exact same value the author of the code used.当对齐也表现与标签结果偏差的代码,除非tab-size设置为完全相同的价值作者的守则。

Take the personclass definition from above as an example. 从上面的类定义为例。Assume that we had done both indentation and alignment with Tabs, with a tab-size of 8 (here and in the following,假设我们做了两个压痕和结盟的标签,tab-size 8(此处在下面, represents a Tab, while代表一个标签,而 represents a Space):代表一个空间):

|-------|-------|-------|-------|-------|-------|------- <- tab stopsclass person{ string name; // person's full name int age; // age in years};

Now somebody who prefers a tab-size of 4 looks at the code:现在有人喜欢tab-size 4看代码:

|---|---|---|---|---|---|---|---|---|---|---|---|---|--- <- tab stops
class person
{
string name;// person's full name
int age;// age in years
};

The indentation is still correct, but the two comments are now misaligned.缩进仍然是正确的,但是这两条现在偏差。

The default indentation mode of the Emacs editor is even worse: it mixes Tabs (which it assumes to be of size 4) and spaces for both indentation and alignment, with an effective amount of 2 character widths per indentation level. 默认的凹坑模式的编辑器Emacs更糟糕:是将标签(它已经被大小的4)和空间两压痕和排列,以有效的数量的2号人物宽度每缩进层次。The resulting code usually looks like a complete mess for any tab-size setting other than 4.结果代码通常看起来像个一团糟tab-size设置为任何其他4。

So, how do you make it tab-size independent? 所以,这是怎样做的tab-size独立?One solution would be to not use any Tab characters at all. 一个解决办法是不要使用任何制表符。This, however, would hard-code the amount of space used for indentation, something which so many people disagree about.然而,这将hard-code的空间量用于缺口,这么多人反对的东西。

Instead, we adopted a different approach in GiNaC: Tabs are used exclusively for indentation (one Tab per level); spaces are used for alignment. 相反,我们采用了不同的方法在GiNaC:标签专门用于凹坑(一个标签。每升1级+);空间用于对齐。This gets you the best of both worlds: It allows every programmer to change the tab-size (and thus, the visual amount of indentation) to his/her own desire, but the code still looks OK at any setting.这让你两个世界的好处:它允许每个程序员都改变tab-size(因此,视觉数量的凹坑)他/她自己的愿望,但是代码仍然看起来可以在任何设置。

This is how our class definition should be entered using this scheme:这就是我们班的定义应进入使用该方案:

|-------|-------|-------|-------|-------|-------|------- <- tab stops
class person
{
string name;// person's full name
int age;// age in years
};

8 characters indentation are too much for you? 8个字符缩进太多了吗?No problem. 没问题。Just change the tab-size, and it still looks good:只是更改tab-size,看起来还很好:

|---|---|---|---|---|---|---|---|---|---|---|---|---|--- <- tab stops
class person
{
string name;// person's full name
int age;// age in years
};

Some more examples (shown with a tab-size of 4):一些例子(显示与tab-size 4):

// here, we have aligned the parameter declarations
int foo(int i1, int i2, int i3,
string s1, string s2,
vector<int> &result)
{
// inside the function, one level of indentation
if (i1 == i2) {
// inside the "if", two levels of indentation
return 0;
}
// outside the "if", one level again

// indentation is also used here:
static int fibonacci[] = {
1, 2, 3, 5, 8, 13,
21, 34, 55, 89, 144
};

// and here:
int x = bar(
i1 - i2,
i2 - i3,
i3 - i1
);

// continuation lines, however, are aligned, not indented:
cout << "i1 = " << i1 << ", i2 = " << i2 << ", i3 = " << i3
<< ", string1 = " << s1
<< ", string2 = " << s2 << endl;

if (s1 == s2)
return i1;// these two comments
else
return i2 + i3;// are also aligned
}
原文来自:http://www.cebix.net/indentation.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值