DEV Community

Cover image for Maximum line length in your code
Evaldas Buinauskas
Evaldas Buinauskas

Posted on

Maximum line length in your code

Until now I've always tried to stick to 80 characters per line and it worked just fine in plain ol' JavaScript, however after adopting TypeScript it feels like it's not enough with all these type declarations, sometimes code becomes harder to read, an example:

export const setCommunity: ActionCreator<SetCommunityAction> = (
  id: string
) => ({
  type: '@@community/SET_COMMUNITY',
  payload: {
    id
  }
});
Enter fullscreen mode Exit fullscreen mode

Whereas if limit would be 100, this becomes more readable:

export const setCommunity: ActionCreator<SetCommunityAction> = (id: string) => ({
  type: '@@community/SET_COMMUNITY',
  payload: {
    id
  }
});
Enter fullscreen mode Exit fullscreen mode

Of course this is a single example that was frustrating me.

What max line length do you use and what are the reasons behind it?

Oldest comments (51)

Collapse
 
johnkennedy9147 profile image
John Kennedy

Why stick to an arbitrary max length at all?
Make lines as long as is natural for the code without having to horizontally scroll.

Collapse
 
buinauskas profile image
Evaldas Buinauskas

This usually comes from tools like GitHub or VSTS. These will enforce horizontal code scrolling on code comparison if lines are longer than {x} characters.

Collapse
 
monknomo profile image
Gunnar Gissel

I like max lengths because I usually work in an IDE with other devs. A shared max length means we can all format our code on save, and it doesn't mess each other up

Collapse
 
stenpittet profile image
Sten

In my case it comes in handy when I have 2 files opened side-by-side. I switched to 120 from 80 and it works pretty well for me.

Collapse
 
khophi profile image
KhoPhi

Now that's the principle.

I think there shouldn't be any definite edged-in-stone approach to maxlengths.

In my case, I simply turn on the text wrap in whatever IDE, and it fits what's right in the viewport.

Collapse
 
klaushauschild1984 profile image
Klaus Hauschild

120
Mainly I am doing Java and a little HTML and CSS.

Just use the horizontal space provided by modern widescreens of huge size :-)

Collapse
 
d4be4st profile image
štef

We had a discussion and settled on 100.

Collapse
 
hdennen profile image
Harry Dennen

We all use the same size main monitors and same IDE with a standardized settings file. To get max line length we took the number of characters that can fit on a line without horizontal scrolling and took a bit off for indentation.

Collapse
 
dmfay profile image
Dian Fay

I go with 100, but still try to fit comments into 80 because I find those are more difficult to read when they break the line or scroll.

Collapse
 
palquimista profile image
Paulo Jesus

120 that's what we use in our company, and I think it fits most of the modern screen sizes

Collapse
 
buinauskas profile image
Evaldas Buinauskas

I wouldn't say that I'm sticking to it religiously, just really wanted to know whether that was bugging just me or not and what others in community are using.

Collapse
 
albinotonnina profile image
Albino Tonnina

I get my favourite readability with 80 :)

Collapse
 
rhymes profile image
rhymes

80

Collapse
 
donis profile image
donis

It's a nice way to have a constraint in your code not to go deep in callbacks or nested statements, not only long variable or method names. These days with wide screen monitors my preference is to have 120 as a soft limit, nicely fits my code and surrounding tools from IDE in one screen. Also fits in GitHub pull request side by side view. It's all about readability, as long as it is not hardly enforced without a valid reason.

Collapse
 
bosepchuk profile image
Blaine Osepchuk

We find than 120 is the most readable while still short enough for all the ways our code is displayed (screen, diffs, Bitbucket, etc.).

80 chars gets hard to read when you have longer, more descriptive names like we use in our code.