74

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.

3
  • 1
    I'm curious of this, too. If there isn't a Swift naming guide from Apple, they need to release it soon. Jun 16, 2014 at 13:03
  • 11
    The reason for the C convention of naming macros (not constants) in all caps was that macros are a particularly dangerous construct and it was very useful to be able to quickly scan code for macros since macro expansion in unforeseen contexts was often the cause of bugs. I'm not sure there's any value to a naming convention to distinguish between constants and variables in a statically typed language with built-in support for immutability.
    – Ferruccio
    Jun 16, 2014 at 15:15
  • @Ferruccio, the convention originates from C, but some later language (Python, Java) just took the same convention, perhaps to ease transition from C. May 18, 2016 at 23:40

7 Answers 7

88

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)

1
  • 13
    the approach with the structs is epic
    – David Seek
    Nov 6, 2016 at 9:12
26

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.

3
  • I like this answer a lot, it still allows you to strictly follow camelCase while showing that it's a constant.
    – Austin
    Aug 19, 2015 at 20:10
  • 1
    I just realized the other day that this approach would also be consistent with Apple's naming convention for enum values (which also use camel case with leading caps).
    – Greg Brown
    Oct 2, 2015 at 17:44
  • 9
    @GregBrown this is no longer the case. Swift 2.2 onwards uses lowerCamelCase.
    – jowie
    Oct 6, 2016 at 14:04
9

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.

3
  • Good point! I amended my answer to include all the most common ways of using global constants, including enums. Thanks for sharing!
    – bmjohns
    Apr 20, 2022 at 14:08
  • @bmjohns I mean, you should at least credit me in your edit, but honestly I think you should remove it, and let our answers compete independently. Just copying my answer into yours kind of goes against the point of the voting and prioritization system. You submitted your answer, and I submitted mine, so I don't think it's right for the top answer to copy all the better ideas from answers with less votes. Apr 21, 2022 at 15:54
  • Didn't mean to steal your thunder here. The true question was the naming convention. Whether they should be all caps, camel case, snake case, ect. Purposing to use enums, extensions or structs is mostly just added gravy IMO which is why I didn't feel like it was stealing. I did add your credit though, my bad for not doing that to begin with.
    – bmjohns
    Apr 21, 2022 at 16:07
5

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.

5

I commonly see constants declared with a k, like so:

static let kLoginAttemptsMax = value

This also follows camel casing to a "T".

4
  • 3
    At least in Obj-C Apple code I see the "k" convention a lot.
    – wcochran
    Feb 27, 2016 at 14:31
  • 8
    This is an old convention, not one Apple are using anymore in Swift.
    – jowie
    Oct 6, 2016 at 14:02
  • 1
    The "k" is used to denote that it's a key to some sort of dictionary. Such as the names of JSON entities. Jan 20, 2017 at 10:26
  • The "k" stands for "constant" as one can read from Steve McConnell's book.
    – kelin
    May 27, 2017 at 20:28
5

There are a couple of options.

  1. 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 of UIColor.MyColor() way before Apple made the change, including me.
  2. Use Objective-C style kThisIsMyConstant. As a lot of Swift developers are objc experts, it should be pretty obvious for most Mac/iOS developers.
  3. Use C style THIS_IS_MY_CONSTANT. It's also used by a lot of other languages like Java.
  4. 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.

2
  • IMO going with Apple's conventions is the right option here; it maximizes consistency.
    – Zorayr
    Jun 3, 2020 at 19:51
  • There are more options other than just let foo = "bar". Like for limited scope, one can use private struct Const; and for shared constants, you can even inject constants as dependencies. And by using these methods, cons of using Apple's standard can be limited. Jun 3, 2020 at 23:05
3

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.

3
  • 19
    More readable: no. Indicates it is a constant: yes. But that does not provide the answer of Apple's best practice for Swift.
    – zaph
    Jun 16, 2014 at 14:21
  • 9
    Apple's best practice is camelCase. Otherwise they would use 'caps-style' in the book (or I haven't seen it yet). But in such non-critical questions I go with my instincs - also I don't have to worry about context switch mistakes, like java <> swift. But that's just my thought to this. Some people may think different..
    – Javatar
    Jun 16, 2014 at 14:41
  • 1
    I'd tend to go with Apple convention first, or with working group's convention on-the-job or contributing to open source. In the latter, I'd probably try to influence towards the Apple convention, since they're setting the standards, though I can understand the suggestion by @Javatar (nice handle, btw).
    – rholmes
    Sep 20, 2015 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.