Swift Naming Conventions
In this article, we'll go over Swift naming conventions.
In programming, naming conventions are the rules you follow when giving names to variables, functions, classes, and so on.
Even if you don't follow these rules, your code will usually still work. However, it's generally a good idea to stick to the conventions recommended for each language whenever possible.
Common Terms Used in Naming Conventions
Let's first look at some common terms you'll often see when talking about naming conventions in programming.
Pascal Case (Upper Camel Case)
When joining words, the first letter of each word is capitalized. Example: GetMethodName
Camel Case (Lower Camel Case)
The first letter of the first word is lowercase, and the first letter of each following word is capitalized. Example: getMethodName
Snake Case
All lowercase letters, with words separated by underscores _. Example: get_method_name
Kebab Case
All lowercase letters, with words separated by hyphens -. Example: get-method-name
Pascal Case (Upper Camel Case) and Camel Case (Lower Camel Case) are called “camel case” because the capital letters in each word look like a camel's humps.
Kebab case looks like words are skewered on a stick, while snake case resembles a snake slithering across the ground.
Naming Conventions in Swift
The following naming conventions are recommended in Swift:
Item | Convention | Example |
---|---|---|
Variables | Camel Case | variableName |
Constants | Camel Case | constantName |
Functions | Camel Case | functionName |
Function Parameters | Camel Case | functionParameterName |
Enums | Pascal Case | EnumName |
Structs | Pascal Case | StructName |
Classes | Pascal Case | ClassName |
Class Variables | Camel Case | classVariableName |
Methods | Camel Case | methodName |
Protocols | Pascal Case | ProtocolName |
That's an overview of Swift naming conventions.