willowtreeapps / swift-style-guide

The official Swift style guide for WillowTree, Inc.
MIT License
16 stars 3 forks source link

Location of variable binding introducers (let/var) for enumerations #30

Closed mattyohe closed 7 years ago

mattyohe commented 8 years ago

Given:

enum Foo {
  case bar(Bool)
  case baz(Bool)
}

Single binding:

A.

case let .bar(qux)

B.

case .bar(let qux)

Multiple binding: C.

case let (.bar(qux), .baz(quux))

D.

case (.bar(let qux), .baz(let quux))

Mixed multiple binding: E.

case (.bar(let qux), .baz(var quux))
mattyohe commented 8 years ago

I'm partial to A. until I am forced in a mixed case to use E.

aenewton commented 8 years ago

I prefer B. Consider something like this:

enum Foo {
    case bar
    case baz(Bool)
}

Using let only makes sense in the context of cases that actually have associated values, so it feels more accurate to move that into the case itself.

case .baz(let qui)
mattyohe commented 8 years ago

@hpwooten Ah, so you point to cases I didn't consider.

enum Foo {
  case bar
  case baz(Bool)
  case fred(Bool)
}

F.

case (.bar, .baz(let quux))

G.

case let (.bar, .baz(quux))

H.

case let (.bar, .baz(quux), .fred(corge))

I.

case (.bar, .baz(let quux), .fred(let corge))

J.

case (.bar, .baz(let quux), .fred(var corge))

Consistent with my previous comment, I'd go with G or H until the mixed binding case where I'd use J

mattyohe commented 8 years ago

After some in-person conversations, and some folks on Slack, I'm now being convinced against my initial thoughts. That is, prefer B, D, E, F, I, J.

Reason being, I'm really more looking for a simple rule to follow when this comes up and just simply stating, "place the introducer next to the variable being bound" is much simpler than trying to describe any other method.