-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Constants Grouping #12
Comments
I'm not really convinced using either Just to make it clear: not using / discouraging the use of any of these is an option as well. |
I somehow like grouping constants sometimes, but I confess that I'm not sure why. A recent example: private enum Animation {
static let shortDuration = 0.1
static let longDuration = 0.4
static let visibleAlpha = CGFloat(1.0)
static let hiddenAlhpa = CGFloat(0.0)
} I'm not sure how That said, if we do group them I like the idea of more descriptive names. Which I think also means that if you can't come up with a name better than Addendum 1, don't repeat the group in the constant name: // Wrong: Titles.buttonTitle has two titles
enum Titles {
let buttonTitle = "Best Button"
let viewTitle = "Amazing!"
}
// Better: Titles.button
enum Titles {
let button = "Best Button"
let view = "Amazing!"
} Addendum 2: keep the group names singular ( Although the more I look at the examples, the more I think I would not group those in particular. |
Also noting that the most common usage I've found for this are dictionary keys. I've played with this in the past and my ideal solution was something that wasn't possible until Swift 4 introduced generic subscripts. I'd make dictionary keys an actual My playground is freezing with this test code, but here's the main idea: // Existing
enum KeysA {
static let monitorEnabled = "monitor"
static let blockMaliciousLoginAttempts = "protect"
// ...
}
// Proposed
enum KeysB: String, DictionaryKey {
case monitorEnabled = "monitor"
case blockMaliciousLoginAttempts = "protect"
// ...
}
// The Dictionary subscript can take any RawRepresentable whose RawValue matches
// the Key type. Which in this case means it will take any `enum X: String` for
// a `[String: Y]` Dictionary
extension Dictionary {
subscript<T: RawRepresentable>(key: T) -> Value where T.RawValue == Key.Type {
get {
return self[key.rawValue]
}
set {
self[key.rawValue] = newValue
}
}
}
var dictionary = [String: String]()
dictionary[KeysA.monitorEnabled] = "true"
dictionary[KeysB.monitorEnabled] = "false" |
That last example is something I’ve been expecting / wishing would work right out of the box in Swift. To be honest I believe it could even be a good candidate for a new official Swift proposal. In terms of this proposal, though I feel like it goes a bit beyond the scope. But I would try shaping it into a new one. |
It's definitely beyond scope, but I brought it up just because if we got rid of the |
Here's an example of why I think more specific naming is important. |
I like this a lot. Can you clarify why structs |
When grouping constants use
enum
s, and select descriptive names.Examples
Rationale
Why
enum
s and notstruct
s?The reason why we should avoid
struct
s is simply because they are going to show up more frequently in autocompletion suggestions (both when declaring a variable's type and assigning it a value).Why descriptive names?
Descriptive names make groups more focused and, well, descriptive.
Naming the group just
Constants
would promote storing completely unrelated values inside of it, making it not too generic:Whereas more descriptive naming would encourage more atomic and logical groups:
Enforcing the rule
Manual peer reviews, but we can try to spot the most common mistakes through Swift lint rules.
The text was updated successfully, but these errors were encountered: