Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New line at the end of source code

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?

like image 632
afaolek Avatar asked Dec 03 '22 08:12

afaolek


2 Answers

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
like image 181
GaTechThomas Avatar answered Dec 29 '22 20:12

GaTechThomas


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.

like image 26
viraptor Avatar answered Dec 29 '22 20:12

viraptor