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:25

Actually, the 80-column thing long precedes DOS. It comes from card punches, which were 80-column devices.

And to kind of answer the OP's question, one "study" has been going on for about 600 years now - the printed book. These have evolved over the centuries, with readbility foremost in mind, to the position we are at now where the average line length for text is around 60 characters. So for readability, go for narrower margins.

查看更多
乱世女痞
3楼-- · 2020-05-11 00:28

I don't have studies, but I will relate my experience.

I find that horizontal scrolling is tedious when dealing with text. I look at the environment that the code will be used in, and set width standards based on that context.

For example, when I worked in Emacs on XWindows, it worked well to have 2 Emacs windows side-by-side at all times. That limited them to 80 characters, so that was my max line length.

At one point I worked in Visual Studio on a 1920x1200 screen. I'd keep it maximized, with all tool windows docked down one side. There was enough space left for two editor windows side-by-side at around 100 characters.

I also find that the longest lines come from method calls with long parameter lists. This is sometimes a code smell: perhaps the method should be refactored.

If you & your co-programmers have high-resolution screens and sharp eyesight, by all means use a small font and long lines. Conversely, you may need short lines.

查看更多
不美不萌又怎样
4楼-- · 2020-05-11 00:30

Disregarding hardware restrictions, and any differences in how we read code versus natural language, I see three primary reasons to limit lines to around 80 characters.

  1. Human eyeballs are round, not really narrow and wide, and most of their resolution is in the middle. When reading for hours at a time it is a lot more comfortable to sweep the eyes in short arcs, using one scroll bar as needed. I don't know of a formal study specific to the legibility of code, but from my own observations, with the monitor 2 feet away, with text sized at a 10pt monospaced font, 100 characters takes up about 1/3 of my horizontal field of vision, or around 60 degrees (outside of the 30 degrees or so where all our eyes' resolution is at).
  2. Most people use a large monitor at work so that they can see multiple things without clicking back and forth, not so that they can see one thing really big.
  3. Shorter lines contain less complexity, which hopefully forces a developer to break up make their code into more digestible units.
查看更多
放荡不羁爱自由
5楼-- · 2020-05-11 00:30

The right margin option is intended to show you the width of the page if you're going to print the code, and has previous posted said it was set to 80 because that's what the line length historically was before GUI all the way back to punch cards.

I've seen a recommendation on some blog recently (can't remember what blog) to increase you IDE font size in order to improve code quality, the logic behind it is that if less code fits on screen you'll write shorter lines and shouter functions.

In my opinion shorter lines make reading the code and debugging it easier, so I try to keep the lines short, if you have to set a limit to make yourself write better code then choose what works for you - also if you are more productive with longer lines feel free to increase the page size and code only on a wide screens.

查看更多
看我几分像从前
6楼-- · 2020-05-11 00:33

I normally use 120-150 unless the company describes otherwise. However it depends also on the kind of code:

  • I (almost) never use multiple statements on one line
  • I only use long lines (>12) only if lines that look similar can be aligned and not broken.
  • I always use enough spaces/parenthesis etc
  • I prefer longer variables names above shorter names

Until a few years ago I limited to 100 but now widescreens are normally used and high resolution monitors 120 can be even seen on laptops (which I barely use).

Comparing a screen to a book is not really good because a book has more vertical space and a screen has more horizontal space. I always try to keep a function max. one visible screen long.

查看更多
We Are One
7楼-- · 2020-05-11 00:36

As some people have pointed out in other answers the reason for the 80 character limit is partly historical (punch cards, small screens, printers etc) and partly biological (to track what line you are in it's generally good to be able to see the entire line without needing to turn the head).

That said, please remember that we are still humans and we build tools to solve our own limitations. I propose you ignore the entire debate about character limitation and just write stuff that makes sense regardless of their length, and use an IDE or text editor that can help you keep track of the lines properly. Using the same argument for indentation in the tabs vs spaces debate, as well as the how wide should the indentations be I propose you use an indentation marker (most commonly the tab) and just have people configure their own IDE or text editors to display them as they find most comfortable to them.

Sticking with a fixed number of characters per line will always make things worse for everyone but the targeted audience. That said, if you will never share the code, ever; then there's really no reason to even have this discussion to begin with. Should you want to share the code, you should probably let people decide what they want on their own instead of forcing yours (or someone elses) ideals on them.

查看更多
登录 后发表回答