183

Seriously. On a 22" monitor, it only covers maybe a quarter of the screen. I need some ammo to axe down this rule.


I'm not saying that there shouldn't be a limit; I'm just saying, 80 characters is very small.

5
  • All answers pretty much state what I wanted to add. To give you a real life example - I have a x61s, the resolution is 1024x768. When I am on the road, I don't have my fancy monitor. Opening code in my IDE is a pain when it exceeds this rule.
    – Till
    Sep 21, 2008 at 13:10
  • possible duplicate of stackoverflow.com/questions/95575/…
    – Roger Pate
    Apr 10, 2010 at 21:27
  • Even if you have a set of 3 monitors. This is not a reason to shaking head right to left and back. Forever. Ah-ha-ha. Actually the eye moves faster than head. Do you know about columns into newspapers? The reason of the width is the eye/head/man convenience.
    – Ivan Black
    May 20, 2016 at 20:22
  • 6
    Update 12/13/2021: Merged : The Linux kernel has officially deprecated its coding style that the length of lines of code comply with 80 columns as the "strong preferred limit".31-May-2020 git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/…
    – Israr
    Dec 13, 2021 at 18:10

32 Answers 32

291

I think the practice of keeping code to 80 (or 79) columns was originally created to support people editing code on 80-column dumb terminals or on 80-column printouts. Those requirement have mostly gone away now, but there are still valid reasons to keep the 80 column rule:

  • To avoid wrapping when copying code into email, web pages, and books.
  • To view multiple source windows side-by-side or using a side-by-side diff viewer.
  • To improve readability. Narrow code can be read quickly without having to scan your eyes from side to side.

I think the last point is the most important. Though displays have grown in size and resolution in the last few years, eyes haven't.

13
  • 7
    They may have "largely gone away", but not entirely. I tend to work with two different setups: 1) in an ssh window connected to a remote machine. which is 80 characters wide by default. and 2) In Visual Studio, with two panels side-by-side so I can see header and cpp file at the same time.
    – Enno
    Nov 13, 2008 at 21:27
  • 13
    @steffenj: Actually, books tend to shoot for about 66 characters per line (although this varies somewhat depending on other parameters) because longer lines do make reading more difficult. Maximum code line length could be argued, but 80 is convenient for historical and practical reasons.
    – Steve S
    Jan 26, 2009 at 16:53
  • 87
    The problem is by forcing people to keep line lengths short they tend to use less meaningfull names.. Jan 21, 2010 at 7:46
  • 11
    I find the remarks about readability quite interesting, because the thing I really hate about printed programming articles/books/... is, that the short lines which are used for the code examples are so extremely hard to read. It can make a lot of sense to move something to a new line, but the grouping should occur logically, dissecting the expression recursively, not because the output device accidentally reached its limitation. IOW, I find that devices which impose such narrow restrictions are not well suited to display code. Mar 30, 2010 at 13:51
  • 12
    I think the problem with 80-column enforcers is that they forget that the code instead grows in the vertical direction. You get the same problem, but in the vertical direction AND of top of this modern code looks awful when you have to break single statements over two or sometimes up to four or five lines. It is NOT more readable. With modern code I mean descriptive variable names and qualifying inheritance, namespaces, classes etc. Please stop the 80 column nonsese, use common sense instead. 120 is better, but should not be a rule either.
    – Larswad
    Feb 2, 2015 at 17:24
87

The origin of 80-column text formatting is earlier than 80-column terminals -- the IBM punch card dates back to 1928, and its legacy to paper tapes in 1725! This is reminiscent of the (apocryphal) story that the US railway gauge was determined by the width of chariot wheels in Roman Britain.

I sometimes find it a bit constricting, but it makes sense to have some standard limit, so 80 columns it is.

Here's the same topic covered by Slashdot.

And here's an old-school Fortran Statement:

FORTRAN punch card

65

80 characters is a ridiculous limit these days. Split your code lines where it makes sense, not according to any arbitrary character limit.

