HTML 或 JavaScript 是否有推荐的最大行长度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2886603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:19:44  来源:igfitidea点击:

Is there a recommended maximum line length for HTML or JavaScript?

javascripthtmlcoding-style

提问by Matthew

Most style guidelines for most programming languages recommend a maximum line length, typically 80 characters. This seems impractically short for HTML and JavaScript (when it is embedded in HTML). Is there a consensus on a practical line length limit for HTML/JavaScript? Or is it generally left up to the developer's common sense?

大多数编程语言的大多数样式指南都推荐最大行长度,通常为 80 个字符。这对于 HTML 和 JavaScript 来说似乎不切实际(当它被嵌入到 HTML 中时)。是否对 HTML/JavaScript 的实际行长度限制达成共识?还是通常取决于开发人员的常识?

采纳答案by Daniel Vassallo

Since you mention JavaScript, this is what Douglas Crockfordhas to say on the topic:

既然你提到了 JavaScript,这就是道格拉斯·克罗克福德 (Douglas Crockford)对这个话题的看法:

Avoid lines longer than 80 characters. When a statement will not fit on a single line, it may be necessary to break it. Place the break after an operator, ideally after a comma. A break after an operator decreases the likelihood that a copy-paste error will be masked by semicolon insertion. The next line should be indented 8 spaces.

From: Code Conventions for the JavaScript Programming Language

避免超过 80 个字符的行。当一个语句不能放在一行中时,可能需要将它打断。将中断放在运算符之后,最好放在逗号之后。运算符后的中断会降低复制粘贴错误被分号插入掩盖的可能性。下一行应缩进 8 个空格。

来自:JavaScript 编程语言的代码约定

回答by leymannx

This limit seems to be a legacy from ancientIBM punch cards. Why is 80 characters the 'standard' limit for code width?

这个限制似乎是古代IBM 穿孔卡的遗留物。为什么 80 个字符是代码宽度的“标准”限制?

I really find it hard to work with only 80 characters for HTML. It gets unreadable really fast. So I decided for myself to take the line length on GitHubas measurement.

我真的发现仅使用 80 个字符的 HTML 很难。它很快变得无法读取。所以我自己决定以GitHub上的行长作为衡量标准。

There seem to be different character limits depending on OS and/or browser. But 120 should be a quite safe value.

根据操作系统和/或浏览器,似乎有不同的字符限制。但是 120 应该是一个相当安全的值。

On Ubuntu:

  • Firefox: 126
  • Opera 12.16: 126
  • Chromium: 113

On OSX 10.9:

  • Google Chrome: 125
  • Firefox: 122
  • Safari: 121

在 Ubuntu 上:

  • 火狐:126
  • 歌剧 12.16: 126
  • 铬:113

在 OSX 10.9 上:

  • 谷歌浏览器:125
  • 火狐:122
  • 野生动物园:121

Source: What is Github's character limit, or line length for viewing files on github?

来源:Github 的字符限制是多少,或者在 github 上查看文件的行长是多少?



On the other hand sometimes choosing your own standard doesn't work. Coding standards or linters for your language or framework might force you to stick to 80 characters. Which at least can be really helpful when it comes to reading code on mobile screens.

另一方面,有时选择自己的标准是行不通的。您的语言或框架的编码标准或短绒可能会迫使您坚持使用 80 个字符。这至少在移动屏幕上阅读代码时非常有用。

But for HTML I'd devo rise this number to 120 characters. For the sake of readability. I mean think of all the CSS-class-heavy frameworks like Bootstrap, what would your template look like in the end with only 80 characters line length? (Apart from that you'd better choose Bourbon Neat which won't pollute your HTML with tons of classes.)

但是对于 HTML,我会将这个数字增加到 120 个字符。为了可读性。我的意思是想想所有像 Bootstrap 这样的 CSS-class-heavy 框架,你的模板最终会是什么样子,只有 80 个字符的行长?(除此之外,您最好选择 Bourbon Neat,它不会用大量的类污染您的 HTML。)

回答by Webveloper

