Empty new line at the end of the java source files

2019-03-11 14:20发布

In my current project we always insert empty new line at the end of the java source files. We also enforce this with CheckStyle (with error level).

I was searching for this topic for a long time, but unfortunately I can't find any convincing reason for this. Seems that other developers are pretty indifferent about this because they just checked one checkbox in eclipse formatter and it's done automatically. But I still don't know why is it needed and why is it can be important). So my question is:

Why are empty lines at the end of Java source files need? Is it a current need or a relic of the past and undesirable in current code bases?

9条回答
老娘就宠你
2楼-- · 2019-03-11 14:27

I think they are trying to ensure every file ends with a trailing newline character. This is different from ending with a blank line, a.k.a. empty newline.

Edit: As @Easy Angel succinctly clarified in the comments: trailing newline = "\n" and blank line = "\n\n"

I think either:

  1. your lead is either mandating that every file ends with a newline character, but its being misinterpreted as mandating that every file end with a blank line (i.e. an empty line that ends in a newline), or else

  2. they are trying to ensure every file ends with a newline character by actually mandating every file end with a blank line (a.k.a. empty line that ends with a newline), thereby ensuring files ends with at least one newline (and possibly redundant additional newline - overkill?).

Unless the editor actually shows newline symbols, its not always clear in some editors that a file:

  1. DOES NOT END a newline at all,
  2. ENDS with a single trailing newline, or
  3. ENDS with a blank newline, i.e. 2 trailing newlines

I think most modern source code editors insert a trailing newline. However, when using older more general editors, I would always try to ensure my source code files (and text files in general) always ended with a trailing newline (which occasionally came out as a blank line/empty newline depending on the editor I was using) because:

  1. when using cat to display the file on the command line, if the file lacked a trailing newline, the next output (like the shell prompt or a visual delimiter a script may output between files) would end up appearing right after the last non-newline character rather than starting on a newline. In general, the trailing newline made files more user- and script- friendly.

  2. I believe some editors (I can't remember any specifics) would automatically insert a trailing newline if the text file lacked one. This would make it appear like the file was modified. It would get confusing if you have a bunch of files open in different windows and then go to close all of them - the editor prompts you to save but you are unsure whether you made "real changes" to the file or its just the auto-inserted newline.

  3. Some tools like diff and some compilers will complain about a missing trailing newline. This is more noise that users and tools may have to deal with.


Edit:

About editors adding newlines and not being able to see whether there's a newline vs blank newline at the end of the file, I just tested Vim, Eclipse, and Emacs (on my Windows system with Cygwin): I opened a new file, typed 'h' 'e' 'l' 'l' 'o' and saved without hitting [ENTER]. I examined each file with od -c -t x1.

  1. Vim did add a trailing newline.
  2. Emacs did add a trailing newline.
  3. Eclipse did NOT add a trailing newline.

But

  1. Vim did NOT allow me to cursor down to a blank line under "hello".
  2. Emacs did allow me to cursor down to a blank line under "hello".
  3. Eclipse did NOT allow me to cursor down to a blank line under "hello".

Interpret as you like.


My personal practice is to try to ensure text files end with a trailing newline. I just feel there's the least surprise to people and tools with this is the case. I wouldn't treat source files any different from text files in this respect.

Google turns up this:

which, as of this edit, show hits that talk about warnings about a missing trailing newline coming from C compilers, svn (because of diff), diff, etc. I feel there's a general expectation that text files (source files included) end with a trailing newline and least surprising (and less noisy) when they tend to be there.

Finally this is interesting:

Sanitizing files with no trailing newline
Text files should have all their lines terminated by newline characters (ie, \n). This is stated by POSIX, that says that a text file is

A file that contains characters organized into zero or more lines.
A line, in turn, is defined as
* A sequence of zero or more non- characters plus a terminating character.


HOWEVER, all that said, this is just my personal practice. I'm happy to share my opinion to anyone that asks, but I don't foist this on anyone. I don't feel this is something worth mandating, like I say here:

While I'm one whose all for consistency, I'm also against micromanaging every bit of style. Having a huge list of coding conventions, particularly when some of them seem arbitrary, is part of what discourages people from following them. I think coding guidelines should be streamlined to the most valuable practices that improve the -ilities. How much is readability, maintainability, performance, etc improved by mandating this practice?

查看更多
Luminary・发光体
3楼-- · 2019-03-11 14:28

Sometimes your compiler doesn't parse it correctly:

Error: Reached end of file while parsing

查看更多
啃猪蹄的小仙女
4楼-- · 2019-03-11 14:30

I have never heard of such a requirement.

In fact, I just confirmed that a Java program will run without any compiler/runtime errors or warnings when there isn't a blank line at the end of the file.

This, as some commenters have said, must be a coding style issue. Unfortunately, I can't suggest why it may be important for there to be a blank line at the end of a file in Java. In fact, it seems entirely pointless to me

查看更多
forever°为你锁心
5楼-- · 2019-03-11 14:31

Aside from the already mentioned valid reasons for having a trailing newline character (possible issues with older tools and diff), here is another way to look at it:

Why special-case the last line by not appending a newline character when every other line in the file has one?

查看更多
ゆ 、 Hurt°
6楼-- · 2019-03-11 14:36

It is just a coding style. Doesn't hurt or help anything. I would not let it bother you it sounds like it is your teams preference to include and empty line. There is not really a good argument against it other than why does anyone care enough to actually add it to checkstyle?

查看更多
Lonely孤独者°
7楼-- · 2019-03-11 14:39

We had to do this for some C++ code as the compiler generated a warning about it, and we had a 'no error or warnings' policy. Maybe the issue lies elsewhere... have you a diffing tool have goes haywire or a merge tool that can't handle it?

It's not a big deal really.

查看更多
登录 后发表回答