Pitch: an Official Style Guide and Formatter for Swift

Should there be an official style? Yes.
Should there be an official formatting tool that enforces this style? Maybe not? I foresee a few problems with this.

Formatting Swift is hard (I have some experience with this), not because of the complexity of the syntax, which we already have SourceKit and LibSyntax to help with anyway, but because it's not clear where the role of the formatter should end.

We can (mostly) agree on basic stuff like whitespace between tokens, brace positions and removing semicolons. But there are plenty of things are much harder to nail down in a way that will suit the entire community. Some examples:

  • 2 or 4 space indenting
  • line width (80, 100 or 120 chars)
  • trailing commas in collection literals
  • use of explicit self for members
  • force unwrap versus guard and assert
  • for … in vs forEach

These belong in style guides, but perhaps not in the official style guide.

There are also things that should go in the official style guide and wouldn't be highly controversial, but which would be impossible to autocorrect (at least with a LibSyntax-based formatter; SourceKit based tools like SwiftLint could do it) e.g.

  • enum cases should start with lowercase names
  • types should start with uppercase
  • booleans vara should be named "isFoo" or "areFoo", etc.

Both of these categories of style rule are currently tackled by SwiftFormat and SwiftLint, but that only works because they are highly customizable, so users can disable rules if they don't like them or find that they cause API-breaking changes.

I don't think it's possible to implement an official zero-config gofmt or Prettier-style "complete" formatter for Swift that can implement everything that should be in the style guide without a significant segment of the Swift developer community refusing to use it.

If you make the rules configurable you then have a massively larger test surface and a potentially unlimited amount of extra work adding support for every possible style in use by Swift developers today.

Also - as has been pointed out - there already exist some good community tools for formatting Swift code that can easily be customized to implement whatever style rules the official guide might contain, as well as any deviations or extensions that specific teams want to add.

These tools are unlikely to ever be candidates to become "official", but relatively little is gained by starting a new formatter project that will in all likelihood never catch up to these tools, which have a 3-4 year head start.

I also fear a problem similar to that of Swift Package Manager, where Apple putting its name to an official (but low-priority, minimum-viable) tool will convince everyone that the existing, mature and feature-rich community tools are effectively dead and no longer worth supporting.

Instead of building a new formatting tool, I would prefer to see Apple adding better hooks into Xcode to support 3rd party formatting and linting tools seamlessly, and the energy of the Swift open source community directed towards improving the open source tools we already have.

19 Likes