去除尾随空格的好处?
我养成了从源文件中删除尾随空格的习惯。事实上,我的编辑器会自动执行此操作。我通过使用 git 养成了这个习惯;它养成了我坚持的习惯。
我的问题与我无法证明这种行为合理这一事实有关。我可以理解,在某些领域,例如网页设计师,这可能会影响他们的最终结果。但对于程序员来说,我们能从中得到什么?我们不能把它留在里面吗?
I got into the habit of removing trailing white spaces from my source file. In fact, I have my editor to do this automatically. I got in this habit by using git; it created a habit that I adhere to.
My question relates to the fact that I cannot justify this behaviour. I can understand that in some fields, such as web designers, it may impact their final result. For programmer though, what do we gain out of it? Can't we just leave it in?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我也喜欢修剪空白。这样做没有技术要求,但它有一些优点:
然而,最重要的是,在整个源代码中使用一致的格式(间距、缩进、大括号样式...)有一个巨大的好处(恕我直言) 这使得代码更易于阅读,并避免重新格式化带来的巨大差异(如果它始终正确格式化,则无需重新格式化)。
因此,我建议让格式化程序在您保存时自动运行(或至少在每次提交时)。这样,可以消除尾随空格的副作用:-)。
I also like to trim whitespace. There is no technical requirement to do it, but it has some advantages:
Most of all, however, there is a huge benefit (IMHO) to use consistent formatting throughout the source code (spacing, indentation, brace style...). This makes the code easier to read, and avoids large diffs from reformattings (if it's always corretly formatted, no need to reformat).
Therefore I would recommend letting a formatter run automatically whenever you save (or at least for every commit). That way, trailing whitespace can be eliminated as a side effect :-).
你没有说明你正在使用什么语言,但在类似 C 语言中,尾随空格会影响宏。
考虑一下:
\ 仅当它是该行的最后一个字符时才有效 - 因此它后面的空格会导致它中断。
You don't say what language you're using, but in C-alikes, trailing whitespace affects macros.
Consider:
The \ only works if it's the last character on the line --- so whitespace after it will cause it to break.
杰夫·阿特伍德帖子的强制链接。
空白:沉默的杀手
Mandatory link to a Jeff Atwood post.
Whitespace: The Silent Killer