10
  • 3
    The character limit does not tell you WHERE you have to split a line of code, but WHEN
    – gztomas
    Aug 25, 2015 at 16:30
  • 2
    No it is not. If you write a longer than 80 chars line, you probably already have a problem in expression complexity or naming strategy. As others have mentioned, readability is a top concern and reading speed starts dropping above 60-66 chars (typography, based on human physiology).
    – sola
    Apr 17, 2018 at 8:55
  • 2
    @sola Your comment appears here with 98 chars, and it is a dense natural non-native language (to me) to understand. Completely legible. A code with until 3-4 indents, syntax markers etc. is even easier.
    – dawid
    Jun 3, 2020 at 23:38
  • 2
    @sola Those studies had literally nothing to do with programming they where to do with normal text (mostly) in the english language. They are pretty dubious not to mention especially when you consider that they contradict how many languages fundamentally work. Not to mention were part of the vast replication crisis in psych. Feb 23, 2022 at 10:53
  • 1
    @sola This fashion of blindly espousing bad science as if were fact because someone has written a paper about it, rather than treating it as what it is i.e. merely popularised conjecture is pretty endemic. Feb 23, 2022 at 10:55
36

You should just do it for the sake of everyone who doesn't have a 22 inch widescreen monitor. Personally, I work on a 17 inch 4:3 monitor, and I find that more than sufficiently wide. However, I also have 3 of those monitors, so I still have lots of usable screen space.

Not only that, but the human eye actually has problems reading text if the lines are too long. It's too easy to get lost in which line you are on. Newspapers are 17 inches across (or somethign like that), but you don't see them writing all the way across the page, same goes for magazines and other printed items. It's actually easier to read if you keep the columns narrow.

10
  • 17
    Not when you add indenting into the mix. If you use 4 spaces per indent, and you're in something like, namespace->class->method->if->for, that's 1/5th of your space blown.
    – TraumaPony
    Sep 21, 2008 at 12:50
  • You could always set the rule at 80 chars from the indent. That way the eye can easily follow it. Sep 21, 2008 at 13:09
  • Sometimes, (but not always) I wish .Net had automatic namespacing so that your didn't have to define the namespace in the file. That seriously messes with the alignment of your code. if you want nested namespaces, you have really big problems.
    – Kibbee
    Sep 21, 2008 at 14:48
  • 17
    However, reading prose is not the same as reading code.
    – Atario
    Nov 17, 2008 at 20:27
  • 3
    +1 for newspapers, great example. @Atario, reading GOOD code is very much like reading prose. May 18, 2012 at 18:37
27

When you have a sequence of statements that repeat with minor variations it can be easier to see the similarities and differences if the they are grouped into lines so that the differences align vertically.

I'd argue that the following is much more readable than it would have been if I'd split it over multiple lines:

switch(Type) {
case External_BL:   mpstrd["X"] = ptDig1.x - RadialClrX;    mpstrd["Y"] = ptDig1.y - RadialClrY;    break;
case External_BR:   mpstrd["X"] = ptDig1.x + RadialClrX;    mpstrd["Y"] = ptDig1.y - RadialClrY;    break;
case External_TR:   mpstrd["X"] = ptDig1.x + RadialClrX;    mpstrd["Y"] = ptDig1.y + RadialClrY;    break;
case External_TL:   mpstrd["X"] = ptDig1.x - RadialClrX;    mpstrd["Y"] = ptDig1.y + RadialClrY;    break;
case Internal_BL:   mpstrd["X"] = ptDig1.x + RadialClrX;    mpstrd["Y"] = ptDig1.y + RadialClrY;    break;
case Internal_BR:   mpstrd["X"] = ptDig1.x - RadialClrX;    mpstrd["Y"] = ptDig1.y + RadialClrY;    break;
case Internal_TR:   mpstrd["X"] = ptDig1.x - RadialClrX;    mpstrd["Y"] = ptDig1.y - RadialClrY;    break;
case Internal_TL:   mpstrd["X"] = ptDig1.x + RadialClrX;    mpstrd["Y"] = ptDig1.y - RadialClrY;    break;
}

