If there's any accepted industry standard for maximum line width, it's 80 characters. I've used that maximum for years, and it's a good maximum. Like all other programmers, other people's code annoys me. The most common annoyance is that people write too wide code.
Ideally, one line of code is a unit element that means or performs something specific – a part of a sentence if you will. It is generally agreed that the ideal length for a line of code is from 80 to 100 characters.
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.
As humans, we can digest text much more efficiently when the sentence length is short. Code line length is no different. Even when I write these blog posts in markdown, I put hard line breaks at 80 characters because it makes the text so much easier to read.
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.
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.
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.
I normally use 120-150 unless the company describes otherwise. However it depends also on the kind of code:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With