在代码文件末尾留下额外的空行的原因是什么?

发布于 2024-08-24 11:58:35 字数 254 浏览 4 评论 0原文

默认情况下,Eclipse 和 MyEclipse 创建新的 Java 文件,并在最后一个右大括号后有一个额外的空行。我认为 CodeWarrior 几年前也做了同样的事情,并且有些人有意或懒惰地在代码中留下了这样的空行。因此,这似乎至少是一种相当普遍的行为。

作为一名前人类语言编辑(主要是编辑报纸),我发现这些行看起来很草率或意外,而且我想不出将它们留在源文件中的理由。我知道它们不会影响 C 风格语言(包括 Java)的编译。拥有这些线路有什么好处吗?如果有,它们是什么?

Eclipse and MyEclipse create new Java files with an extra blank line after the last closing brace by default. I think CodeWarrior did the same thing a few years back, and that some people leave such blank lines in their code either by intention or laziness. So, this seems to be at least a moderately widespread behavior.

As a former human language editor -- copy editing newspapers, mostly -- I find that those lines look like sloppiness or accidents, and I can't think of a reason to leave them in source files. I know they don't affect compilation in C-style languages, including Java. Are there benefits to having those lines, and if so, what are they?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

仄言 2024-08-31 11:58:35

这是历史性的,与文本处理命令行实用程序中的常见错误有关。 Unix 系统上有许多程序用于处理文本,并且大多数程序都是逐行工作的。当最后一行不以换行符结尾时,可能有也可能没有任何常见的命令行工具会出现错误,但这种现象在历史上很常见,以至于其他工具开始对这种情况发出警告。

最后我检查了一下,vim 实际上无法保存不以换行符结尾的文件,甚至无论您是否将其放在那里,都会添加一个文件。

It is historical, relating to common bugs in command line utilities for text processing. There are many programs on a Unix system for processing text, and most of them work on a line by line basis. There may or may not be any common command line tools that misbehave when the last line does not end in a newline, but the phenomenon is historically common enough that other tools started warning about the situation.

Last I checked vim is actually incapable of saving a file that does not end in a newline, going so far as to add one whether you put it there or not.

鹊巢 2024-08-31 11:58:35

AFAIK,使用换行符结束每一行是一种很好的做法,因为在解析代码时,可以使用一个简单的函数来读取它,该函数将根据这些换行符将文件拆分为行。如果没有空行,因此最后一行以换行符结尾,您将丢失该行上的所有字符。

AFAIK, it is good practice to have every line ended using newline character, because when the code is parsed, it can be read using one simple function, which will split file into lines according to those newline characters. Without blank line and therefore last line ended with newline, you would lose all characters on that line.

a√萤火虫的光℡ 2024-08-31 11:58:35

这不一定是历史原因,但在我看来是一个很好的原因:

它可以防止您忘记在文件末尾有一个尾随换行符。编译器通常会对此发出警告,源代码控制系统通常也会发出警告,并且当您添加换行符以消除编译器警告时,会将行显示为已更改。

简而言之,它减少了潜在的噪声和不必要的 SCM 差异。

This isn't necessarily the historical reason why, but is a good reason, IMO:

It prevents you from forgetting to have a trailing newline at the end of your file. Compilers typically warn about this, and source control systems often will too, and will show the line as changed when you add the newline to get rid of your compiler warning.

In short it reduces the potential for noise and unnecessary SCM diffs.

朕就是辣么酷 2024-08-31 11:58:35

我没有足够的观点来评论,但我想对 @Justin Smith 的声明说一些话,即“[vim 将会]添加一个[换行符]无论你是否把它放在那里 ,”。

我刚刚检查了 vim,虽然它确实有一个,但 vim 不显示空白换行符。如果你在nano中打开用vim保存的文件,nano会显示末尾有一个空白换行符。所以我决定看看如果我用 vim 手动创建一个新行会发生什么,nano 显示有 2 个新行

对我来说,这是有道理的,因为就像在文件结束之前关闭行,就像关闭 HTML 中的所有标签一样。

I don't have enough points to comment but I wanted to say something about @Justin Smith statement that "[vim will go] so far as to add one [newline] whether you put it there or not,".

I just checked vim, and while it does have one, vim does not show the blank newline. If you open the file saved with vim in nano, nano will show that there is a blank newline at the end. So I decided to see what happens if I manually make a new line with vim, and nano showed that there are 2 new lines.

To me this makes sense because its like closing the line before the file ends, like closing all your tags in HTML.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文