'Swift: Global constant naming convention?
In Swift, it seems that global constants should be camelCase.
For example:
let maximumNumberOfLoginAttempts = 10
Is that correct?
I'm used to all caps, e.g., MAXIMUM_NUMBER_OF_LOGIN_ATTEMPTS
, from C, but I want to acquiesce to Swift conventions.
Solution 1:[1]
Swift 3 API guidelines state that "Names of types and protocols are UpperCamelCase. Everything else is lowerCamelCase."
https://swift.org/documentation/api-design-guidelines/
Ideally your global constants will be located within an enum
, extension
, or struct
of some sort, which would be UpperCamelCase, and all properties in that space would be lowerCamelCase.
struct LoginConstants {
static let maxAttempts = 10
}
And accessed like so,
if attempts > LoginConstants.maxAttempts { ...}
(credit to Ryan Maloney's answer for calling out the benefits of enum
)
Solution 2:[2]
I've been debating using camel case with a leading capital for class-level constants. For example:
static let MaximumNumberOfLoginAttempts = 10
It's still camel-case (as Apple appears to recommend), but the capitalized leading character makes it clear that the value is immutable.
Solution 3:[3]
To improve on bmjohns answer, it's better to use an enum
instead of a struct
to act as a namespace for your constants. An enum
with no cases can't be instantiated, whereas a struct
can. If it is a struct
, then instantiating it (via LoginConstants()
) is allowed, but that has no meaning and doesn't make sense to do.
The convention is to use enumerations for namespacing, as such:
enum LoginConstants {
static let maxAttempts = 10
}
This ensures that the only valid usage of LoginConstants
is for accessing its static members.
Solution 4:[4]
Apple advocates the camelCase. That said, many use _camelCase just to differentiate it especially if you are likely to have the same name at a lower scope.
Solution 5:[5]
I commonly see constants declared with a k
, like so:
static let kLoginAttemptsMax = value
This also follows camel casing to a "T".
Solution 6:[6]
There are a couple of options.
- Use Apple's naming convention as
thisIsMyConstant
.- Upside: it's promoted by Apple, so it's a "standard" thing.
- Downside: no way to tell something is a constant or a variable without
Option+Click
. - Side note: Apple is not always right. A lot of people were using
UIColor.myColor
instead ofUIColor.MyColor()
way before Apple made the change, including me.
- Use
Objective-C
stylekThisIsMyConstant
. As a lot ofSwift
developers areobjc
experts, it should be pretty obvious for most Mac/iOS developers. - Use
C
styleTHIS_IS_MY_CONSTANT
. It's also used by a lot of other languages likeJava
. - Use something like
ThisIsMyConstant
. I can't think of any languages using this style for now (there should be some but I just can't recall), but it's kind of close to Apple's suggestion.
Edit: it also depends on your linting/autoformatting tool(s). More thoughts were added as comments.
Solution 7:[7]
Apple shows us constants with camelCase.
I use the bether readable variant. So for your example:
let maximumNumberOfLoginAttempts = 10
let MAXIMUM_NUMBER_OF_LOGIN_ATTEMPTS = 10
'MAXIMUM_NUMBER_OF_LOGIN_ATTEMPTS' ist bether readable for me and it shows instantly, that it's a constant var.
Solution 8:[8]
You can see not just what Apple says, but what they do.
You can have a look at Foundation class, that is a class created by apple and it is imported by default in the ViewController file when you create a new project.
Press command+option+click over the class to see its declaration.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ryan M |
Solution 2 | Greg Brown |
Solution 3 | Ryan M |
Solution 4 | Tokuriku |
Solution 5 | Hobbyist |
Solution 6 | |
Solution 7 | Javatar |
Solution 8 | Ryan M |