181

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?

7

11 Answers 11

159

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.

12
  • 15
    These are great reasons to keep line width to 80 characters or less. I'm really surprised (disappointed) that your answer, which is clearly thought out and correct, didn't get more points. To this list, I would add: (1) horizontal scrolling is no fun. (2) You can greatly increase the density of the code you're working on by viewing that code in multiple colums. A great deal of real estate goes to waste when you have a few lines that extend far to the right when most of the other lines don't. Jul 12, 2013 at 21:30
  • 6
    ok but what happens when there is a block of code with few indentations ? that have happened to me and 80 characters are not fun at all.
    – EKanadily
    Feb 5, 2015 at 18:20
  • 35
    Limits the complexity in one line I'm not sure why spreading complexity across multiple lines is better. It just pushes more onto your mental stack.
    – Jonathan
    Jul 9, 2015 at 16:34
  • 11
    This is a very old topic. but do you still agree now that loads of developers use 27 inch monitors :-). I mean if sight is an issue a bigger screen can help. 8 years ago we were still working on 17 or 20 inch monitors and some on 4:3 resolutions even. Nov 21, 2017 at 9:35
  • 6
    Making the code narrower means that it ends up longer. So less can be seen at once. So it actually becomes harder to read IMHO.
    – Tuntable
    Aug 12, 2019 at 4:52
139

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.

11
  • 126
    I really don't believe you can compare reading natural language with reading a programming language in terms of usability.
    – Frug
    Dec 14, 2012 at 17:22
  • 35
    @Frug - actually, you probably can. The reason for the 65 character width isn't because larger lines can't be read, but that it's too tight an arc when the eye moves to the next line. You can get around this by increasing line height, but that makes it harder to use block spacing to convey meaning, so it's probably something to avoid in an IDE. Dec 23, 2012 at 23:33
  • 43
    @Jim - My natural language doesn't contain words with 30 characters in it (not that I use anyway) and it parses completely differently than a programming language. You can often group a line of code as separate from the rest, be it a long conditional or a combination of long methods and classes. Combine this with indentation and the comparison between the two languages becomes absurd. I have no doubt anyone scientifically studying readability and line length would object to your washing over the differences.
    – Frug
    Dec 27, 2012 at 16:52
  • 13
    @Frug - I don't really see how your objections engage with any of the claims I made, but I can see that indentation breaks the model I'm proposing. Don't call me 'Jim', though. Dec 28, 2012 at 0:10
  • 25
    A book is typically placed much closer to the eyes than a monitor, which means that fewer characters per line is allowed if the reader is to be able to read the book without having to crane the neck. A screen is typically not placed at the distance of a book, which means more characters per line can be used while keeping within the limits of the maximum eye angle. Also, code isn't read as much as it is perused, making this width less important. I (YMMV) can easily follow lines with 120 characters of code on my laptop screen, but this is too wide for 2 emacs buffers on my 15" laptop, alas.
    – fcar
    Aug 23, 2013 at 7:44
51

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.

2
  • 1
    plus one for the "sharp eyes" because really that what happened with me.
    – EKanadily
    Feb 5, 2015 at 18:22
  • The exception where horizontal scrolling should be tolerated is for constant data tables, to avoid rows spanning across multiple lines. Apr 13, 2021 at 19:52
38

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.

3
  • 7
    How does 120-150 chars per line work with having multiple windows open side by side? Do you keep many code editor windows open side by side? — On my 30'' monitor, I can have 3 windows side by side, if I limit my lines to 97 chars/line.
    – KajMagnus
    Dec 2, 2012 at 16:43
  • 3
    I code on a large display and I also like larger amounts. I aim for 110-130. One of my main goals is readability and breaking up statements into 2-3 lines is sometimes less readable in my opinion. I also will sometimes go to 500-1000 to hide junk I don't want to see such as some comments, disabled code, and some hard-coded values. I think it depends on the programmer also. If most coders operate at 80 then its best to aim for that when working with shared code. Feb 5, 2015 at 18:50
  • 120-150 line-length makes it impossible to display a three-way merge window which is the most efficient way of doing merges when the team is doing a lot of code changes overlapping with each other (which is the case for most of the teams). These kinds of ultra-wide lines can be soft-wrapped but that is ultra-ugly and hard-to-read as well.
    – sola
    Apr 28, 2022 at 8:07
