I will today settle one of the great issues of our time: whether or not to always put a newline at the end of a file. There are two reasons why it's a good idea, and zero reasons why it's a bad idea.
Let's consider these files:
The first reason: when using various command line tools, their output will either be ugly because they don't compensate for the lack of a newline, or be ugly because they do. Here are some examples:
The second and more semantically-consequential reason: when adding code to the last line of a file, if it didn't originally have a newline then diff (or git) will think that, in addition to the new code being added, the last line was also changed (because it was, because you added a newline to the end). So the output of the diff becomes less semantically accurate.
See how in the first example, diff sees the change as 2 existing lines being changed. In the second example, it sees the change as a single line insertion, with no existing lines modified (and doesn't do the ugly announcement of lack of newline).
Ahhh, isn't that nice? So, put newlines at the end of your code files!
Co-Founder and CTO of Medstro