在java源文件的末尾清空新行 [英] Empty new line at the end of the java source files
问题描述
在我当前的项目中,我们总是在java源文件的末尾插入空的新行。我们还使用CheckStyle强制执行此操作(具有错误级别)。
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).
我很长一段时间都在搜索这个主题,但不幸的是我找不到任何令人信服的理由。似乎其他开发人员对此非常漠不关心,因为他们只是在eclipse格式化程序中选中了一个复选框,并且它是自动完成的。但我仍然不知道为什么需要它,为什么它很重要。所以我的问题是:
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:
为什么Java源文件末尾的空行需要?
它是当前的需求还是过去的遗物,在目前的代码库中是不受欢迎的?
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?
推荐答案
我认为他们正在尝试确保每个文件都以尾随的换行符结尾。这不同于以空白行结尾,即空行换行。
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.
编辑:正如@Easy Angel在评论中简明扼要地澄清:尾随换行符=\ n和空行=\ n \ n
我认为:
-
你的主管是强制要求每个文件以换行符结尾,但是它被误解为强制每个文件以空行结束(即以换行符结尾的空行) ),或者
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
他们试图通过实际强制每个文件以空行结束(也就是结束的空行)来确保每个文件以换行符结尾使用换行符),从而确保文件以至少一个换行符结束(可能还有多余的额外换行符 - 过度杀伤?)。
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:
- DOES NOT END 一个新行,
- ENDS 带有一个尾随换行符,或者
- ENDS 带有空白换行符,即2尾随换行符
- DOES NOT END a newline at all,
- ENDS with a single trailing newline, or
- 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:
-
当使用
cat
时在命令行上显示文件,如果文件缺少尾随换行符,则下一个输出(如shell提示符或脚本可能在文件之间输出的可视分隔符)最终会出现在最后一个非换行符后面而不是开始换行。一般来说,尾随换行使文件更易于用户和脚本。
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.
我相信一些编辑(我记不清任何细节)会自动插入如果文本文件缺少一个,则为尾随换行符。这会使文件看起来像是被修改过的。如果您在不同的窗口中打开一堆文件然后关闭所有文件会让您感到困惑 - 编辑器会提示您保存,但您不确定是否对文件进行了真正的更改,或者只是自动修改了插入换行符。
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.
某些工具如 diff
和一些编译器会抱怨缺少尾随换行符。这是用户和工具可能需要处理的更多噪音。
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.
编辑:
关于编辑添加换行符,但无法查看是否有换行符与空白换行符在文件的最后,我刚刚测试了Vim,Eclipse和Emacs(在我的Windows系统上使用Cygwin):我打开了一个新文件,键入'h''e''l'''''o'并保存点击[ENTER]。我用 od -c -t x1
检查了每个文件。
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
.
- Vim 添加一个尾随换行符。
- Emacs 添加一个尾随换行符。
- Eclipse 没有添加尾随换行符。
- Vim did add a trailing newline.
- Emacs did add a trailing newline.
- Eclipse did NOT add a trailing newline.
但是
- Vim 没有允许我向下移动到hello下的空白行。
- Emacs 允许我向下光标到空行你好。
- Eclipse 没有允许我向下光标到你好下的空行。
- Vim did NOT allow me to cursor down to a blank line under "hello".
- Emacs did allow me to cursor down to a blank line under "hello".
- Eclipse did NOT allow me to cursor down to a blank line under "hello".
您可以随意解释。
我个人的做法是尝试确保文本文件以尾随换行符结尾。我觉得人们和工具的 最少惊喜 就是这种情况。在这方面,我不会将源文件与文本文件区别对待。
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出现 this :
,在这个编辑,显示点击谈论有关来自C编译器,svn(因为差异),差异等的丢失尾随换行的警告。我觉得有一般期望文本文件(包括源文件)以a结尾当他们往往在那里时,尾随换行和最不令人惊讶(并且噪音较小)。
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.
最后这个很有意思:
清理没有尾随换行符的文件
文本文件的所有行都应以换行符(即\ n)终止。这是由POSIX声明的,即文本文件是
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 ,所有这一切,这只是我个人的做法。我很高兴与任何要求的人分享我的观点,但我不会强迫任何人这样做。我不认为这是值得强制的事情,就像我说的那样这里:
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?
这篇关于在java源文件的末尾清空新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!