xcode 用空格、制表符和多少空格或制表符宽度缩进
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3335966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Indenting with white spaces, tabs, and how many spaces or the tab width
提问by Jesse Armand
I know that this is more of a coding style, instead of a one right way of doing things. But, I'm a bit frustrated if I came across different indenting formats.
我知道这更像是一种编码风格,而不是一种正确的做事方式。但是,如果我遇到不同的缩进格式,我会有点沮丧。
But, I would like to hear the reasons by various people on these issues:
但是,我想听听不同人在这些问题上的原因:
- Do you use spaces or tabs? Tabs with spaces? Any difference with "Tab insert space", instead of using the space key directly?
- How many spaces to indent each line of code? Why?
- Does different code has different style that is more suitable for each of them?
- 你使用空格还是制表符?带空格的标签?与“Tab insert space”有什么区别,而不是直接使用空格键?
- 每行代码缩进多少个空格?为什么?
- 不同的代码有不同的风格,更适合他们每个人吗?
Is there a way to "visually" indent code without actually writing the indent? So it won't ruin the original indenting? It seems unlikely.
有没有办法在不实际编写缩进的情况下“视觉上”缩进代码?所以它不会破坏原来的缩进?似乎不太可能。
I'm using Xcode, so it's better if you have advice for Xcode projects.
我正在使用 Xcode,所以如果您对 Xcode 项目有建议会更好。
回答by John Scipione
You should always use the coding style that the project is already using if you are modifying an existing project.
如果您要修改现有项目,则应始终使用该项目已在使用的编码风格。
That being said, if you are able to choose your own coding style for a new project I suggest that you use tabs to indent code and not spaces -- here is why. By using spaces you force all of the other developers working on the project to conform to your indentation preference whether that be 2 spaces or 4 or 8 (or whatever). But by using tabs each developer can view the code using their own preference. You should uses spaces and not tabs to format text (to line up variable names one per line, or to line up multi-line comments) because that will work with any tab width preference. However, when you indent code use tabs not spaces. In short, indent with tabs because indenting with spaces is rude.
话虽如此,如果您能够为新项目选择自己的编码风格,我建议您使用制表符来缩进代码而不是空格——这就是原因。通过使用空格,您可以强制参与该项目的所有其他开发人员符合您的缩进偏好,无论是 2 个空格还是 4 个或 8 个(或其他)。但是通过使用选项卡,每个开发人员都可以根据自己的喜好查看代码。您应该使用空格而不是制表符来格式化文本(每行排列一个变量名称,或排列多行注释),因为这适用于任何制表符宽度首选项。但是,当您缩进代码时,请使用制表符而不是空格。简而言之,使用制表符缩进,因为用空格缩进是不礼貌的。
回答by just somebody
- spaces because they render the same everywhere
- indent-width is context-dependent. runaway indentation is bad. generally, languages where you cannot easily reset the indentation to sane levels through named abstraction items (functions or methods) need narrow indentation.
- yes, see previous bullet
- 空间,因为它们在任何地方都呈现相同
- 缩进宽度是上下文相关的。失控的缩进很糟糕。通常,无法通过命名抽象项(函数或方法)轻松将缩进重置为合理级别的语言需要窄缩进。
- 是的,请参阅上一个项目符号
the claim that tabs allow individual developers to use different tab widths is false. consider
标签允许个人开发人员使用不同标签宽度的说法是错误的。考虑
typedef __LA_SSIZE_T archive_write_callback(struct archive *,
void *_client_data,
const void *_buffer,
size_t _length);
if this was produced with tabwidth of 2, it would look like this with tabwidth = 4:
如果这是在 tabwidth 为 2 的情况下生成的,则 tabwidth = 4 将如下所示:
typedef __LA_SSIZE_T archive_write_callback(struct archive *,
void *_client_data,
const void *_buffer,
size_t _length);
conversely, if it was produced with tabwidth = 8 and displayed with tabwidth = 4, the result would be
相反,如果它是用 tabwidth = 8 生成并用 tabwidth = 4 显示的,结果将是
typedef __LA_SSIZE_T archive_write_callback(struct archive *,
void *_client_data,
const void *_buffer,
size_t _length);
so if a project style guide requires tabs and says function parameters need to align as in the first code display, then there's a single correct tab width.
因此,如果项目样式指南需要制表符并说函数参数需要像第一个代码显示中那样对齐,那么只有一个正确的制表符宽度。
(another answerpresents the same argument.)
(另一个答案提出了相同的论点。)
a long-overdue edit:
一个姗姗来迟的编辑:
i wholeheartedly agree with the opening sentence of the accepted answer, which is
我完全同意接受的答案的开场白,即
You should always use the coding style that the project is already using if you are modifying an existing project.
如果您要修改现有项目,则应始终使用该项目已在使用的编码风格。
the rest of that answer is, excuse me, rubbish, and i tried to explain why above.
其余的答案是,对不起,垃圾,我试图在上面解释原因。
the question of indentation style should only come up at the very beginning of any project, once it's settled it's done. i dobelieve that spaces are better than tabs, and have (again, i believe) rational arguments in support of my position, etc. but i'm not going to start discussions on this topic because they're useless: most programmers i met support their preferences with irrational arguments, and the rest have settled for a style which works best with their tooling (like me: "spaces because they render the same everywhere"), which i have no influence over in general.
缩进样式的问题应该只出现在任何项目的一开始,一旦确定就完成了。我确实相信空格比制表符更好,并且(再次,我相信)有理性的论据来支持我的立场等等。但我不会开始讨论这个话题,因为它们毫无用处:我遇到的大多数程序员用非理性的论点来支持他们的偏好,而其他人则选择了一种最适合他们的工具的风格(比如我:“空间,因为它们在任何地方都呈现相同”),我一般没有影响力。
anecdote: i once worked on a project which underwent a change of newlines, from \r\n
to \n
, and it turned out to be a pain in the ass any time we needed to go past the revision in svn blame
. s/newlines/indentation/
, and you have a nice argument against massive indentation changes in a running project.
轶事:我曾经参与过一个项目,该项目经历了换行符的更改,从\r\n
到\n
,结果证明任何时候我们需要通过svn blame
. s/newlines/indentation/
,并且您有一个很好的论据反对正在运行的项目中的大量缩进更改。
回答by user497804
An advantage to spaces vs tabs is alignment when wrapping long lines. If you use tabs, no matter what you do, the lines below will most likely not align, unless the editor has the same settings.
空格与制表符的一个优点是在换行时对齐。如果您使用选项卡,无论您做什么,下面的行很可能不会对齐,除非编辑器具有相同的设置。
For example:
例如:
result = variable_one + variable_two + variable_three +
variable_four;
If you use tabs, how to ensure that 'variable_four' will show up aligned if tab indentation changes?
如果您使用制表符,如果制表符缩进发生变化,如何确保“variable_four”将显示对齐?
回答by Philip Regan
I simply use the defaults of the editor I'm currently working in, but if I have the choice I use tabs because it means less invisible characters to manage. I have one editor that wholly manages indentation for me (REALbasic), another editor that manages indentation for me, allows for spaces as prefix but formatting gets wonky if I leave them in (Applescript), and there's Xcode.
I see 4 spaces more than another number for space-based indentation, so I go with that if I have to.
I've found that every community more or less has a standard that has been placed in example code, so I just go with that otherwise tabs then 4 spaces. Simple.
我只是使用我目前使用的编辑器的默认设置,但如果我可以选择,我会使用制表符,因为这意味着要管理的隐形字符更少。我有一个完全为我管理缩进的编辑器(REALbasic),另一个为我管理缩进的编辑器,允许使用空格作为前缀,但如果我将它们留在(Applescript)中,格式就会变得不稳定,并且有 Xcode。
对于基于空格的缩进,我看到的空格比另一个数字多 4 个,因此如果需要,我会使用它。
我发现每个社区或多或少都有一个标准,已放置在示例代码中,所以我只使用其他选项卡然后 4 个空格。简单的。
Honestly, I gave up on the indentation fight a long time ago and expend my brain power on other, what I feel are more important, code-related issues like good variable and functions names. Any reasonable code editor can help you manage poorly-placed or complex indentation but none of them can help with sorting out poorly-written or complex code.
老实说,我很久以前就放弃了缩进之争,把我的脑力花在了其他,我觉得更重要的,代码相关的问题上,比如好的变量和函数名称。任何合理的代码编辑器都可以帮助您管理放置不当或复杂的缩进,但它们都不能帮助整理编写不当或复杂的代码。
回答by Ioan Paul Pirau
There are different programming languages with very different syntax. The syntax is crucial in choosing a coding style. Especially the tab size (number of spaces) that you will chose will depend on how many levels you need to have. In HTML for instance there are many levels, and there it makes no sense to have a big tab size, it even makes no sense in using tabs.. you just use spaces. Another very important aspect is the IDE you use. In some IDE's you are encouraged to use tabs (in Visual Studio you have the nice facility to add/remove tabs to more than one line (TAB to add, SHIFT+TAB to remove) and so on. Projects have different indent sizes because people use different indent settings (even in the same code) or because they use different editors or because the project includes code from other projects or even simply because people have different preferences or even different sized monitors. I work in Visual Studio C++ and VB.NET, indent is tab based(3 lines) and tabs do not insert spaces.
有不同的编程语言具有非常不同的语法。语法对于选择编码风格至关重要。尤其是您将选择的制表符大小(空格数)将取决于您需要拥有多少级。例如,在 HTML 中,有很多级别,并且使用大标签尺寸是没有意义的,甚至在使用标签时也没有任何意义......你只是使用空格。另一个非常重要的方面是您使用的 IDE。在某些 IDE 中,我们鼓励您使用选项卡(在 Visual Studio 中,您可以很好地将选项卡添加/删除到多行(添加 TAB,删除 SHIFT+TAB)等等。项目有不同的缩进大小,因为人们使用不同的缩进设置(即使在相同的代码中),或者因为他们使用不同的编辑器,或者因为项目包含来自其他项目的代码,甚至仅仅是因为人们有不同的偏好甚至不同大小的显示器。我在 Visual Studio C++ 和 VB.NET 中工作,缩进基于制表符(3 行)并且制表符不插入空格。
回答by GalacticCowboy
Depending on your IDE and your "tab inserts spaces" option, it's a nice shortcut over using the spacebar. I.e. at 3 indentation levels with 3 spaces per tab, you press the Tab key 3 times instead of the spacebar 9 times. The net effect is the same - you inserted 9 spaces - but with 1/3 the number of keystrokes.
根据您的 IDE 和您的“制表符插入空格”选项,这是使用空格键的一个不错的快捷方式。即在每个选项卡有 3 个空格的 3 个缩进级别,您按 Tab 键 3 次而不是空格键 9 次。最终效果是相同的 - 您插入了 9 个空格 - 但击键次数减少了 1/3。
回答by Doddy
A benefit of using spaces instead of tabs can make printing your code a bit nicer, especially if you're using a non-typewriter style font. I usually use a width of 3 spaces for each 'level' of indentation.
使用空格而不是制表符的好处是可以使您的代码打印得更好一些,尤其是在您使用非打字机风格的字体时。我通常为每个“级别”的缩进使用 3 个空格的宽度。
回答by Eljay
Because I prefer proportional fonts at a specific 15pt size, I set a TAB to be 37 pixels.
因为我更喜欢特定 15pt 大小的比例字体,所以我将 TAB 设置为 37 像素。
I don't mind that you use FOUR SPACES as long as you also use a font where a SPACE is 37/4 pixels wide. Or TWO SPACES as long as you also use a font where a SPACE is 37/2 pixels. Or even THREE SPACES, as long as you also use a font where a SPACE is 37/3 pixels.
我不介意您使用四个空格,只要您还使用空格为 37/4 像素宽的字体。或者两个空格,只要您还使用空格为 37/2 像素的字体。甚至三个空格,只要您还使用空格为 37/3 像素的字体。
Otherwise, a TAB works great for both us.
否则,TAB 对我们俩都很有用。