javascript 我们在进行源码格式化时使用tab和space有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7728829/
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
What is the difference between using tab and space when we do source code formatting?
提问by Jitendra Vyas
When we want to give some formatting to code to make it more readable. We can use both Taband Space(by pressing space bar on keyboard). Many time I give spacemanually.
当我们想对代码进行一些格式化以使其更具可读性时。我们可以同时使用Tab和Space(通过按键盘上的空格键)。很多时候我手动给空间。
I've some confusions
我有些困惑
- Is there any difference between them?
- Is using tabbetter than spaces?
- Does using spaceincrease the size of document and tabnot?
- Is there any differences between single tab/spaceand multiple. in terms of page size?
- Is there any difference on using tabor spacein HTML, CSS and Javascript?
- 它们之间有什么区别吗?
- 使用制表符比使用空格更好吗?
- 使用空间会增加文档和选项卡的大小吗?
- 单个选项卡/空格和多个选项卡/空格之间有什么区别吗?在页面大小方面?
- 在 HTML、CSS 和 Javascript 中使用制表符或空格有什么区别吗?
Dreamweaver has this option in preferences
Dreamweaver 在首选项中有这个选项
采纳答案by campino2k
Tabs need less characters and are formatted by users (some display as 2, some as 4 spaces), what can make some code look bad on some other environment.
制表符需要较少的字符并由用户格式化(有些显示为 2 个,有些显示为 4 个空格),这会使某些代码在其他环境中看起来很糟糕。
Spaces consume more KBs but look equal everywhere.
空间消耗更多的 KB,但在任何地方看起来都一样。
Good editors have retab-functions to convert those.
好的编辑器有 retab 函数来转换它们。
In JS and CSS it does not matter, HTML shouldnot matter, but can in some cases.
在 JS 和 CSS 中没有关系,HTML应该没有关系,但在某些情况下可以。
回答by JB Nizet
Using tabs is annoying because some editors interpret a tab as 4 spaces, some others as 8 spaces, and some others as 2 spaces, which makes the indentation completely wrong if tabs and spaces are used in the same file.
使用制表符很烦人,因为一些编辑器将制表符解释为 4 个空格,有些编辑器解释为 8 个空格,而另一些解释为 2 个空格,如果在同一文件中使用制表符和空格,则缩进会完全错误。
I always use spaces only to avoid this problem.
我总是使用空格来避免这个问题。
It could slightly increase the download size of your pages, but you could also minify the JavaScript and the CSS files, and/or use gzip on-the-fly compression to mitigate this small problem.
它可能会稍微增加页面的下载大小,但您也可以缩小 JavaScript 和 CSS 文件,和/或使用 gzip 即时压缩来缓解这个小问题。
回答by Anil Shanbhag
Using Tabs :
使用标签:
- Tab takes less space
- Tab is user system defined :: So in my case if i prefer 2space::tab i can view it that way
- Moving one indentation level back is lot easier if you use tabs .
- 标签占用更少的空间
- Tab 是用户系统定义的 :: 所以在我的情况下,如果我更喜欢 2space::tab 我可以这样查看
- 如果您使用制表符,将缩进一级移回会容易得多。
Using Spaces :
使用空格:
- Tab space ratio usually defaults to 1:8 . So on all 'newbie' systems your code will be difficult to read . Also if you view your code on github / pastebin there again it will be some what awkward .
- 制表符空间比率通常默认为 1:8 。因此,在所有“新手”系统上,您的代码将难以阅读。此外,如果您再次在 github/pastebin 上查看您的代码,那将会有些尴尬。
My take : Go with tabs for development , find replace '\t' with ' ' [4 spaces] and then for release minify [ this strips tabs and spaces ] .
我的看法:使用制表符进行开发,找到将 '\t' 替换为 ' ' [4 个空格],然后释放缩小 [这会去除制表符和空格]。
回答by Reece
Personally, I see it as an issue of preference involving ease of formatting. I'd rather use tab, because it's only one click and it simulates 4-5 spaces. A lot simpler in my mind. Otherwise, I don't see much purpose.
就个人而言,我认为这是一个涉及易于格式化的偏好问题。我宁愿使用制表符,因为它只需单击一下,就可以模拟 4-5 个空格。在我看来要简单得多。否则,我看不出有多大目的。
回答by Toomai
In pretty much all cases, you can use tabs or spaces and it won't make a difference. Using tabs will make the source files slightly smaller, while using spaces will ensure that spacing is consistent for everyone (since tabs can have a variable width).
在几乎所有情况下,您都可以使用制表符或空格,这不会有什么区别。使用制表符会使源文件稍微变小,而使用空格将确保每个人的间距都是一致的(因为制表符可以具有可变宽度)。
I believe the only language where it actually matters is Python. For any other language, tabs vs. spaces is basically even - just be sure to pick one and be consistent with it.
我相信唯一真正重要的语言是 Python。对于任何其他语言,制表符与空格基本上是偶数 - 只需确保选择一种并与之保持一致即可。
回答by Jim Hall
Using a tab technically takes up less memory than using, say, 5 spaces. So when trying to optimize file size it could be helpful. However minifying text would have a better effect. Look up minifying code for more on this topic.
从技术上讲,使用制表符比使用 5 个空格占用更少的内存。因此,当尝试优化文件大小时,它可能会有所帮助。然而,缩小文本会有更好的效果。有关此主题的更多信息,请查找缩小代码。
Some people prefer spaces and some prefer tabs. Its a matter of preference and many folks have different reasons for it. Here is a great article on this point: http://www.jwz.org/doc/tabs-vs-spaces.html
有些人喜欢空格,有些人喜欢制表符。这是一个偏好问题,许多人对此有不同的原因。关于这一点,这里有一篇很棒的文章:http: //www.jwz.org/doc/tabs-vs-spaces.html
Chances are for your application it wont matter much.
对您的应用程序来说,它并不重要。
回答by dmerrill
If your code is being downloaded, such as HTML or Javascript or CSS, it makes a difference because the file is larger if spaces are used. How much larger depends on the number of lines and indent levels. It's the same as including comments in the code: they do increase file size.
如果您的代码正在被下载,例如 HTML 或 Javascript 或 CSS,它会有所不同,因为如果使用空格,文件会更大。大多少取决于行数和缩进级别。这与在代码中包含注释相同:它们确实会增加文件大小。
If your code will be compiled, such as Actionscript or Java or C, or tokenized such as Perl, it makes no difference whether you use spaces or tabs, and you can include as many paragraphs of comments/documentation as you like, because it's only for your own benefit. All those tabs and spaces and comments will be ignored when the final, lower-level code is built.
如果您的代码将被编译,例如 Actionscript 或 Java 或 C,或标记化(例如 Perl),则使用空格或制表符都没有区别,您可以根据需要包含任意多的注释/文档段落,因为它只是为了您自己的利益。在构建最终的低级代码时,所有这些制表符、空格和注释都将被忽略。