Update: In the comment's it's been suggested that this would be a more succinct way of doing the above:

switch(Type) {
  case External_BL: dxDir = - 1; dyDir = - 1; break;
  case External_BR: dxDir = + 1; dyDir = - 1; break;
  case External_TR: dxDir = + 1; dyDir = + 1; break;
  case External_TL: dxDir = - 1; dyDir = + 1; break;
  case Internal_BL: dxDir = + 1; dyDir = + 1; break;
  case Internal_BR: dxDir = - 1; dyDir = + 1; break;
  case Internal_TR: dxDir = - 1; dyDir = - 1; break;
  case Internal_TL: dxDir = + 1; dyDir = - 1; break;
}
mpstrd["X"] = pt1.x + dxDir * RadialClrX;
mpstrd["Y"] = pt1.y + dyDir * RadialClrY; 

although it now fits in 80 columns I think my point still stands and I just picked a bad example. It does still demonstrate that placing multiple statements on a line can improve readability.

11
  • 11
    By saying that there are only small differences from line to line, you also say, that there is a lot of redundant code. Removing some of that could significantly decrease the number of columns and still be vertically aligned.
    – foraidt
    Oct 23, 2008 at 16:29
  • @mxp: agreed. If there's a more concise way of writing the above I'd be interested to see it.
    – Sam Hasler
    Oct 24, 2008 at 13:01
  • 1
    I agree with the general idea, but the example... What about this: switch(...) { case ...BL: dxDir = - 1; dyDir = - 1; break; case ...BR: dxDir = + 1; dyDir = - 1; break; ... } ...["X"] = pt1.x + dxDir * Rad...X; ...["Y"] = pt1.y + dyDir * Rad...Y;
    – Yarik
    Oct 27, 2008 at 8:29
  • 52
    The fact that I need to scroll the first of the two examples horizontally kind of proves the pint about shorter lines being better :-)
    – Enno
    Nov 13, 2008 at 21:30
  • 1
    I don't understand the hate for scrolling? It's a common opinion, and I'm not saying it's wrong, I just don't understand it. Especially if you're in a code editor, you don't even need to move your hands off the keyboard to get to the mouse -- just (ctrl+)arrow over or hit end
    – KOGI
    Jul 13, 2013 at 0:10
23

Printing a monospaced font at default sizes is (on A4 paper) 80 columns by 66 lines.

17

I use the the advantage of bigger screens to have multiple pieces of code next to eachother.

You won't get any ammo from me. In fact, I'd hate to see it changed since in emergencies I still see rare cases where I need to change code from a text-console.

14

Super-long lines are harder to read. Just because you can get 300 characters across on your monitor doesn't mean you should make the lines that long. 300 characters is also way too complex for a statement unless you have no choice (a call that needs a whole bunch of parameters.)

I use 80 characters as a general rule but I'll go beyond that if enforcing it would mean putting a line break in an undesirable location.

5
  • There are studies that show that people can read and follow x amount of characters/words, before they loose track. I am thinking 80 is in there somewhere. I don't have any sources to back that up though.
    – Till
    Sep 21, 2008 at 13:19
  • Yeah, I think really, it's not about keeping the lines short so much as about keeping the lines clean/concise/readable/understandable.
    – KOGI
    Jul 13, 2013 at 0:12
  • If you have a (a call that needs a whole bunch of parameters.) you need to do some refactoring anyway. Oct 6, 2014 at 13:33
  • @Zarremgregarrok I've seen some very long parameter lists in Microsoft APIs. Oct 6, 2014 at 17:29
  • @LorenPechtel Does that make it well written? Oct 21, 2014 at 10:00
9

