Search code examples
version-controlformattingconventions

New Lines at the End of Code Files


I've noticed what seems to be a very strong convention among IDEs and code-oriented text editors. Almost any time I create a new code file, regardless of the language, IDE, or platform on which I create the file, they almost always have a trailing new line at the end.

For example:

  • C++ and C# class wizards in Visual Studio always have this new line after the last curly brace.
  • The nano editor in the Linux terminal does not allow you to remove the last new line.
  • Git version control makes a specific point to point out files which do not end with a new line.

So, why is this a common practice? I've never had a problem with scripts where I intentionally remove this new line. Does it have significance for legacy purposes?

Thank you for your time.


Solution

  • While you would be hard pressed to find a modern editor or compiler which has issues with unterminated lines, this wasn't always the case. If you need to manipulate your source code programmatically, there are probably still e.g. sed implementations out there which are buggy in this respect.

    At a minimum, you should make a conscious decision about which way you want things; if you are authoring, say, a company coding style guideline, it probably makes sense to require or recommend source files to have a final newline, because it helps avoid religious wars and supports the lowest common denominator.