Quick Style survey

I put up a poll asking whether it was good style to move attributes and access modifiers to their own line. Results are here.

My take on things after the poll and after talking directly with a number of developers is this: Placing attributes like @objc, @testable, @available, @discardableResult on their own lines before a member declaration has become a conventional Swift style.

@objc(objectAtIndex:)
internal func objectAt(_ index: Int) -> AnyObject {

@available(*, unavailable, renamed: "Iterator")
public typealias Generator = Iterator

@discardableResult
func _copyContents(initializing ptr:
UnsafeMutablePointer<Iterator.Element>)
-> UnsafeMutablePointer<Iterator.Element>

This approach limits declaration length. It allows a member to float below its attribute and supports flush-left access modifiers, so `internal`, `public`, etc appear in the leftmost column. Many developers mix-and-match styles for short Swift attributes like `@objc`. It is common to see `@objc(countByEnumeratingWithState:objects:count:)` on its own line while `@objc` moves down to the primary declaration, particularly for single-line declarations:

@objc internal var count: Int

@objc internal final class _SwiftDeferredNSArray
: _SwiftNativeNSArrayWithContiguousStorage

Others prefer to break attributes out universally, regardless of length. This approach is consistent, and it promotes consistent leftmost access modifiers:

@objc
internal var count: Int

@objc
internal final class _SwiftDeferredNSArray
: _SwiftNativeNSArrayWithContiguousStorage

Comments are closed.