180

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?

SmacL
  • 22,555
  • 12
  • 95
  • 149
Fostah
  • 11,398
  • 10
  • 46
  • 55
  • 3
    no studies that I know of but you might find it interesting to look at different projects coding standards. For example Google's are 80 characters. (http://code.google.com/p/google-styleguide/) where as WebKit (ala Apple's?) have no limit AFAIK (http://www.webkit.org/coding/coding-style.html). Mozilla's appears to be 80 (https://developer.mozilla.org/En/Mozilla_Coding_Style_Guide#Line_length) – gman May 16 '12 at 05:45
  • 1
    While I don't know of any studies, you'll find plenty of opinions as answers to this question: * [Is there a valid reason for enforcing a maximum width of 80 characters in a code file, this day and age?](http://stackoverflow.com/questions/110928/) – Adam Bellaire Feb 23 '09 at 15:51
  • Perhaps related (for prose): https://en.wikipedia.org/wiki/Line_length – Emile Cormier Feb 20 '22 at 00:42
  • a good formatting style enforced by tooling can help a lot here – Thorbjørn Ravn Andersen May 24 '22 at 20:07
  • Required a 2022 answer – Sajith Mantharath Jul 17 '22 at 14:23
  • "no one really knows why it is that way?" -- we very much know why it is that way: https://www.ibm.com/ibm/history/ibm100/us/en/icons/punchcard/ – Jim Balter Sep 02 '23 at 01:20
  • I suppose it's a good thing this was closed, not for the bogus reason given, but because the "answers" don't answer the question and are just rationalizations, often profoundly stupid ones like "I'd say the same general principles for optimal line length in books are applicable to reading code. That is, narrower columns are read more quickly, whereas wider columns are read more slowly." -- reading code as quickly as possible is not a goal. – Jim Balter Sep 02 '23 at 01:57

11 Answers11

160

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.

starblue
  • 55,348
  • 14
  • 97
  • 151
  • 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. – Donnie Cameron Jul 12 '13 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 05 '15 at 18:20
  • 34
    `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 09 '15 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. – Mathijs Segers Nov 21 '17 at 09:35
  • 4
    @MathijsSegers regardless of monitor size or resolution, it's still more comfortable to keep text within the middle 30 degrees of your field of vision. When working with multiple windows open in side-by-side monitors, I tend to turn my head to look from one to the other. A person shouldn't have to turn their head or rotate their eyes all the way in order to read from one end of a line to the other. So much rapid eye or head rotation would probably cause vertigo if done all day. – maurice Feb 05 '18 at 18:46
  • 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 '19 at 04:52
  • I'm a proponent of the 80 character limit, except for data tables (e.g. constant arrays of structs) where making rows span across multiple lines makes it harder to read. This is one exception where I don't mind horizontal scrolling for the sake or clarity and easier maintenance. – Emile Cormier Apr 13 '21 at 19:50
  • 2
    I personally prefer 100 due to the fact I prefer 4 spaces indentation. – basickarl Feb 18 '22 at 21:52
  • @Jonathan how does using line breaks push more onto a reader's mental stack? – duhaime Nov 09 '22 at 16:39
  • 1
    Reading this answer 14 years later (as someone who indeed maintains earlier written code), it is amusing to see an 80-character recommendation given I have Visual Studio open right now on my 4k monitor with 4 side-by-side screens of code, each no less than 110 columns each. The codebases which hard wrapped lines prematurely to 80 are much harder to follow their zig-zag pattern of fragmented statements than the ones that *target* 80 but flexibly allow up to 120 columns. – Dwayne Robinson Apr 13 '23 at 04:00
  • @DonnieCameron It shouldn't have received *any* upvotes since it doesn't answer the question. Neither do any of the other "answers", which are just opinions that have nothing to do with the question asked--about *studies*. – Jim Balter Sep 02 '23 at 01:32
  • @DwayneRobinson Indeed. Remarkable are the mental contortions that people will employ to *rationalize* an *arbitrary* number that comes from *ancient* technology: https://www.ibm.com/ibm/history/ibm100/us/en/icons/punchcard/ – Jim Balter Sep 02 '23 at 01:36
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.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
  • 125
    I really don't believe you can compare reading natural language with reading a programming language in terms of usability. – Frug Dec 14 '12 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. – Jimmy Breck-McKye Dec 23 '12 at 23:33
  • 42
    @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 '12 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. – Jimmy Breck-McKye Dec 28 '12 at 00:10
  • 1
    I second that you should look how books do it more often. For example, it irritates me how bad software often is at putting margins or similar space where it is needed. E.g. in Eclipse there is no space between line numbers and program text. – starblue Jul 13 '13 at 09:48
  • 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 '13 at 07:44
  • 3
    Let's just gather our gall and face the elephant in this room: @Obscaenvs just made more sense then everyone else here combined. Nailed the heart of the issue right on the head. – ChaseMoskal Feb 08 '14 at 23:42
  • Thank you, @ChaseMoskal! ...now, where's that "Follow" button? :) – fcar Feb 15 '14 at 09:39
  • 2
    This answer should have been downvoted not upvoted, it does not answer the question. It's just popular because so many programmers are opinionated about it. – oz123 Feb 16 '16 at 10:05
  • In these discussions people always pretend all the lines will have the limit. – dawid Jun 02 '20 at 22:21
  • @oz123 I downvoted it both because, as you note, it has nothing to do with the question but is just one person's baseless opinion, and also because the logic is absurd and doesn't even account for font size. The only good thing about it is that it correctly notes where 80 columns came from. (BTW, punch cards were 187 mm wide, which was not in any way related to the width of a book.) – Jim Balter Sep 02 '23 at 01:11
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.

Jay Bazuzi
  • 45,157
  • 15
  • 111
  • 168
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.

gustavohenke
  • 40,997
  • 14
  • 121
  • 129
Michel Keijzers
  • 405
  • 4
  • 2
  • 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 02 '12 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. – SunsetQuest Feb 05 '15 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 '22 at 08: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.

Christopher Klewes
  • 11,181
  • 18
  • 74
  • 102
  • 10
    FYI, excessive method chaining like this is known as the [train wreck anti-pattern](http://c2.com/cgi/wiki?TrainWreck). – Dennis May 22 '16 at 16:35
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.
maurice
  • 334
  • 3
  • 14
  • 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. – Thorbjørn Ravn Andersen May 28 '22 at 11:23
7

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
DrBeco
  • 11,237
  • 9
  • 59
  • 76
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.

lindelof
  • 34,556
  • 31
  • 99
  • 140
  • No, it didn't: https://www.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 02 '23 at 01: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.

Nir
  • 29,306
  • 10
  • 67
  • 103
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.

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.