Names should be in American English.
- Swift.org’s naming guidelines: Swift.org - API Design Guidelines § Naming
If this is difficult for you to understand at the call-site, then you may have chosen the wrong words. If all else fails, simply use Quick Help (by ⌥-clicking the symbol or by selecting it and using the Quick Help inspector) and the resulting documentation will describe its declaration for you.
This one is simple. Types (
enums, etc.) are always CapitalizedCamelCase. Instances (
cases, etc.) and functions (
vars, non-anonymous closures, etc.) are always uncapitalizedCamelCase.
When an acronym or initialism like HTTP or JSON is a part of a name, treat it like an English word rather than an abbreviation, even if it’s not pronounceable like a word.
Do not use Systems Hungarian notation!
Knowing the type, implementation, etc. in something’s name is not important. If, however, you find that such information is necessary, you may append it as full words to the end of a name, like “
widthInt”, or “
Do not use Apps Hungarian notation!
Knowing the purpose of something in its name is very important. However, you should prepend it as full words (not short letter abbreviations), like “
unsanitizedUserInput”, or “
Names should be descriptive and self-documenting (however, that does not mean you should forego documentation). The name of a function, for instance, should describe everything that function does. If that name seems too unwieldy, it’s probably because that function does too much. In that case, split it up into multiple functions, each also with descriptive names.
If your instance, function, or type name is only a few characters long, it probably needs a longer name.
Here are some good rules from Swift.org’s design guidelines:
RULE-OF-THUMB Clarity at the point of use is your most important goal. Entities such as methods and properties are declared only once but used repeatedly. Design APIs to make those uses clear and concise. When evaluating a design, reading a declaration is seldom sufficient; always examine a use case to make sure it looks clear in context.
RULE-OF-THUMB Clarity is more important than brevity. Although Swift code can be compact, it is a non-goal to enable the smallest possible code with the fewest characters. Brevity in Swift code, where it occurs, is a side-effect of the strong type system and features that naturally reduce boilerplate.
RULE-OF-THUMB Write a documentation comment for every declaration. Insights gained by writing documentation can have a profound impact on your design, so don’t put it off.
GOLDEN RULE If you are having trouble describing your API’s functionality in simple terms, you may have designed the wrong API.
It’s important to note that this page uses placeholder names like foo, bar, baz, etc. to name things when the name is unimportant to the example. It’s strongly discouraged to use such obscure andor short names in production code!
- Getters are named the same as fields; that is to say they are simply the noun or noun phrase which describes the thing they get. If properties need to be passed in, they are named parameters, not part of the actual function name.
- Boolean (or other very restricted sets of values) getters are named as assertions about something. They might be prefixed with is/does/etc. or be a present-tense verb or verb phrase.
- Setters take the same syntax to getters, but with the word “
set” prefixing them. Note that it’s preferred to use a var rather than a func to get & set a single value. The incoming parameter which holds the new value should not have a label (use
- Non-Setter Mutators are verbs or phrases describing the mutation they perform
- Copiers (functions which otherwise act like mutators, but instead of mutating return a new object with the changed data) are present- or past-participle verbs or verb phrases (ending in “ing”, “ed”, etc.) describing the mutation they perform
Types should not be prefixed; this was a Cocoa pattern used until Swift as a primitive form of namespacing, but since Swift is modular, no prefix is needed.
- Classes and structs have varied purposes, so they are named in a way that reflects their purpose, using nouns or noun phrases.
- Protocols define a contract, so there are two approaches:
- Protocols which define what something is are named exactly like the classes which might implement them; nouns or noun phrases.
- Protocols which define what something can do are named using present-participle verbs or verb phrases (end in
- Enums are named as the singular noun which describes what their cases represent.
- Typealiases are named just like the type for which they are an alias.
- Associated types are named just like typealiases.
Anything that holds an instance/value (stored fields,
cases, etc.) are named as nouns. An exception can be made for Booleans, which are named as asserting verbs or verb phrases like their function counterparts.
Note that fancy
vars may be named like functions.
All instances have the same naming scheme:
Do not use
_underscorePrefixesshould only be used if absolutely necessary.
Only collection types (and instances of those types) should be plural (for instance,
Arrays). Everything else should be singular.