WhatsApp / erlfmt

An automated code formatter for Erlang
Apache License 2.0
407 stars 51 forks source link

Settle default line width #17

Closed michalmuskala closed 4 years ago

michalmuskala commented 4 years ago

Today we use 92 as default line width, which is largely arbitrary.

We should experiment to see what impact line width has on the final size of real programs (in terms of LoC). The experiment would format large codebases and plot the total LoC in relation to the formatter's maximum line width. Having that data, we'll be able to make a more informed decision.

richcarl commented 4 years ago

Please please don't default to over 80 chars. Any time you try to quickly look at the file in a terminal, you'll have line wraps all over the place making it unreadable. 76 is good, leaving a small margin in most cases and allowing slightly overfull lines when needed without going over 80.

eproxus commented 4 years ago

Shorter line lengths leads to better reading comprehension, and shorter functions. It is also better for people with bad eyesight who tend to use larger fonts.

My vote goes for 80 as well.

alanz commented 4 years ago

I see the linux kernel is moving to 100 chars.

https://linux.slashdot.org/story/20/05/31/211211/linus-torvalds-argues-against-80-column-line-length-coding-style-as-linux-kernel-deprecates-it

eproxus commented 4 years ago

@alanz It's more nuanced than that:

Yes, staying withing 80 columns is certainly still preferred," notes the official commit message for this change. "But it's not the hard limit that the checkpatch warnings imply, and other concerns can most certainly dominate. Increase the default limit to 100 characters. Not because 100 characters is some hard limit either, but that's certainly a 'what are you doing' kind of value and less likely to be about the occasional slightly longer lines.'"

And they still use 8 characters tab width if I remember correctly, which might explain it. And it's C code, Erlang tends to be more succinct.

michalmuskala commented 4 years ago

Indenting Erlang with 4 spaces is very similar to indenting C with 8 when it comes to control structures. For example in C the body of an if is indented just once, while in Erlang the body of case, receive, etc is usually indented twice (once for pattern, once for body). With just 2 levels of nested case we're talking about an indentation of 20 for the body. The code migrates really fast to the right.

eproxus commented 4 years ago

@michalmuskala I never have more than two nested case statements, preferable never more than 1 (and most beautiful is 0, and use function head pattern matching instead). 😄

awalterschulze commented 4 years ago

The width is now configurable using the --print-width flag. Here is a document explaining our formatting decision https://github.com/WhatsApp/erlfmt/pull/106 on the default width of 100 And here is a document explaining formatting decisions in general: https://github.com/WhatsApp/erlfmt/blob/master/doc/Readme.md and how these documents are still open to feedback.