源代码末尾换行
每当我在 Visual Studio 中打开代码编辑器时,生成的代码末尾总是有一个空的新行。我通常会删除它们,因为它们似乎与我无关。但是,最近我在 Github 上面写着:
\ No newline at end of file
这是最后一行。现在我认为源代码末尾的那些空新行确实有一些相关性。但它们是什么意思呢?它们是否提供任何性能提升?
Anytime I open up the code editor in Visual Studio, there is always an empty new line at the end of generated codes. I usually delete them since they seem irrelevant to me. However, recently I read code at Github which said:
\ No newline at end of file
This was the last line. Now I'm thinking those empty new lines at the end of source codes do have some relevance. But what do they mean? Do they provide any performance boost?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有两件事让我更喜欢在文件末尾添加换行符:
Two things make me prefer having a newline at the end of files:
几乎唯一的区别是,如果你有一个没有换行符的文件 - 就像这样:
当你将其修改为:
然后根据差异,你修改了 2 行 - 一行包含内容,另一行包含换行符..这可能不是你想要的。话又说回来,实际上,你选择哪种方式并不重要。如果您包含换行符,您的差异会更干净一些。
正如其他答案中提到的,它对于某些预处理器也有影响 - 但这取决于您使用的语言。
当然,它根本没有性能差异。
Pretty much the only difference it makes is that if you have a file with no newline - like this:
When you modify it to be:
Then according to the diff, you modified 2 lines - one with content, the other one with newline... which is not what you wanted probably. Then again, in reality it doesn't matter that much which way you choose. If you include newlines, your diffs will be a little bit cleaner.
It also makes difference for some preprocessors as mentioned in other answer - but that depends on what language you use.
Of course it makes no performance difference at all.
不,这没有任何区别。
有些编码约定说最好有最后一个换行符,有些则说最好不使用换行符。
No, it makes no difference whatsoever.
Some coding conventions say it's good to have a final newline, some say it's good not to.
在此处阅读有关 C++ 中换行符的更多信息:"文件结尾处没有换行符“编译器警告
我认为 Visual Studio 和 Git 这样做主要是为了与约定保持一致。
Read more about new line in C++ here: "No newline at end of file" compiler warning
I suppose both Visual Studio and Git do it mostly for being coherent with the convention.