空格与制表符——最佳实践?
我曾经是一个制表符狂——我总是使用制表符来缩进我的源代码,如果我看到任何空格,我会立即执行正则表达式搜索/替换,以用制表符替换所有前导空格。
...直到我意识到我没有注意到某些文件中存在很多空格,因此它们在不同的编辑器中打开不一致(例如 Notepad++、Emacs 和 Visual Studio) )。
一般来说,有哪些充分理由可以解释为什么一个人比另一个人更好?哪一种通常被认为是更好的做法?
I used to be a tab freak -- I would always use tabs to indent my source code, and if I saw any spaces, I would instantly do a regex search/replace to replace all leading spaces with tabs.
...until I realized that I hadn't noticed the presence of a lot of the spaces in some files, such that they opened up inconsistently in different editors (e.g. Notepad++ vs. Emacs vs. Visual Studio).
What are good reasons for why one is better than the other in general? Is either one generally known as better practice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有了好的文本编辑器,这并不重要。这只是一个技术背景细节。 UI 行为将完全相同。
这里唯一重要的是项目中的每个人都决定使用什么,并且每个人都坚持这一点。因为,正如您所发现的,如果在文件内同时使用制表符和空格进行缩进,将会出现问题。
更新:当我的意思是项目中的每个人都决定使用什么时,我当然是指每个人都应该同意相同的决定。 :D(这可能是棘手的部分。)
With good text editors, it does not really matter. It is just a technical background detail. The UI behavior will be just the same.
The only important thing here is that everyone in a project makes the decision what to use, and that everyone sticks to that. Because, as you discovered, there will be problems if both tabs and spaces are used for indentation inside a file.
UPDATE: When I mean that everyone in a project makes the decision what to use, I of course mean that everyone should agree on the same decision. :D (This can be the tricky part.)