javascript 源代码中的 TAB 字符是坏的吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9231688/
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
Is the TAB character bad in source code?
提问by Maverick
I'm pretty familiar I guess with both Zend and PEAR PHP coding standards, and from my previous two employers no TAB characters were allowed in the code base, claiming it might be misinterpreted by the build script or something (something like that, I honestly can't remember the exact reason). We all set up our IDE's to explode TABs to 4 spaces.
我想我对 Zend 和 PEAR PHP 编码标准都很熟悉,而且从我的前两个雇主那里,代码库中不允许使用 TAB 字符,声称它可能被构建脚本或其他东西误解了(类似的东西,老实说具体原因记不清了)。我们都设置了我们的 IDE,以将 TAB 分解为 4 个空格。
I'm very used to this, but now my newest employer insists on using TABs and not spaces for indentation. I suppose I shouldn't really care since I can just tell PHP Storm to just use the TAB char when i hit the Tabkey, but, I do. I want spaces and I'd like a valid argument for why spaces are better than TABs.
我对此非常习惯,但现在我的新雇主坚持使用制表符而不是缩进空格。我想我真的不应该在意,因为我可以告诉 PHP Storm 在我按下Tab键时只使用 TAB 字符,但是,我这样做了。我想要空格,并且我想要一个有效的论据来说明为什么空格比 TAB 更好。
So, personal preferences aside, my question is, is there a legitimate reason to avoid using TABs in our code base?
所以,撇开个人喜好不谈,我的问题是,是否有正当理由避免在我们的代码库中使用 TAB?
Keep in mind this coding standard applies to PHP and JavaScript.
请记住,此编码标准适用于 PHP 和 JavaScript。
回答by user1429980
Tabs are better
标签更好
- Clearerto reader what level each piece of code is on; spaces can be ambiguous, espcially when it's unclear whether you're using 2-space tabs or 4-space tabs
- Conceptually makes more sense. When you indent, you expect a tab and not spaces. The document should represent what you did on your keyboard, and not in-document settings.
- Tabs can't be confused with spacesin extended lines of code. Especially when word warp is enabled, there could be a space in a wrapped series of words. This could obviously confuse the readers. It would slow down paired programming.
- Tabs are a special character. When viewing of special characters is enabled on your IDE, levels can be more easily identified.
- 更明确的读者什么样的水平每一段代码是; 空格可能不明确,特别是当不清楚您使用的是 2 空格制表符还是 4 空格制表符时
- 从概念上讲更有意义。缩进时,您需要的是制表符而不是空格。该文档应该代表您在键盘上所做的事情,而不是文档中的设置。
- 标签不能与扩展代码行中的空格混淆。尤其是当启用 word warp 时,在一系列环绕的单词中可能会有一个空格。这显然会使读者感到困惑。它会减慢结对编程的速度。
- 制表符是一个特殊字符。在 IDE 上启用查看特殊字符时,可以更轻松地识别级别。
Note to all coders: you can easily switch between tabs and spaces using any of JetBrains' editors (ex. PHPStorm, RubyIDE, ReSharper, IntelliJIDEA, etc.) by simply pressing CTRL + ALT + L on Windows, Mac, or Linux.
所有编码人员请注意:您可以使用任何 JetBrains 的编辑器(例如 PHPStorm、RubyIDE、ReSharper、IntelliJIDEA 等)在选项卡和空格之间轻松切换,只需在 Windows、Mac 或 Linux 上按 CTRL + ALT + L。
回答by webbiedave
is there a legitimate reason to avoid using TABs in our code base?
是否有正当理由避免在我们的代码库中使用 TAB?
I have consulted at many a company and not once have I run into a codebase that didn'thave some sort of mixture of tabs and spaces among various source files and not once has it been a problem.
我曾在许多公司咨询过,而且我从未遇到过在各种源文件中没有某种制表符和空格混合的代码库,而且从来没有出现过问题。
Preferred? Sure.
首选?当然。
Legitimate, as in accordance with established rules, principles, or standards? No.
根据既定规则、原则或标准,合法? 不。
Edit
编辑
All I really want to know is if there's nothing wrong with TABs, why would both Zend and PEAR specifically say they are not allowed?
我真正想知道的是,如果 TAB 没有任何问题,为什么 Zend 和 PEAR 都会明确表示不允许使用?
Because it's theirpreference. A convention they wish to be followed to keep uniformity (along with things like naming and brace style). Nothing more.
因为这是他们的偏好。他们希望遵循的约定以保持一致性(以及命名和括号样式等)。而已。
回答by Fred Foo
Spaces are better than tabs because different editors and viewers, or different editor settings, might cause tabs to be displayed differently. That's the only legitimate reason for avoiding tabs if your programming language treats tabs and spaces the same. If some tool chokes on tabs, then that tool is broken from the language point of view.
空格优于选项卡,因为不同的编辑器和查看器,或不同的编辑器设置,可能会导致选项卡的显示方式不同。如果您的编程语言对制表符和空格的处理方式相同,那么这是避免使用制表符的唯一正当理由。如果某些工具在选项卡上卡住,那么从语言的角度来看,该工具已损坏。
Even when everybody in your team sets their editor to treat tabs as four spaces, you'll get a different display when you have to open up your source code in some tool that doesn't.
即使您团队中的每个人都将他们的编辑器设置为将选项卡视为四个空格,当您必须在某些不这样做的工具中打开源代码时,您也会得到不同的显示。
回答by hugomg
The most important thing to worry about is being consistentabout always using the same indentation scheme - having a confused mix of tabs and spaces is living hell, and is worse then either pure tabs or pure spaces. Therefore, if the rest of the project is using tabs you should use them too.
需要担心的最重要的事情是始终使用相同的缩进方案保持一致- 将制表符和空格混合在一起是地狱般的生活,而且比纯制表符或纯空格更糟糕。因此,如果项目的其余部分正在使用选项卡,您也应该使用它们。
Anyway, there isn't a clear winner on Tabs vs Spaces. Space supporters say that the using only spaces for everything is a simper rule to enforce while Tabs supporters say that using tabs for indentation and spaces for alignment allows different developers to display the tab-width they find more comfortable.
无论如何,Tabs 与 Spaces 没有明显的赢家。空格支持者说,所有东西都只使用空格是一个简单的规则,而制表支持者说,使用制表符进行缩进和使用空格进行对齐允许不同的开发人员显示他们认为更舒服的制表符宽度。
In the end, tabs-vs-spaces is should not be a bid deal. The only time I have seem people argue that one of the alternatives is strictly better then the other is in indentation-sensitive languages, like Python or Haskell. In these mixing tabs and spaces can change the program semantics in hard to see ways, instead of only making the source code look weird.
最后,tabs-vs-spaces 不应该是出价交易。我唯一一次似乎有人争辩说,其中一种替代方案绝对优于另一种是使用缩进敏感语言,例如 Python 或 Haskell。在这些混合制表符和空格中,可以以难以看到的方式改变程序语义,而不仅仅是使源代码看起来很奇怪。
回答by Highroller
Ever since my first CS class, tabs have always been taboo. Reason being, tabs are basically like variables. Different IDE's can define a TAB as a different number of spaces. Speaking from a Visual Studio/NetBeans/DevC++ perspective, all have the capacity to change the 'definition' of a TAB based on number of desired spaces. So if you have 4 spaces defined, there is no way that you can know if my IDE says 3 spaces or 5 spaces. So if anyone happens to use a space-based indentation style and someone else uses TABS, the formatting can get all Hymaned up. As a counter-point, however, if the 'standard' is to always use tabs, then it really wouldn't matter since the formatting will all appear the same - regardless of the number of defined spaces. Butall it takes is one person to use a space and the formatting can look horrid and get really confusing. This can't happen when using spaces. Also, what happens if you don't want to use the same spacing between functions/methods, etc? What if you like using 4 spaces in some cases and only 2 in other cases?
自从我第一次 CS 课程以来,标签一直是禁忌。原因是,选项卡基本上就像变量。不同的 IDE 可以将 TAB 定义为不同数量的空格。从 Visual Studio/NetBeans/DevC++ 的角度来看,都可以根据所需空间的数量更改 TAB 的“定义”。所以如果你定义了 4 个空格,你就无法知道我的 IDE 是 3 个空格还是 5 个空格。因此,如果有人碰巧使用基于空格的缩进样式而其他人使用 TABS,则格式可能会被全部提升。然而,作为对立点,如果“标准”总是使用制表符,那么这真的无关紧要,因为格式将全部显示为相同 - 无论定义的空格数量如何。但所需要的只是一个人使用一个空格,格式可能看起来很可怕并且变得非常混乱。使用空格时不会发生这种情况。另外,如果您不想在函数/方法等之间使用相同的间距,会发生什么?如果您喜欢在某些情况下使用 4 个空格而在其他情况下只使用 2 个,该怎么办?
回答by gilly3
I have seen build scripts that parse source code and generate documentation or even other code. These kind of scripts usually depend on the code being in an expected format, and frequently that means either using spaces (or sometimes tabs). Perhaps these scripts couldbe modified to be more robust by checking for tabs orspaces, but frequently you are stuck with what you've got. In that kind of an environment, consistent formatting becomes more important.
我见过解析源代码并生成文档甚至其他代码的构建脚本。这些类型的脚本通常取决于采用预期格式的代码,这通常意味着使用空格(或有时使用制表符)。也许可以通过检查制表符或空格来修改这些脚本以使其更加健壮,但您经常会被现有的内容所困扰。在那种环境中,一致的格式变得更加重要。