views:

622

answers:

10

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?

+4  A: 

IMO The 80 char width constraint is a remanent from the old DOS days. DOS Consoles used to be limited to 80 char lines I guess this was in in turn based on a limitation of old dot matrix printers/Graphics adapters.

So in other words its an arbitrary number use what ever suits you / your team the best.

Here is an article worth reading which discusses this.

SDX2000
+8  A: 

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.

anon
+1  A: 

While I don't know of any studies, you'll find plenty of opinions as answers to this question:

Adam Bellaire
+4  A: 

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
+1 for "horizontal scrolling is tedious"
SDX2000
A: 

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
+2  A: 

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
+1  A: 

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

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.

+1  A: 

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.

codedevour
A: 

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.

Michel Keijzers