The only thing I enforce to stay within 80 chars is my commenting.

Personally...I'm devoting all my brain power (what little there is) to coding right, it's a pain to have to go back and break everything up at the 80 char limit when I could be spending my time on the next function. Yes, Resharper could do it for me I suppose but then it freaks me out a little that a 3rd party product is making decisions on my code layout and changes it ("Please don't break my code into two lines HAL. HAL?").

That said, I do work on a fairly small team and all of our monitors are fairly large so worrying about what bothers my fellow programmers isn't a huge concern as far as that goes.

Seems though some languages encourage longer lines of code for the sake of more bang for the buck (short hand if then statements).

0
8

In the Linux coding standard, not only do they keep the 80 character limit, but they also use 8 space indentation.

Part of the reasoning is that if you ever reach the right margin, you should consider moving an indentation level into a separate function.

This will make clearer code because regardless of indentation lengths, it is harder to read code with many nested control structures.

1
  • 4
    How about reading code with many function calls? Surely there's a compromise between these two approaches... May 27, 2009 at 10:31
7

The other answers already summed things up nicely, but it is also worth considering when you might want to copy & paste some code into an email, or if not code then a diff.

That's a time when having a "max width" is useful.

7

I have two 20" 1600x1200 monitors and I stick to 80 columns because it lets me display multiple text editor windows side-by-side. Using the '6x13' font (the trad. xterm font) 80 columns take up 480 pixels plus the scrollbar and window borders. This allows one to have three windows of this type on a 1600x1200 monitor. On windows the Lucida Console font won't quite do this (the minimun usable size is 7 pixels wide) but a 1280x1024 monitor will display two columns and a 1920x1200 monitor such as an HP LP2465 will display 3. It will also leave a bit of room at the side for the various explorer, properties and other windows from Visual Studio.

Additionally very long lines of text are hard to read. For text the optimum is 66 characters. There is a point where excessively long identifiers start to be counterproductive because they make it hard to lay out code coherently. Good layout and indentation provides visual cues as to the code structure and some languages (Python comes to mind) use indentation explicitly for this.

However, The standard class libraries for Java and .Net tend to have a preponderance of very long identifiers so one cannot necessarily guarantee to be able to do this. In this case, laying out code with line-breaks still helps to make the structure explicit.

Note that you can get windows versions of '6x13' fonts Here.

1
  • Thank you for saying this! Big monitors are all the more reason for the 80 line limit, so you can fit more windows side-by-side. Not to mention that it's nice to be able to print source code (on paper) sometimes. Or paste snippets into other documents.
    – dreeves
    Jan 2, 2009 at 9:50
6

People say long lines of code tend to be complex. Consider a simple Java class:

