13

Well, does it? Is it considered bad practice?

IMO, it is less readable. I hate having to scroll to the right, then back left, right, left, and so on. It makes coding more painful and confuses me sometimes.

For example, anytime I am coding a long string, I will do the following...

bigLongSqlStatement = "select * from sometable st inner join anothertable at" +
"on st.id = at.id" +
"and so on..." +
"and so on..." +
"and so on..."
6
  • 16
    yes, it does. (and don't forget to indent the spilled lines)
    – Javier
    Mar 25, 2011 at 19:45
  • 2
    I would say that it does. From what I have seen it seems to be fairly standard practice among experience programmers to eliminate horizontal scrolling in the manner you outlined. To some extent though there's a bit of personal preference on this matter...
    – Kenneth
    Mar 25, 2011 at 19:47
  • 2
    I can navigate code vertically and within lines at high speed with a few keys, scrolling horizontally is annoying slow in comparision.
    – user7043
    Mar 25, 2011 at 19:50
  • Overwide columns make anything less readable. That's been determined many times over. Mar 25, 2011 at 19:50
  • 2
    I agree. Only problem is getting everyone to agree on how wide to make your editor. On extra wide code I usually just set wrap if I have a ton of it to read through. Mar 25, 2011 at 19:51

9 Answers 9

19

Yes, indeed it does, in the literal sense as well as the general sense.

I like to do side-by-side code diffs, and too-wide lines make that harder:

https://i.stack.imgur.com/fWVuz.jpg

Languages like Scala with triple-quoted strings allow you to construct a string from many lines without the runtime expense, unsightly quotes and plus signs (as seen in your example) of joining together parts of a string.

11

Yes , I think 80 characters per line is reasonable and commonly used.

6

It is a really important question to me! I've worked 7 months on a 13" laptop with colleagues having 24" desktop monitors, and I found myself spending lots of time shortening lines to end up with something readable.

80 columns is a bit small in lots of cases (except if you're working on a terminal with vi the only option ;)), but more than ~150 is too much (see below).

That's for the pure 'readability' question.

Now, for the 'good practice' part, I very often find such long lines to be flawed, ie having some part that should be extracted in a temporary variable, or which is duplicated, for instance (ObjectiveC, common snippet in iPhone programming):

CGPoint point = CGPointMake(someOtherView.frame.origin.x + someOtherView.frame.size.width, someOtherView.frame.origin.x + someOtherView.frame.size.height);

Please note this can become even nastier when working with 3 dimensional vectors or matrices.

Rewritten example:

CGRect frame = someOtherView.frame;
CGPoint origin = frame.origin;
CGSize size = frame.size;
CGPoint point = CGPointMake(origin.x + size.width, origin.x + size.height);

This is now fitting on a smaller screen, easier to debug using an IDE or some write to standard output, and might even be faster, depending on the cost of method/property invocation. This is a bit forced of course, most real world examples are much more complex...

4

Not always.

Just to add an alternative view, when I read code I can often get the gist of what the line of code is doing without having to read the entire line. If I can read the method name but the method parameters spill off the screen, I'm normally not to fussed as I can tell from the method name alone what the intent of that line of code is. If a few lines of code spill off the screen then I think the trade off of having to occasionally (important word there) scroll horizontally is worth it for the more compact code. I sometimes find multi-line single-statement code distracting as I have to mentally piece together which code goes with which statement.

Often, the lines of code which spill over horizontally have their important bits on the left (visible) and the less important bits on the right (off-screen), so to me this enhances readability as I can scan the code downwards mostly seeing the important bits on each line as opposed to the alternative of having the less important bits of code from a too-long line taking up the visually important left hand side space on the following line(s).

Having said all that, I certainly wouldn't want to scroll horizontally very often, but I'm finding this less of an issue in these wide-screen monitor days.

3
  • 2
    I didn't know that some program bits are more important than others. I will try to improve my productivity that way: coding only important bits.
    – mouviciel
    Apr 24, 2012 at 8:39
  • 1
    @mouviciel, Its not that the left side of the code is more important, but that semantically the left side of the code has more significance in understanding what the line of code does than the right. As you scan code, you often read just the beginning of the line to understand what it does before moving onto the next. Apr 24, 2012 at 9:22
  • 1
    For me, arguments passed to a method have as much significance as the method name. Leaving that information leads to guess what the code does more than understand it.
    – mouviciel
    Apr 24, 2012 at 9:41
1

Yes, it does.

Incidentally a tip. If you're using a language with multi-line strings (virtually all scripting languages have them) and are including long SQL, it really helps readability to put the SQL in a multi-line string using consistent formatting rules for the SQL. See http://bentilly.blogspot.com/2011/02/sql-formatting-style.html for the formatting style that I use.

1

Certainly is does. There is a reason newspapers and magazines use columns. Readability is a significant factor. When reading our eyes scan down with relatively little side to side movement. The effect is to allow our eyes to scan what we are reading quickly.

Even when visible on screen wide columns force our eyes to scan back and forth rapidly. While scanning back we don't really comprehend anything. This will significantly slow reading and comprehension. The effect is similar to old mechanical printers. These often required several null characters to be inserted after a carriage return to allow time for the carriage or print head to reposition for the next line.

Additionally, the vertical layout is usually done in a way to clarify the grouping of the contents on the line. This should usually only apply to compound logic conditions. Long formulas may be better structured as a series of statements. (The optimizer will fix any extra overhead, and some optimizers will give up or perform poorly on complex formulas.)

Identifiers with multiple dots requiring large lines indicate coding techniques which should be rectified.

1

No, it doesn't.

I have an editor. Not only does it have line wrap, it has line wrap indentation, which (if the screen is say 100 chars wide) would cause

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

to appear as

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut 
    labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco 
    laboris nisi ut aliquip ex ea commodo consequat.

or with whatever indentation level is set as default for the current language.

Lines wider than my screen never makes code less readable versus manually line-wrap-indented code.

edit: ooooh, I knew this answer would be unpopular :)

6
  • 3
    Good for you. But what would you do if you had to switch to an editor that didn't have this feature?
    – user20891
    Apr 24, 2012 at 1:21
  • 1
    @dunsmoreb: Why would you switch to an editor which is so outdated that it doesn't support even word wrap (unless you work on source code written thirty years ago and work on a legacy platform where you don't have choice of a correct editor)? Apr 24, 2012 at 10:39
  • MainMa, I'm referring to your line wrap indentation feature.
    – user20891
    Apr 24, 2012 at 11:07
  • @dunsmoreb: to be fair, even just word wrap is plenty good enough if long lines are uncommon
    – amara
    Apr 24, 2012 at 14:13
  • 7
    Just because your editor can wrap a line, doesn't mean it's going to wrap it the most logical place for readability.
    – Craige
    Apr 24, 2012 at 14:53
0

Mouses-wheels make it easy to scroll vertically fast ... scrolling horizontally is too expensive in comparison.

0
0

Avoid horizontal scrolling.

~Windows User Experience Interaction Guidelines Pg 112

Yes.

Englishmen read from left to right resulting in Constant scrolling = Nonproductive

For this reason I always enable word wrap with visual line glyphs in my IDE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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