Why is it recommended to have empty line in the end of a source file?

An argument can also be made for cleaner diffs if you append to the file following the same reasoning as Why are trailing commas allowed in a list?

The following is copied (and trimmed a bit) from the linked resource:

Changing:

s = [
  'manny',
  'jack',
]

to:

s = [
  'manny',
  'jack',
  'roger',
]

involves only a one-line change in the diff:

  s = [
    'manny',
    'jack',
+   'roger',
  ]

This beats the more confusing multi-line diff when the trailing comma was omitted:

  s = [
    'manny',
-   'jack'
+   'jack',
+   'roger'
  ]

Apart from the fact that it is a nicer cursor position when you move to the end of a file in a text editor.

Having a newline at the end of the file provides a simple check that the file has not been truncated.


If you try to concatenate two text files together, you will be much happier if the first one ends with a newline character.


Many older tools misbehave if the last line of data in a text file is not terminated with a newline or carriage return / new line combination. They ignore that line as it is terminated with ^Z (eof) instead.