public class PlaintiffServiceImpl extends RemoteServiceServlet implements PlaintiffService {

This is 94 characters long and the class name is quite short (by GWT standards). It would be difficult to read on 2 lines and it is very readable on one line. Being pragmatic about it and thus allowing "backwards compatibility", I'd say 100 characters is the right width.

4
  • 13
    I'm not a fan of horizontal scrollbars
    – bryc
    Feb 28, 2015 at 9:34
  • 18
    I'm surprised no one has said this, given that I'm several years late to this discussion, but I think newlines (maybe with an indent for clarity) right before "extends" and/or "implements" keywords would still produce very readable code.
    – mtraceur
    Nov 13, 2015 at 19:21
  • 11
    I love the fact that it says "it is very readable on one line" while at the same time I can't read the entire code snippet since it overflows the horizontal space in the browser. Point disproven.
    – oligofren
    Oct 4, 2018 at 11:17
  • @oligofren It doesn't overflow the horizontal space in the browser, it overflows the narrow widths that SO uses. The point proven here is that there are a lot of crappy tools that don't adapt correctly for window size, etc. People talk about lines getting truncated in their IDEs because their IDEs are too dumb wrap lines or because they haven't turned on that option.
    – Jim Balter
    Sep 2, 2023 at 2:24
5

You are not the only person who is going to maintain your code.

The next person who does might have a 17" screen or might need large fonts to read the text. The limit has to be somewhere and 80 chars is the convention due to previous screen limitations. Can you think of any new standard (120) and why it is a good idea to use that other then "that's what fits on my monitor at Xpt font?"

Remember, there are always exceptions to every rule so it you have a particular line or block of code that makes sense to be more than 80 chars then be a rebel.

But take the time first to think "is this code really that bad that it can not live within 80 chars?"

2
  • 1
    I will live with 80 chars when I can have 2spc tabstops. Better yet, actually use tabs for indentation, the requirement is when tabsize = 2, fits in 80 columns, use 4 most of the time for better readability. That way when you really have to choke down to 80 columns you can, but at a price.
    – Joshua
    Jan 26, 2009 at 16:52
  • If you are working professionally on a 17" screen with a resolution of 1024x768 in 2022 then sorry you are not someone we should be caring about catering for. If your tooling is forcing that limit on you then you are using out of date tools. It's a very tenuously disguised mandate by bad programmers to try to force even worse programmers to write "better" code all it actually does, however is make everyone write badly formatted code. Feb 22, 2022 at 14:07
5

I've widened my code out to 100 characters which fits comfortably in less than half my screen on my Macbook. 120 characters is probably the limit before lines start to get too long and complex. You don't want to get too wide else you encourage compound statements and deeply nested control structures.

The right margin is nature's way of telling you to perform an extra method refactoring.

5

I wonder if this might cause more problems in this day and age. Remember that in C (and possibly other languages) there are rules for how long a function name can be. Therefore, you often see very hard-to-understand names in C code. The good thing is that they don't use a lot of space. But every time I look at code in some language like C# or Java the method names are often very long, which makes it close to impossible to keep your code at a 80 characters length. I don't think 80 characters are valid today, unless you need to be able to print the code, etc.

5

As the author of coding guidelines for my employer I have upped the line length from 80 to 132. Why this value? Well, like others pointed out, 80 is the length of many old hardware terminals. And 132 is as well! It's the line width when terminals are in wide mode. Any printer could also make hardcopies in wide mode with a condensed font.

The reason for not staying at 80 is that I rather

