[Dream] Towards standard source code formatting

I have switched from being anti to being pro-formatting, after starting on a codebase that had machine formatting by default, and realising how liberating it is. Bang in some code anyhow, hit format and move on. No hassles (it works even better if the language uses braces and the like, rather than layout).

And I think the specific style of formatting being used is a red herring. The advantage comes from having a standard style, which can be quickly and easily applied. Whatever the style is does not really matter.

I believe when Elixir introduced a formatter as standard, they asked people to just use it for a year, then they would address feedback. By then everyone was used to it, and saw the benefits, and there wasnā€™t further conflict.

And if it is built into the tooling, different projects can choose different formatters. And I suspect over time a small set would become standard, accepted by people as ā€œthe way code looksā€, and well maintained.

13 Likes

Preface: My experience with auto-formatting is generally horrible (granted in a different language, namely Scala), because Iā€™m the type of developer who tends to refactor a lot of shared code, because I tend to work on shared infrastructure code.

Itā€™s horrible because it leads to absurdly large diffs (and hence git conflicts) because it will tend to re-flow code for no good reason. Now, Haskell is more resilient to this effect when using the whitespace sensitive syntax, but itā€™s not immune due to e.g. aligning ā€˜<-ā€™ preferences (and similar wrt. [ and , placement for lists.)

The thing is: I wouldnā€™t have a problem accepting ā€˜arbitraryā€™ choices as long it wouldnā€™t lead to absurd headaches figuring out what changed when I have a merge conflict.

(This may ultimately be a problem with Git and similar line-based-conflict resolution, but git is the world we live inā€¦ and I happen to value git extremely highly when you want to maintain a high standard of source provenance, etc.)

1 Like

As I think youā€™re alluding to hereā€¦ I donā€™t think thereā€™d be any problem with a ā€œformat:ā€ field in a cabal file specifying which (cabal-compiled) formatter to use. There might be something to figure out about which version (and its dependencies and such), but that should be figure-outable.

I think the point of the ormolou formatting choices is to make them diff-friendly. Hence the extensive use of newlines

As a maintainer of Fourmolu, I have to admit that we (and Ormolu) sometimes split things across lines in ways which can be rather ugly and even make diffs worse by changing the indentation of whole blocks.

5 Likes

Interesting, I also refactor a lot of shared code that is formatted with Ormolu and I donā€™t really experience this as a problem. Iā€™m not sure why that might be. Perhaps itā€™s because Ormolu is better for diffs than the Scala formatter you use. I never get ā€œabsurdly largeā€ diffs with Ormolu (at least not large when viewing with git diff -w. Alternatively, it might be a difference in workflow. I tend to make ā€œabsurdly smallā€ commits which tend to be easier to resolve when conflicts arise. Additionally I have a well-specified approach to resolving rebase conflicts.

FYI it seems that brittany may actually be maintained: tomejaguar comments on Good Haskell code formatters?

Iā€™d want to have a default formatter too :raising_hand_man:
And I think that having a default code formatter would benefit almost everyone.

As a seasoned developer Iā€™d rather be a little unhappy with the style picked for the project but not have take part in any of the discussions/wars around the code formatting.

Having a code formatter right out of the box would also make the whole user experience smoother. So we donā€™t have to install and updated fourmolu separately (or am I getting the idea wrong?). It will also make easier for editors to get auto-formatting enabled by default, because itā€™d be like if you have cabal, you have the formatter too.

PS Iā€™m wondering what Go people think about code formatting since this is something they donā€™t think often about (I am guessing) I am not sure if they even have many options for how their code gets formatted :slight_smile:

1 Like

Just to throw in my two cents: yes, default formatter would be very much appreciated.

Weā€™re using fourmolu and I feel like the newlines can be excessive, but on the whole the ā€œkeeping diffs to a minimumā€ aspect is what makes me like it the most.

line-based diffing ā†’ diff-friendly formatting ā†’ tooling that optimizes for the wrong end of a stick

Iā€™m so unhappy that the ā€œdefault formattingā€ is so hostile to me and creeps up into my life like a steamroller from hell.

4 Likes

This is a phenomenon that extends beyond formatters, I find.

I havenā€™t really found a quippy term for it, but itā€™s basically when people allow the limitations of their tools drive their technical decision making.

Itā€™s the same thinking as, say, avoiding advanced types because they take longer to compile. Or always putting :: on the same line as a definition so itā€™s more greppable.

Not that either of those decisions are inherently bad or anything. But they also arenā€™t objective improvements despite sounding scientific and data-driven.

Is it just that the alternativeā€™s benefits are less quantifiable? ā€œYou optimize what you measureā€?

5 Likes

It used to be called ā€œputting the cart before the horseā€.

2 Likes

haha I think this is more like if you put the horse in the cart, handed it the reins, and dragged then cart yourself.

3 Likes

I would love to see diffs being done with something like difftastic which does a syntax-tree comparison, so layout doesnā€™t matter.

6 Likes

Yeah, Iā€™ve switched to difftastic too. Certainly a step in a right direction.

3 Likes