Back in the days, computers could only display 80 columns of text on screen and developers were forced to split up long lines of code so none of them exceeded this limit. Today, it’s unlikely that anyone has this problem, so why, people stuck with the line limit rule?
For me, the reason I stuck with this standard (more or less, I sometimes use 100 characters) is because of the other benefits it provides:
- Humans can’t read well too wide. With few columns you move your eyes less, you focus better.
- First line of defense against bad code: if you have a column limit you will be forced to refactor nested IF statements and other dangerous constructs.
- If your code is narrow enough, you can fit two files on screen, side by side, at the same time. This can be very useful if you’re comparing files, or watching your application run side-by-side with a debugger in real time.
- Code snippets of narrow code are much easier to embed into documents or blog posts.
Now let’s start the flame: should we all enforce the 80 characters line limit? Or let it be 100 characters? Why not 120?
Some people argue that a wider line can make code more readable. The 80-column limit is obsolete; modern equipment has wide screens that can easily show longer lines. Linus Torvalds is even more dramatic here:
“80 characters is causing too many idiotic changes. There are way worse problems in many patches than long lines. Too complex expressions. Too deep indentation. Pure crap code. People seem to get way too hung up on.. but at least it passes checkpatch.”
I agree with the fact that constraining the width of your code can sometimes require you to break up lines in unnatural places, but that’s not a reason to write your code in such way that functions are only viewable on a 30″ screen. That’s code smell and needs refactoring. That said, I agree with Linus: there are more important problems than the line limit. Choose a decent number, choose something that works for your organization and make sure you are consistent across your code base. I prefer, 100 for Java and 80 for C++, but I can live with any decent number.