Google says 500-ish for JS: https://developers.google.com/closure/compiler/faq#linefeeds

Google 说 JS 为 500-ish:https: //developers.google.com/closure/compiler/faq#linefeeds

The Closure Compiler intentionally adds line breaks every 500 characters or so. Firewalls and proxies sometimes corrupt or ignore large JavaScript files with very long lines. Adding line breaks every 500 characters prevents this problem.

Closure Compiler 有意每 500 个字符左右添加换行符。防火墙和代理有时会损坏或忽略行很长的大型 JavaScript 文件。每 500 个字符添加换行符可防止出现此问题。

uglify --max-line-len 500or grunt-contrib-uglify options: { maxLineLen: 500 }

丑化--max-line-len 500或 grunt-contrib-uglifyoptions: { maxLineLen: 500 }

I'm not sure if the limit applies to HTML or CSS, but I haven't seen that option for any CSS compressor I've used.

我不确定该限制是否适用于 HTML 或 CSS,但我没有在我使用过的任何 CSS 压缩器中看到该选项。

SMTP rules would apply to HTML email: https://www.ietf.org/rfc/rfc0821.txt

SMTP 规则适用于 HTML 电子邮件:https: //www.ietf.org/rfc/rfc0821.txt

The maximum total length of a command line including the command word and the CRLF is 512 characters.

包括命令字和 CRLF 的命令行的最大总长度为 512 个字符。

回答by Carson Myers

The 80 character line limit spawns from the days of screens without a lot of real estate. Now it's just like that for readability, and so it's possible to have two (or more) different code files opened side-by-side, without having to scroll to see each of them.

80 个字符的行限制产生于没有大量空间的屏幕时代。现在它就像可读性一样,因此可以并排打开两个(或更多)不同的代码文件,而不必滚动查看每个文件。

Those reasons still apply to HTML and JavaScript, although, it's obviously not necessary to comply with them. So it's up to you.

这些原因仍然适用于 HTML 和 JavaScript,尽管显然没有必要遵守它们。所以这取决于你。

I agree that with HTML it can be difficult to keep within that limit, although with JavaScript it shouldn't be a problem.

我同意使用 HTML 可能很难保持在该限制内,尽管使用 JavaScript 应该不成问题。

回答by VoodooChild

The maximum limit on line length is whatever you feel comfortable reading and editing. personally, any code of mine that won't fit on the screen without wrapping will be rewritten.

行长度的最大限制是您在阅读和编辑时感到舒适的程度。就个人而言,我的任何代码如果不包装就无法显示在屏幕上,都将被重写。

回答by hassan

How about putting all of the HTML on one line? How will browsers cope with that? I've read somewhere that some browser (can't remember which one) breaks with long lines.

把所有的 HTML 放在一行怎么样?浏览器将如何应对?我在某处读到某些浏览器(不记得是哪一个)以长行中断。

Stripping new lines from the file is an optimisation technique. You don't need to give clients the same easy-to-read version that you use when you develop it. You can give them a stripped version. Why? Bandwidth is not free.

从文件中剥离新行是一种优化技术。您不需要为客户提供与您在开发时使用的相同的易于阅读的版本。你可以给他们一个剥离的版本。为什么?带宽不是免费的。

回答by SherylHohman

Many "services" (eg Some terminalwindow implementations, GitHub, etc) will not wrap long lines and do not have scroll bars.
So anything beyond past 72 or 80 chars can simply get cut off.

许多“服务”(例如某些终端窗口实现、GitHub等)不会换行并且没有滚动条。
所以超过 72 或 80 个字符的任何东西都可以简单地被切断。

Also, it just helps for readability even if there are scrollbars, code can be easier to read/digest/debug if it can fit on (or mostly on) a single screen width.

此外,即使有滚动条,它也有助于提高可读性,如果代码可以适合(或主要适合)单个屏幕宽度,则代码可以更容易阅读/消化/调试。

So while, the code will run, it may not always be able to be displayed in its entirety.

因此,虽然代码将运行,但它可能并不总是能够完整显示。