Saturday, November 6, 2021

Does Standard Column Width of 80 On Coding Make Sense in Modern Era of Big Monitors?

One of the oldest coding practices is to keep line width 80, and many of us follow it blindly but have you ever thought about why we have this practice in the first place? I believe it was to make your code more readable in the age of small monitors so that the whole content can fit on the screen, or it might have originated from the age of punch cards, which was used to be 80 columns wide. This sounds reasonable when we think about those old days but do you think this rule makes sense now? We are now living in the age where most of the developers have got large monitors, which can show up to 180 characters, doesn't this is wastage of precious monitor space? It also makes your code unnecessary long, than it actually is.

I first come to know about line wrapping at 80, while reading Oracle Code Conventions for the Java Programming Language, which was last revised on April 20, 1999, which under indentation says
4.1 Line Length
Avoid lines longer than 80 characters, since they're not handled well by many terminals and tools.

Note: Examples for use in documentation should have a shorter line length-generally no more than 70 characters.

source: http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-136091.html#248


If I understood correctly (I may be wrong), one goal of this rule is consistency. I used to think that 80 was silly, but being able to go through source code written by a dozen different teams over the last 7 years and not needing to re-size my window is a really nice thing. Consistent column width helps with the pace of reading code.

Since I mostly worked with large monitors, like LG 32MP58HQ-P 32-Inch IPS Monitor with Screen Split, I also realize that we are wasting lots of precious space. The consistent column width of 80 is simply too little. I personally use 120 unless the project I work on already finalized a column width, in that case, I go for consistency.

One more reason people give for still using a column of 80 is that nowadays they are working with multiple files at once. For example, if you use standard column width you can fit a couple of files across a reason and can compare them line by line, which I believe is a real benefit.

You can even do a three-way merge inspection on one screen without scrolling sideways. By the way, this should not be done at cost of excessive wrapping. I understand that consistent columns make it easier to scan and read through text but it doesn't matter whether it's 80 or 120.

Why standard column width is 80 character, does it make sense


On a closing note, I would say that consistency is nice and you must go for it but 80 or even 100 is too short. Many developers could probably live with 120 or even 150 though.  Our modern widescreen high-definition LCD monitors can easily handle more.

It is much more readable than excessive wrapping because I personally find it much harder to read a wrapped line than just seeing the whole thing in one line. Of course, this is just preference and others will feel different.


Other Articles You May Like to Explore
10 Things Java and Web Developer Should Learn
10 Testing Tools Java Developers Should Know
5 Frameworks Java Developers Should Learn
Finally, Java has var to declare Local Variables
10 Books Every Java Programmer Should Read
10 Tools Java Developers uses in their day-to-day work
10 Books for Coders of All Level
5 Best Programming Langauge for Web Development

Thanks for reading this article so far. If you like this article then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.

17 comments :

Tomer Ben David said...

coding with shorter lines max 80 is an indication of good coding, concise coding. long line represent too many parameters, overlong function names, comments which are too long. So sticking with 80 chars can actually make your code more readable by a side effect of having better written code.

Martin Thoma said...

*AAAARGH* The preview deleted my longer, comment.

In short: 80 characters is nice for reading and for diffs.

Also LongClassAndNames.asWellAsLongMethodNames might be a problem of Java.

Anonymous said...

You can always get a monitor which can be flipped to a vertical position and take advantage of all the lost space.

Anonymous said...

80 Columns is generally easier to read - consider the width of pages in most books, magazines and even the width of your blogspot theme.

Additionally (as mentioned by Tomer Ben David) it is also an indication that the programmer has been concise with their code.

jul said...

80 cols is about limiting complexity.
PS btw as a sysadmin I had more than once to go patch only with vim in 80x24 some code.
https://www.kernel.org/doc/Documentation/CodingStyle

Now, some people will claim that having 8-character indentations makes
the code move too far to the right, and makes it hard to read on a
80-character terminal screen. The answer to that is that if you need
more than 3 levels of indentation, you're screwed anyway, and should fix
your program.

