It's not a big deal either way, but ending in a new line makes consistent the property that each line of text ends in a new line character (as opposed to EOF).
Ask HN: Should source files always end in a new line?
11–20 of 40 posts
Re: Ask HN: Should source files always end in a new line?
#12I was thinking a while ago about formatting restrictions that could make sense in a language (programming, markup, whatever) to simplify things (… and which would surely also annoy some users, but what’s life without fun?). Things like requiring tabs (partly because in a vacuum they’re simply better than spaces, and partly to protest against YAML which goes the other way), and banning carriage returns (how much trouble has supporting both ␍␊ and ␊ caused, I wonder? It’s not a little). Requiring EOL at EOF was also one I thought of.
Re: Ask HN: Should source files always end in a new line?
#13It makes it easier to parse a text file without special cases.
It's an insignificant reason, but that's why it's traditionally recommended.
Re: Ask HN: Should source files always end in a new line?
#14The only difference I can think of is when appending to a file. Starting with an empty file you’re on a new line. But if you don’t end every line with a newline character then your next line will end up on the same line in the file as the last one.
Only reason I do it is because if you're adding onto a file, git will say the (prior) last line is modified because you have added the newline to it. But it's not a really big deal, I don't enforce it with any linter/CI. It's just 0.01% annoying to see a 20 line commit say 21 lines changed.
Re: Ask HN: Should source files always end in a new line?
#15GitHub gives me an angry looking red circle if I don't so I add one
Re: Ask HN: Should source files always end in a new line?
#16Yes, purely because otherwise diffs get this added:
\ No newline at end of fileRe: Ask HN: Should source files always end in a new line?
#17Source files are text files.
echo “new line” >> textfile
cat
If you and your toolchain don’t see them as text files and never will, and you never use your editor to edit other text files (e.g. /etc/…), you’re probably ok without a newline at eof.Re: Ask HN: Should source files always end in a new line?
#18This doesn't matter even remotely and it is the height of bike shedding to even have a discussion about it.
I mean, I’ve found it very interesting to see different pros of ending a file in a new line I would have never even considered.
Re: Ask HN: Should source files always end in a new line?
#19Whatever your code formatter says. ESLint adds one for me so my source files have a newline.
Re: Ask HN: Should source files always end in a new line?
#20Generally I would say yes. POSIX dictates that a line must end in newline, and some tools rely on it. See https://stackoverflow.com/a/729795