reasonml-q-a icon indicating copy to clipboard operation
reasonml-q-a copied to clipboard

Are there community recommendations for bsrefmt statement length?

Open rastreus opened this issue 4 years ago • 4 comments

Question

Section "31.5 Laying Out Individual Statements" of Code Complete 2 by Steve McConnell (Wikipedia), refers to Statement Length with the following guidance:

Statement Length

A common and somewhat outdated rule is to limit statement line length to 80 characters. Here are the reasons:

  • Lines longer than 80 characters are hard to read.
  • The 80-character limitation discourages deep nesting.
  • Lines longer than 80 characters often won't fit on 8.5" x 11" paper, especially when code is printed "2 up" (2 pages of code to each physical printout page).

With larger screens, narrow typefaces, and landscape mode, the 80-character limit appears increasingly arbitrary. A single 90-character-long line is usually mode readable than one that has been broken in two just to avoid spilling over the 80th column. With modern technology, it's probably all right to exceed 80 columns occasionally.

I enjoy bsrefmt, but the default option of 80 characters typically makes my ReasonML (ReasonReact) code less readable than if I hadn't used it.

Is the default value of 80 characters commonly used across the community, or are most developers setting it to more appropriate lengths? If so, what is being used? 90 cols, 100 cols, 110 cols, 120 cols?

Answer

This question hasn't been answered yet.

Further reading

rastreus avatar Feb 25 '20 20:02 rastreus

80 characters is the de facto default. That's what the reason-vscode plugin ships with, so the vast majority of Reason out there is at 80 characters. The problem with changing this default is that, anytime people want to collaborate on a project, their different lengths will cause all the files they edit to reflow, and result in lots of spurious diffs. This should be prevented.

Another reason that wasn't mentioned above, is to enable trivial side-by-side file arrangement and diffing. One common workflow with Reason code is editing an interface file and its implementation file together. If you're able to line them up side-by-side, it's really convenient.

yawaramin avatar Feb 25 '20 22:02 yawaramin

We use 120 characters and therefore have the following keys set in <project-root>/.vscode/settings.json:

{
  "reason_language_server.format_width": 120,
  "editor.rulers": [120]
}

Also we have a package.json script to reformat the whole codebase:

"refmt": "bsrefmt --in-place -w 120 `find src -name \"*.re\" -o -name \"*.rei\"`"

This script also gets called by CI to check whether formatting changes, so the codebase is kept clean (it could also be a pre-commit hook).

So this way you could enforce a certain line length even in open source projects.

fhammerschmidt avatar Mar 11 '20 13:03 fhammerschmidt

@fhammerschmidt Have you all had any issues or regrets in using 120 rather than the default 80? I would think that 120 characters would be more readable, but @yawaramin made a good point that it might limit usability with side-by-side arrangements. In the end, I guess all settings will have their pros and cons.

rastreus avatar Mar 11 '20 17:03 rastreus

Not really. The only thing what bothered me a bit was just recently when a coworker shared his 13" screen with me and the side-by-side view in VSCode hid parts of the code. I advised him to zoom out a bit and everything was readable again.

Could be that your sweet spot is 100, but for us it was mostly because we were used to it thanks to Prettier and we sticked to it from the first day using Reason.

fhammerschmidt avatar Mar 11 '20 17:03 fhammerschmidt