Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 511
ma11hew28 Avatar asked Jun 16 '14 12:06

ma11hew28


People also ask

How do you name a constant in Swift?

In Swift, constants are declared using the let keyword. In a similar fashion to variables, the let keyword is followed by the name of the constant you want to declare and then a type annotation (a colon, a space and then the type of data you want to store in the constant).

What is the convention for naming a constant?

Constants should be written in uppercase characters separated by underscores. Constant names may also contain digits if appropriate, but not as the first character.

What is constant in Java naming convention?

The names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores ("_"). (ANSI constants should be avoided, for ease of debugging.)


1 Answers

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 a struct of some sort, which would be UpperCamelCase, and all properties in that struct would be lowerCamelCase

struct LoginConstants {     static let maxAttempts = 10 } 

And accessed like so,

if attempts > LoginConstants.maxAttempts { ...} 
like image 50
bmjohns Avatar answered Sep 22 '22 15:09

bmjohns