开发者

New line at the end of source code

开发者 https://www.devze.com 2023-03-16 14:40 出处:网络
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, r

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?


Two things make me prefer having a newline at the end of files:

  1. Code reviews are slightly easier when looking at diffs that occur at the end of the file (i.e., if a line is added at the end of the file, it appears that the previous line changed, when it only gained a newline)
  2. Going to the end of the file (Ctrl+End in Windows) always puts me at the same column and not in some unexpected position out to the right


Pretty much the only difference it makes is that if you have a file with no newline - like this:

blah\n
bleh (no newline)

When you modify it to be:

blah\n
bleh\n
foo (no newline)

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号