RFC: Space or not to space

We don’t have a rigid, machine-enforced formatting policy* for the stdlib, so its coding style is not set in stone — it slowly evolves to follow the changing preferences of all the fickle individuals working on it.

Coding style is mostly a matter of taste, so while it’s fun to argue about it, it is usually not very productive. It is also human nature to consider whatever arbitrary style one’s currently using as the self-evidently optimal choice.

Keeping all that firmly in mind, I believe it is obvious that having no space before the colon is the only correct option. If you forced me to rationalize it, I could come up with arguments like these:

  • Apple’s official developer documentation (books, guides, API docs, WWDC material) do not insert a space before the colon. The code samples in these documents define the closest thing we have for an official Swift coding style. Most third-party documentation and many, many projects follow it. I expect this is the style what most people see when they first learn Swift, so major deviations from it tend to stick out.

  • The most prominent feature of the stdlib’s own house style is the strict 80-character line length limit. This is amazingly useful in practice, but it is also a great stress test for edge cases in our line breaking conventions. With such a tight line limit, any non-essential horizontal whitespace feels wasteful. That extra space before the colon used to sometimes force a line break — causing such terrible, terrible anguish; an almost physical pain!

  • The extra space opens a huge gap in the middle of constraint clauses that visually cuts the declaration in half at a weird point.

  • It seems typographically inconsistent to add an extra space before the colon in struct Foo<Element : Hashable>, but not in let foo: Foo or foo.index(forKey: 42) or [“foo”: 42, “bar”: 13]. (There are clear differences in meaning, but it seems just as easy to recognize the various contexts without arbitrarily distinguishing some of them with leading spaces.)

  • <Some even more infuriating nonsense about how the colon is aesthetically designed to be followed by a space, but not preceded by one.>

Then again, I still sometimes catch myself typing the extra space, and it looks okay. We did not go and update existing code unless we had to modify it anyway - so the stdlib still uses the original spacing in lots of places. It’s fine.

* I think it would be valuable to define a single official Swift coding style, for use as a sort of normal form for Swift programs. I really like the stdlib style, but it’s probably too idiosyncratic to serve as such — we need something that feels a bit more familiar to most Swift programmers.

13 Likes