Newline at end of file

This is just me asking a question in public and doing as little research on the subject as possible, but why wouldn’t you want a newline character at the end of a file?

There are several reasons why I can see they are more beneficial to have than not, I’ll list those out now:

  • Diffs display better on files that end in newlines
  • When I press “Ctrl + End” or “Cmd + Down” I don’t expect the file to scroll right (if the line is long enough)

So maybe bullet points weren’t necessary there because I only had two points, but it’s always been a pet-peeve of mine and I’m curious if there’s ever a good reason not to have one at the end.

Sure, the newline can go away in minified files, but I shouldn’t really care about diffs and actually editing them in an editor, so that doesn’t really count. I would consider that a deployment version, not a development version. I’m just referring to files that are shared in source control.

About Chris Missal

Oh hey, I'm a Senior Consultant for Headspring in Austin, TX. I've been working in software professionally since 2006 and I really, really love it. I'm mostly in the Microsoft world, but enjoy building computer things of all sorts (to be vague). When I'm not slinging code, I'm probably out and about slinging discs, bowling balls, or good beer with great friends.
This entry was posted in Rant and tagged . Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Greg Sohl

    What kind of text file? Is there an established convention? If this is a source code file then I’ve not seen a benefit one way or another.

    • Most recently I’ve noticed it in Powershell, but usually JavaScript files come to mind.

  • There is no reason for having a new line at the end of file. There is also no reason for not having one. This is something is can leave with. If only this was the only debate… think about tabs vs spaces and you will instantly not care about that new line.

    •  But there are two reasons. The two I mentioned above.

  • Hwiechers

    You wouldn’t want it because  source files should be free from unnecessary whitespace. It’s the same reason that people freak out about trailing whitespace.

    Of your two points, I don’t think the first makes much of a difference.
    The second depends on the editor you use, and if you’re using a language where your files always end with ‘}’, the second isn’t going to make much of a difference either.

    Personally, I don’t mind either way as long as it’s consistent.

    • I would agree that source files should be free from unnecessary *trailing* whitespace.

      Here’s what I mean about the diffs: I don’t want files to show as modified when logically they’re not.

      You’re right about files ending with ‘}’ and I would prefer consistency over “my way” any day.

  • Another benefit: concatenating files (say a header) is simpler with a newline at the end.

    • Good call. I’ve actually run into that issue before and had to concat “file” + newline + “file” and so on. It was annoying to say the least.

  • Edwin Castro

    Old *NIX gurus tend to *always* have a new line at the end of a text file. In *NIX, where nearly everything you do is text processing, you have a lot of tools that can process files on a line by line basis. Having you last line in the file contain a new line keeps the last line in the file consistent with all other lines in the file. Immediately after that last new line character you see EOF.

    Other than for consistency and so that your tools work better together, I don’t see any good reason for one convention over the other.

    I’m personally on the fence on this one, but I think I lean more towards ending text files with a new line.