Do you still limit line length in code?
Posted
by Noldorin
on Stack Overflow
See other posts from Stack Overflow
or by Noldorin
Published on 2009-05-24T13:22:48Z
Indexed on
2010/05/15
12:14 UTC
Read the original article
Hit count: 424
source-code
|language-agnostic
This is a matter on which I would like to gauge the opinion of the community: Do you still limit the length of lines of code to a fixed maximum?
This was certainly a convention of the past for many languages; one would typically cap the number of characters per line to a value such as 80 (and more recnetly 100 or 120 I believe). As far as I understand, the primary reasons for limiting line length are:
- Readability - You don't have to scroll over horizontally when you want to see the end of some lines.
- Printing - Admittedly (at least in my experience), most code that you are working on does not get printed out on paper, but by limiting the number of characters you can insure that formatting doesn't get messed up when printed.
- Past editors (?) - Not sure about this one, but I suspect that at some point in the distant past of programming, (at least some) text editors may have been based on a fixed-width buffer.
I'm sure there are points that I am still missing out, so feel free to add to these...
Now, when I tend to observe C or C# code nowadays, I often see a number of different styles, the main ones being:
- Line length capped to 80, 100, or even 120 characters. As far as I understand, 80 is the traditional length, but the longer ones of 100 and 120 have appeared because of the widespread use of high resolutions and widescreen monitors nowadays.
- No line length capping at all. This tends to be pretty horrible to read, and I don't see it too often, though it's certainly not too rare either.
- Inconsistent capping of line length. The length of some lines are limited to a fixed maximum (or even a maximum that changes depending on the file/location in code), while others (possibly comments) are not at all.
My personal preference here (at least recently) has been to cap the line length to 100 in the Visual Studio editor. This means that in a decently sized window (on a non-widescreen monitor), the ends of lines are still fully visible. I can however see a few disadvantages in this, especially when you end up writing code that's indented 3 or 4 levels and then having to include a long string literal - though I often take this as a sign to refactor my code!
In particular, I am curious what the C and C# coders (or anyone who uses Visual Studio for that matter) think about this point, though I would be interested in hearing anyone's thoughts on the subject.
Edit
Thanks for the all answers - I appreciate the variety of opinions here, all presenting sound reasons. Consensus does seem to be tipping in the direction of always (or almost always) limit the line length.
Interestingly, it seems to be in various coding standards to limit the line length. Judging by some of the answers, both the Python and Google CPP guidelines set the limit at 80 chars. I haven't seen anything similar regarding C# or VB.NET, but I would be curious to see if there are ones anywhere.
© Stack Overflow or respective owner