  • prefer longer names with a meaning for identifiers
  • not bother with typedefs for structs and enums in C (they are BAD, they HIDE useful information! Ask Peter van der Linden in "Deep C Secrets" if you don't believe it), so the code has more struct FOO func(struct BAR *aWhatever, ...) than code of typedef fanatics.

and under these rules just 80 chars/line cause ugly line wraps more often than my eyes deem acceptable (mostly in prototypes and function definitions).

4

As others have said, I think it's best for (1) printing and (2) displaying multiple files side by side vertically.

4

I like to limit my width to 100 chars or so to allow two SxS editors on a widescreen monitor. I don't think that there is any good reason for a limit of exactly 80 chars anymore.

4

Use proportional fonts.

I'm serious. I can usually get the equivalence of 100-120 characters in a line without sacrificing readability or printability. In fact it's even easier to read with a good font (e.g., Verdana) and syntax coloring. It looks a little strange for a few days, but you quickly get used to it.

3
  • Really bad idea when you want to use 'indents' and a monospaced fonts.
    – Bersaelor
    Jul 31, 2015 at 10:32
  • 3
    @Bersaelor No, it works fine when you always indent using tabs only and set the tab width properly (width 4 monospaced are like maybe 7 proportional). Indentation works, you just can't do ASCII art, but I don't think ASCII art belongs in code.
    – maaartinus
    Jan 8, 2017 at 2:17
  • Personally, I am quite on the opposite side when programming. I find proportional code really hard to read. Sometimes, I even configure the IDE to use monospace fonts (yes, including menus). Oct 18, 2018 at 12:10
3

I try to keep things down near 80 characters for a simple reason: too much more than that means my code is becoming too complicated. Overly verbose property/method names, class names, etc. cause as much harm as terse ones.

I'm primarily a Python coder, so this produces two sets of limitations:

  1. Don't write long lines of code
  2. Don't indent too much

When you start to reach two or three levels of indentation, your logic gets confusing. If you can't keep a single block on the same page, your code is getting too complicated and tricky to remember. If you can't keep a single line within 80 characters, your line is getting overly complicated.

It's easy in Python to write relatively concise code (see codegolf) at the expense of readability, but it's even easier to write verbose code at the expense of readability. Helper methods are not a bad thing, nor are helper classes. Excessive abstraction can be a problem, but that's another challenge of programming.

When in doubt in a language like C write helper functions and inline them if you don't want the overhead of calling out to another function and jumping back. In most cases, the compiler will handle things intelligently for you.

3

There's already a lot of good answers to this, but it's worth mentioning that in your IDE you might have a list of files on the left, and a list of functions on the right (or any other configuration).

You're code is just one part of the environment.

2

I'm diffing side-by-side all day long and I don't have a freakin' 22 inch monitor. I don't know if I ever will. This, of course, is of little interest to write-only programmers enjoying arrow-coding and 300-char lines.

1
  • 1
    Did you ever get a 22" monitor? Aug 7, 2023 at 10:47
1

I actually follow a similar rule for my own code but only because of printing code to an A4 page - 80 columns is about the right width for my desired font size.

But that's personal preference and probably not what you were after (since you want ammo to go the other way).

What don't you question the reasoning behind the limit - seriously, if no-one can come up with a good reason why it's so, you have a good case for having it removed from your coding standards.

1
  • I'm pretty sure it's from the days when text mode screens were 80 characters wide.
    – TraumaPony
    Sep 21, 2008 at 12:47
1

Yes, because even in this day and age, some of us are coding on terminals (ok, mostly terminal emulators), where the display can only display 80 chars. So, at least for the coding I do, I really appreciate the 80 char rule.

1

I force my students to squeeze into 80 columns so I can print out their code and mark it up.

And about 17 years ago I let my own code expand to 88 columns, because I started doing everything using Noweb and 88 columns is what fits in a nicely printed document using TeX.

I indent by only two spaces, but the extra room is wonderful.

1

I thing not enforcing 80 characters means eventually word wrapping.
IMO, any length chosen for a max-width line is not always appropriate and word wrapping should be a possible answer.
And that is not as easy as it sound.

It is implemented in jeditalt text
(source: jedit.org) which offers word wrap

But it is bitterly missed in eclipse from a looong time ! (since 2003 in fact), mainly because a word wrap for text editor involves:

  • Wrapped line information is for the text viewer, code navigation, vertical rulers.
  • Unwrapped line information is required for functionalities like goto line, line numbering ruler column, current line highlight, saving file.
0

I still think that the limit isn't limited on the visual part. Sure, the monitors and resolutions are big enough to show even more characters in one line nowadays, but does it increase the readability?

If the limit is really enforced it's also a good reason to re-think the code and not to put everything into one line. It's the same with indentation - if you need to much levels your code needs to be re-thought.

0

Breaking at 80 characters is something you do while coding, not afterwards. Same with comments, of course. Most editors can assist you in seeing where the 80-characters limit is.

(This may be a little OT, but in Eclipse there is an option which formats the code when you save it (according to whatever rules you want). This is a little freaky at first, but after a while you start to accept that the formatting is no more in your hands than the generated code is.)

0

If we had one of these, we wouldn't be having this discussion! ;-)

But seriously the issues that people have raised in their answers are quite legitimate. However the original poster was not arguing against a limit, merely that 80 columns is too few.

The issue of emailing code snippets has some merit. But considering the evil things that most email clients do to pre-formatted text I think that line wrapping is only one of your problems.

As for printing I usually find that 100 character lines will very comfortably fit onto a printed page.

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