80 columns limit

Arseni Mourzenko
Founder and lead developer
161
articles
November 6, 2014
Tags: code-style 3 short 48

I re­call dis­cussing 80 columns lim­it a year ago with a friend of mine. It's fun to read this part now, to see how I changed, and to try to un­der­stand what could my friend tell me to con­vince me that I was wrong.

This is the quote I'm talk­ing about:

I sim­ply can­not get why would any­one en­force such ab­surd lim­i­ta­tion. This feels wrong. This looks wrong. This looks as wrong as eight-spaces-sized tab in Win­dows Notepad; no, wait, this is even wronger.

I can un­der­stand why this was done in, say, sev­en­ties. This was an ac­tu­al hard­ware lim­i­ta­tion, so we had a choice: ei­ther we add line breaks in the code to avoid lines which are too long, or we learn it the hard way, through cryp­tic bugs. But this was a dif­fer­ent era.

To­day, I have my two (on re­quest three) 23'' mon­i­tors. With 80 char­ac­ters lim­i­ta­tion, my Vi­su­al Stu­dio will look emp­ty even if I stack two files side by side.

More im­por­tant­ly, with no length lim­i­ta­tion, I have an ac­tu­al choice. Most, if not all IDEs dis­play long lines as a set of lines with spe­cial ar­rows show­ing that the new line is not an ac­tu­al new line. Line num­bers make things vi­su­al­ly easy too. This means that I can dis­play the same file with its very long lines span­ning the en­tire width of my mon­i­tor, or I can re­size the win­dow to fill only the half of my screen, and the text dis­play is read­just­ed im­me­di­ate­ly. If I de­cide to make the doc­u­ment nar­row, say 60 char­ac­ters long, I can do that, and the text will still be dis­played cor­rect­ly. Now, with 80-char­ac­ters lim­i­ta­tion, the text will look stu­pid for any res­o­lu­tion out­side the 80-90 char­ac­ters range. Make the win­dow wide? You'll have too much white-space. Make it nar­row? It will look ter­ri­ble.

Aside that, how would you like me to write any code in C#? Name­space, class and method, this makes an in­dent of three, which means twelve spaces. I'm now with six­ty-eight char­ac­ters left for the ac­tu­al code, or ac­tu­al­ly six­ty-four if I need to do any­thing use­ful, such as do­ing some­thing in a using state­ment or in a foreach loop. An if in a trans­ac­tion means that I have six­ty char­ac­ters for the ac­tu­al code.

Then we ask our­selves, why are be­gin­ner pro­gram­mers nam­ing their vari­ables a, b and c, and we blame their teach­ers or the stu­pid ex­am­ples from books, where vari­ables are ac­tu­al­ly named x, y or n. But shouldn't we blame the 80 char­ac­ters lim­it? generalCrossLink is 16 char­ac­ters long, and this is a rather short iden­ti­fi­er. I've seen much longer ones, and I've writ­ten much longer ones my­self. Six­ty char­ac­ters left for code means that if you're call­ing an in­stance method (so this. pre­fix is manda­to­ry) with just two pa­ra­me­ters, you are near­ly sure you will hit the right edge of the page.

Not count­ing the fact that you sim­ply can't en­force such lim­it on code such as HTML: deeply nest­ed el­e­ments will quick­ly reach the lim­it, and not nest­ing them would cre­ate ad­di­tion­al com­plex­i­ties at CSS lev­el. Vi­su­al Stu­dio's ASP.NET ed­i­tor at­tempt­ed to en­force such lim­it. Re­sult: ugly for­mat­ting and ex­cep­tion­al cas­es to han­dle.

To­day, the same 23 inch screens rarely dis­play a small logo in their left bot­tom cor­ner. More of­ten, they show dark vi­o­let ter­mi­nals, two and a half per screen. To­day, if I write a line longer than 80 char­ac­ters, the pre-com­mit hook gen­tly re­jects my com­mit: "this is not PEP-8 com­pli­ant"; "this vi­o­lates js­lint rules."

What hap­pened? Sim­ply Ubun­tu 14.04 re­placed Win­dows 8.1, vim re­placed Vi­su­al Stu­dio and Python and Node.js re­placed C#.

But it was also an op­por­tu­ni­ty to dis­cov­er some­thing new: that the 80 columns lim­it ac­tu­al­ly mat­ters.

First, the as­ser­tion I made a year ago is based on my nar­row view of pro­gram­ming through the prism of C# and Vi­su­al Stu­dio. C# tends to off­set quick­ly; as I said, take an or­di­nary code: a name­space, a class, a method and a log­i­cal block, you've eat­en 16 columns just for that.

Python or Node.js don't off­set with the same speed. In Node.js, one may start by hav­ing a func­tion with­in a call­back of an­oth­er func­tion which is called in­side a collection.forEach() with­in a func­tion, but soon­er or lat­er, this code is refac­tored into well-sep­a­rat­ed and much less nest­ed func­tions. In Python, you rarely find your­self nest­ing too much, be­cause you sim­ply keep things as sim­ple as pos­si­ble. Name­space-class-method is the de­fault in C#. In Python, the de­fault is a def at the root, un­less you re­al­ly do need class­es.

Sec­ond, the 80 columns lim­it has its ben­e­fits. I first dis­cov­ered that when writ­ing Node.js code. Be­ing forced to make lines short­er helped me to find iden­ti­fiers which were too long and could eas­i­ly be short­ened with­out de­creas­ing the read­abil­i­ty. Ac­tu­al­ly, the read­abil­i­ty only in­creased. By re­mov­ing deep nest­ing, by sim­pli­fy­ing com­pli­cat­ed log­ic, I no­ticed that in­deed, the lim­i­ta­tion I hat­ed so much a year ago is ac­tu­al­ly help­ing me im­prov­ing my code.

Would I ap­ply the same lim­it to my C# code? I hard­ly doubt it: giv­en the pace at which C# code in­dents, it would be too risky. But it would still be an in­ter­est­ing ex­pe­ri­ence to do in or­der to know how does it feel to have this lim­it in C#. I'm also pret­ty sure that hav­ing such lim­i­ta­tions can only be ben­e­fi­cial for the be­gin­ners who need to learn that deep nest­ing, long iden­ti­fiers and com­pli­cat­ed log­ic is some­thing which should be avoid­ed.