Thursday, January 7, 2016 [Tweets] [Favorites]

Swift Bracing

Erica Sadun:

It’s a shame then that the language has adopted 1TBS over Allman when the latter exposes scope so beautifully and the former can be quite hard to follow, especially with respect to complex declarations that include external and internal labels, default values, and other complicating features

Around the time Swift was introduced, I switched to 1TBS after about 20 years of Allman. I like the compactness.

The indentation of switch statements is pretty bad, though. At first I thought it was a bug in Xcode’s auto-indentation, but the Swift documentation uses the same style.

8 Comments

Cases are labels, not statements. Thus, conventionally, they are not indented.

”Around the time Swift was introduced, I switched to 1TBS after about 20 years of 1TBS.”

Not what you meant, right?

@Adrian Fixed, thanks.

"Around the time Swift was introduced, I switched to 1TBS after about 20 years of Allman. I like the compactness."

I was using 1TBS before starting writing code in Objective-C. Moved to Allman then. 1TBS is more compact but it is less legible when it's not your own code and also when it's code you wrote a long time ago.

Considering code written in Swift is already not very legible, using 1TBS is not helping.

I switched to 1TBS like five+ years ago when I did my first Android project (that's the Google's convention). I like it better than the eight years of C# using Allman. I have a very wide display, but less vertical real estate, so I am used to it. I don't think it makes a HUGE difference in readability, it's just muscle memory to me.

Swift was my watershed moment too. I still miss Allman

@Martin If you have a very wide display, it makes it easier to spot the { at the end of a line. But in some cases, this also requires to have text-wrapping enabled.

And for instance, it's quite common for lines of code to be wider than GitHub's (or BitBucket's) website source code viewer pane.

Quico Moya:

"Cases are labels, not statements. Thus, conventionally, they are not indented."

In Swift, this is nowhere near true: `case` is not a label (not in the Obj-/C/++ sense of being used with `goto`), it's a structural element of the Switch statement that delineates logical blocks. Therefore, it makes no sense to apply this classical negative indent for that reason.

Stay up-to-date by subscribing to the Comments RSS Feed for this post.

Leave a Comment