Studies on optimal code width?

2020-05-11 00:15发布

If you enable the "View Right Margin" in your IDE of choice, it is likely that it will default to 80 characters. I tend to change it to 120 for no reason other than it was the standard at a company I was with a few years back, and no other company has told me to do it differently.

My question is, are there any studies that actually show 80 characters to be the optimal maximum width for code readability, or is this value just a "that's the way it's always been" and no one really knows why it is that way? And, should the width of a line of code be part of your coding standard?

10条回答
手持菜刀,她持情操
2楼-- · 2020-05-11 00:38

Maybe the 80 characters is also a good point to avoid these bad getter chains:

object.getFoo().getBar().getFooBar().get ...

if you limit it to 80 characters, maybe someone would localize these variables and do null check etc, but maybe most programmers would let them wrap in the next row. i don't know

Beside that, 80 characters are great as starblue mentioned. This should defenitely goes into the coding standards.

查看更多
趁早两清
3楼-- · 2020-05-11 00:45

I distinctly remember reading somewhere (I think it was in Agile Documentation) that for optimal readability a document's width should be about two alphabets, or 60-70 characters. I think the old terminals' line width came in part from that old typographical rule.

查看更多
家丑人穷心不美
4楼-- · 2020-05-11 00:48

To the best of my knowledge the 80 character is used as a coding standard to maintain compatibility with command line editors (default terminal width is typically 80 characters). With modern IDEs and large screen resolutions 80 characters is probably not "optimal", but for many developers maintaining readability in the terminal is essential. For that reason it is not likely that 80 character width will be replaced as the de facto standard for code width anytime soon. And to answer your final question, yes, code width as well as any other characteristic which will affect your code's readability should be addressed in your coding standards.

查看更多
Anthone
5楼-- · 2020-05-11 00:51

Have mercy on the programmers who have to maintain your software later and stick to a limit of 80 characters.

Reasons to prefer 80:

  • Readable with a larger font on laptops

  • Leaves space for putting two versions side by side for comparison

  • Leaves space for navigation views in the IDE

  • Prints without arbitrarily breaking lines (also applies to email, web pages, ...)

  • Limits the complexity in one line

  • Limits indentation which in turn limits complexity of methods / functions

Yes, it should be part of the coding standard.

查看更多
登录 后发表回答