10

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.

1
10

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.
1
  • 1
    I agree on the "see multiple things without clicking". We have the same need for visualizing a lot of data as pilots have in cockpits. IDE's do not necessarily understand this and waste a lot of pixels. May 28, 2022 at 11:23
8

Here is a study for you:

I teach programming for more than 30 years, and during this time I accumulated 1823 source codes in C with various routines, from random small games to neural networks, compilers, educational softwares, automatically generated lex/yacc source codes, etc.

Following the Pareto Principle, 80% of those programs have lines smaller than 159 columns. So, to cut the lower 20% and the upper 20%, we have:

  • 1823 source codes
  • 80% of them smaller than 159 columns
  • 80% of them bigger than 64 columns

Now that is a range that gives you freedom. You don't want to limit your code to 80 columns just for the sake of it, because sometimes you will need nested loops, functions, or some indentation that will be easy to understand in a bigger line, and if you forcibly distort your logic to fit an arbitrary column width, you are not using the best logic.

Sometimes, on the other hand, the size of a line is an indicator that your logic could be better, and your line could be smaller; specially if you are not in a nested part of the code and you already crossed the limit of, say, 160 columns.

Based on my own experience and readability, I recommend you write your code aiming for 80 columns, but allowing until 120 columns of margin, and never crossing the 160 columns limit.

Also, we should consider the older experience of reading that exists: books. Books are typographically created to easy the reader's eye movement, and the best sized column according to professionals in the area is ideally around 60 characters per line, not less than 40, not more than 90.

Since coding is not exactly reading a book we can go for the upper limit, and we should stay between 80 and 90 characters per line.

Unless you are programming low level code that will run in specific screen sizes and old monitors, than I would recommend you follow gopher standard, which is 67 characters per line.


Curiosity:

  • the biggest line in the 1823 source codes is a quine of 1020 columns in a single line
  • the biggest line of code that is not a single line is a text adventure game with 883 lines. Of course this is good practice, because if you don't limit the break of a line, the text will be better displayed in your screen, breaking in the column you actually have as size.
  • the smallest lines of code (non zero) are 14 characters long.
  • the command I used to check the file sizes is: find . -type f -name "*.c" -exec wc -L {} \; | sort -n | less -N
4
  • Are the sources student assignments? They may simply not reflect production quality code - full error handling gives a lot extra indented statements in my experience. May 24, 2022 at 20:03
  • To answer your question: yes, they are student assignments.
    – DrBeco
    May 25, 2022 at 23:25
  • That may actually be interesting - how do code change when programmers mature. May 26, 2022 at 9:08
  • Yep... It would be very nice to learn from such database. I guess the problem is to gather such data.
    – DrBeco
    Jun 3, 2022 at 3:54
4

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.

1
  • No, it didn't: ibm.com/ibm/history/ibm100/us/en/icons/punchcard ... and there's no such "typographical rule", and a rule based on the size of the alphabet makes no sense (oh those poor Chinese). And saying that you recall reading something somewhere is completely useless. Even if you did read it somewhere, so what? There's no reason to think that your source is correct/valid/authoritative ... it was probably written by the some sort of people posting their opinions here as answers to a request for studies ... papers that are based on facts, evidence, that sort of thing.
    – Jim Balter
    Sep 2 at 1:51
4

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.

2
  • being able to print code is almost a forgotten art. Being able to use a highlighter and comment in the margin is a great tool for understanding. May 24, 2022 at 20:05
  • The question is about studies, but all we get in these answers are personal opinions.
    – Jim Balter
    Sep 2 at 1:42
2

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.

1
0

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.