Anonymous said...

In addition to the rest: If you're worried about losing screen space to empty text buffers, get a modern text editor (like vim) that can view multiple files at once in columns. With 80-character widths, you can have three files viewable at once with most modern widescreen displays.

Anonymous said...

As everyone mentioned already, having a 80 cols is about limiting complexity on a single line and making things more legible by indenting and structuring your code properly.
An example would be 3 way diffs when merging branches in GIT, or during code review.

Zulko said...

A new good reason for limiting yourself to 70-80 characters is that the code in your blog or in your documentation may be read on a tablet or an iphone. And some devs like to have 3 files open side-by-side; so even on big screen it's better to have a short code.
In the end it may depend on the language, how much it requires imbricated patterns, and how much spaces people put in the indentation.

Luke Plant said...

There are a few considerations that mean we should still favour shorter lines today:

1) Readability - it is well known by typesetters readability gets much worse when you exceed about 60 characters per line, as your eye has trouble finding the next line if it has to jump a long way back. See https://en.wikipedia.org/wiki/Column_%28typography%29 . For source code the column limit should probably be higher than that, as you usually have significant indentation at the beginning which takes up some columns, and variation in indentation which means you don't have the "wall to wall" text that makes reading hard.

2) Given the prevalence of code 80 characters wide, and larger screens, we now have the possibility of being able to see multiple columns of code.

On my laptop monitor, I can get 200 columns of code, and my additional monitor I can get 280. With 80 char code, I can get 2 and 3 columns respectively, but with 120 I could get only 1 and 2 columns respectively.

Would you rather use the additional screen width to get wider code (which is harder to read), or extra columns (which might include other things, such as class browsers etc. in some IDEs)? I'm not a zealot for a hard limit of 80 chars where it doesn't make sense, but as a general rule I think it still helps.

MiguelGL said...

I stick to 100 char lines so that I still can have two editors side by side, which I do quite often.

Unknown said...

I dissagree that short lines encourage better code, for example whats better? :
nCust = nSub + nRed
or
numCustomers = numSubscribers + numReaders

If short lines encourage the former then we have a problem

Anonymous said...

Shorter rows are easier to the eye. I believe a column width of 80 still makes sense.

Maureen O'Hagan said...

I'm sure the 80 characters width goes back to the punchcard; programmers were forced to use that width while writing code and it's been the standard ever since. That said, I agree with keeping lines of code shorter; I too hate scrolling to see the end of a line, and I think we should respect the research which shows readability goes down on longer lines of type. Working on a PC gives you more width, but when I write code on the IBM, I have to use their text editor, which only shows 70 columns on a screen. BTW, I know it was just an example, but the comment arguing for longer code which included a line using more meaningful variable names was still only a little over 40 characters. You really don't want to pack too much in one line of code. You'll get confused, or the computer will. KISS!

Anonymous said...

I use 80 lines to 1) get more files side by side as you mentioned above, 2) print the code onto paper.
I print my important functions and tape them into my lab book so I have a record that fits into my other notes. I also like being able to print code for code reviews or to explain my algorithms. The code reviews I am referring too are not large group meetings, but meetings where I am sitting with a couple scientists reviewing an algorithm or explain logic of some idea, etc..
That said, I don't use short names for variables. I have plenty of code that wraps onto three or four lines. What I've found is that I can format it well and make it easier to read than a long line. For instance in a function I might have,
foo( something_useful, //variable 1
something_else_useful, //variable 2
something_else_also_useful) //variable 3
Expand that into something with 6+ inputs and I've got a nice template with comments so other developers don't have to go look at the header to figure out what the function is.

Anonymous said...

Probably I could get a 50 inch monitor and make my lines 500 chars. That would be great isn't it.

al123 said...

On my monitor i can put 350 collumns :)
But then people would complain.

Anonymous said...

Now that we browse code online with our tablets and phones (esp production problems), it's nice to stick to a shorter line like 80 chars.

Post a Comment