views:

147

answers:

5

Some code style tools recommend this and I remember seeing some unix command line tools warning about missing empty line.

What is the reasoning for having an extra empty line?

+6  A: 

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.

Ralph Rickenbach
+3  A: 

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.

rsp
The file might be truncated and you would never even kn
Simon Nickerson
+1 for the "kn"
Andrea Ambu
+1  A: 

I don't believe leaving empty lines at the bottom is good style in all cases - I've seen many a PHP script fail because of spurious empty lines at the bottom.

Rob Cowell
You don't believe _what_ is true in all cases? I believe this would work better as a comment on the answer you are referring to.
Wayne Koorts
I've edited in an attempt to clarify ;-)
Rob Cowell
+1  A: 

The empty line in the end of file appears so that standard reading from the input stream will know when to terminate the read, usually returns EOF to indicate that you have reached the end. The majority of languages can handle the EOF marker. It is there for that reason from the old days, under DOS, the EOF marker was F6 key or Ctrl-Z, for *nix systems, it was Ctrl-D.

Most, if not all, will actually read right up to the EOF marker so that the runtime library's function of reading from input will know when to stop reading any further. When you open the stream for Append mode, it will wipe the EOF marker and write past it, until a close is explicitly called in which it will insert the EOF marker at that point.

Older tools were expecting a empty line followed by EOF marker. Nowadays, tools can handle the empty line and ignore it.

Hope this helps, Best regards, Tom.

tommieb75
^D was not "the EOF marker". Pressing ^D caused the shell to close the write side of the pipe that the foreground process group was reading from, so that a read from that pipe returned EOF. There is no "EOF marker".
William Pursell
+1  A: 

Some languages define their input file in terms of input lines, where each input line is a series of characters terminated by a carriage return. If their grammar is so defined, then the last valid line of the file must be terminated by a carriage return too.

Damien_The_Unbeliever