views:

2799

answers:

13

Everybody's screen size and font are different, so while my code fits on my screen, it might scroll off the right edge of your screen.

What should be the maximum length of a line of code?

+1  A: 

Again it depends on one's preferences. I guess for me given the quality of my eyes I like bigger fonts. And I guess everyone would be unanimous in saying we wouldn't want to do horizontal-scrolling. I don't keep count but I guess my number is in the vicinity of 70 characters, given that we tend to have longer variable and class names these days. (ah, good riddance, "Hungarian" notation!)

cruizer
A: 
Peter Turner
+3  A: 

I like it at about 100, that gives me enough room on the side for the solution explorer in Visual Studio / Eclipse. I think it will depend on the IDE you're using and how it's set up though.

I don't know if you can come up with a definitive answer for this, but I think within a development team it would be good for everyone to agree on the same number. It should be part of the team's style guide

Glenn Slaven
+1  A: 

At uni we are told to restrict it to a maximum of 80 characters. This is to accomodate command line interfaces. It also has the advantage of keeping your code clean.

mdec
+2  A: 

This isn't 1980, so lines can be longer than 80chars. Most ppl I know do somewhere around 100-120chars per line.

This is really something that you & your teammates should agree on, and then set your editors to make it obvious when you're at the limit.

jpeacock
Yeah, they can be, but does this make your code more readable? Hardly! Why do you think newspapers write in such small columns instead of writing in a single block over the whole page?
Mecki
Well-used whitespace is what makes code readable, not narrow columns. 80chars starts failing when you have a couple indent levels+famous java verbosity. Trying to format that to fit in 80chars is ugly :(
jpeacock
+1  A: 

80 characters seems to be a good rule of thumb, but I look at a couple of not especially complicated lines and I see lines easily 90 characters long.

I think it'll come down to personal preference, but I'd say between 70 and 100 characters.

Merus
+1  A: 

I try to keep my code below 80 characters. Code is much more readable that way.

yjerem
A: 

Anyone can make up some arbitrary value that they deem fits well with their personal preference, the size of their windows, the size of the font they use, their screen size, their eye-sight, the editor they're using, their screen resolution, their operating environment, the way they view code, print code, interact with code, the revision control they use etc.

In fact there are so many hidden variables with regard to this issue that I don't see any reason not to go with the 80 character suggested limit. It's a historical limit, but one that's still fairly reasonable and relevant.

I'm really not sure why this is such an issue for everyone. Go with 80 characters, or don't. Either way make sure the code that you are writing is readable to yourself and everyone else that needs to read it. If you're working in a team whose agreed coding standard is 80 characters per line the go with it!

Mathew Byrne
A: 

Having too short line length limits makes code much harder to read and encourages bad naming. People should name variables delegatingFooBarFactory instead of just dfbf. A call like

delegatingFooBarFactory.createNewFooBar(randomeFooBarProperty, "Example Name", someOtherProperty)

should not look like stairs because that's infinitely harder to read and parse what's going on. Its especially difficult if the calls are deeply nested or method parameters/arrays are broken into multiple lines.

EDIT: Of course my example looks bad here because StackOverflow limits the width of replies so I have a scroll bar.

In the days of large 20+ monitors being almost standard, I don't think it's unreasonable to have a 110+ character limit. At work, our standard is 125.

Laplie
A: 

IDesign says 120. I thought this was impractically high until I switched to the Consolas font. Now I swear by it. It's much easier to use multiple parameters and write error messages, etc.

alord1689
A: 

For PHP, the PEAR coding standard is 85 characters.

David Stockton
+5  A: 

You should keep your lines as short as possible; period. Why? Well, have you ever read a newspaper? Most likely yes. How do they write their articles? One big block going from the left page margin to the right one? No. But why do some people here insist on writing source code like this??? Newspapers write articles in very short columns. Why? Because it is much easier to read. Your eyes can quickly jump from left to right and then to the next row, without ever losing focus.

See what Jeff writes about it at Coding Horror

Let me quote the results:

  • Longer line lengths generally facilitate faster reading speeds.
  • Shorter line lengths result in increased comprehension.
  • The optimal number of characters per line is between 45 and 65.
  • Paging through online text generally results in better comprehension than scrolling.
  • Reading speed is faster for both single and multiple columns, but preference is for multiple short columns.
  • Left-justified text is read faster than full-justified text.

The shorter you keep the line length, the better the comprehension is. And when you read source code, isn't comprehension the most important property that matters? Doesn't it matter a lot more than just plain reading speed? Reading code without really understanding it is the main cause for bugs!

And optimal line length should be between 45 and 65 according to studies for an optimal reading experience. So the good old "don't exceed 80 characters" is a very good limit. It will make sure it won't break in real terminal (e.g. Linux without X11 is still having 80 characters by default, doesn't it?) and usually you try to stay somewhat below 80 (it looks ugly if everything goes to the furthest right screen border), thus you might stay within the 65 char limit actually.

Please note, I usually don't count indention as characters! Indention is just white-space. That means I can look at source with less in the terminal and just make the text scroll left/right as needed. If I "hide" indention to read the full line, I'm not hiding anything important (not if I just want to understand this single line); I only need to see the indention to find out how this line relates to other lines. Still I usually stay below 80 characters absolute line length (counting spaces in front as well and counting tabs as 4 spaces). However, if it's absolutely impossible to break the line and I have already 12 spaces in front, I may use up to 92 characters absolute line length for example (still you can always view the whole line without indention on a 80 char display).

Also I have found my very own way of breaking long lines. A lot of people look at this and think it's crap... but all people that had to read my code for a very long time sooner or later agreed, that this makes code easy readable and changeable and some actually adopted the style unintentionally (they started to break their lines the same way, without even thinking about it). It violates pretty much any coding style guideline, though. I use an example of another poster here:

delegatingFooBarFactory.createNewFooBar(
    randomeFooBarProperty,
    "Example Name",
    someOtherProperty
 );

It keeps lines short, it's easily readable, I increase indention so you can see where the parameters for the call start and I decrease it, so you can see where it ends. It's easy to add another parameter, replace one or remove one by just deleting the appropriate line or adding one (okay, in the last case, you need to add a comma before adding a line). Of course I don't always break that way, if I can make it fit into one line without breaking at all, this is preferred.

Mecki