Naming
Last modified on Wed 18 Oct 2023
Basic:
- Use descriptive names
- Lower camel case for classes, methods, variables, etc.
- Class names should be capitalized
- Method names and variables should start with a lowercase letter
- Use
_
for private methods and variables (open for discussion)
Preferred:
let maximumWidgetCount = 100
class WidgetContainer {
var widgetButton: UIButton
let widgetHeightPercentage = 0.85
}
Not preferred:
let MAX_WIDGET_COUNT = 100
class app_widgetContainer {
var wBut: UIButton
let wHeightPct = 0.85
}
Functions
Since Swift 3, all function parameters have labels unless you request otherwise by using underscore.
Prefer method and function names that make use sites form grammatical English phrases.
Preferred:
x.insert(y, at: z) “x, insert y at z” x.subViews(havingColor: y) “x's subviews having color y” x.capitalizingNouns() “x, capitalizing nouns”
Not preferred:
x.insert(y, position: z) x.subViews(color: y) x.nounCapitalize()
Include all the words necessary to avoid ambiguity for a person reading code where the name is used.
Preferred:
func remove(at position: Index) -> Element employees.remove(at: x)
Not preferred:
func remove(_ position: Index) -> Element employees.remove(x) // unclear: are we removing x?
When the first argument forms a part of a prepositional phrase, give it an argument label. The argument label should begin at the preposition.
Preferred:
func numberOfSections(in tableView: UITableView) -> Int func viewForZooming(in scrollView: UIScrollView) -> UIView? names.index(of: "Taylor") color.fadeFrom(red: b, green: c, blue: d) // Begin the argument label after the preposition, to keep the abstraction clear.
Not preferred:
func numberOfSectionsInTableView(tableView: UITableView) -> Int func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? names.indexOf("Taylor") color.fade(fromRed: b, green: c, blue: d) // The first two arguments represent parts of a single abstraction
Please visit Swift API Design Guidelines for more info and examples on function naming—they provide a lot of examples and explanations on edge cases.
Enumerations
- Each enumeration definition defines a brand new type. So, like other types in Swift, their names should start with a capital letter.
- Use lower camelCase for enumeration values.
Preferred:
enum CompassPoint {
case north
case south
case east
case west
}
Not preferred:
enum compassPoint {
case North
case South
case East
case West
}
Prose
- When referring to functions in prose (tutorials, books and comments) include the required parameter names from the caller's perspective or
_
for unnamed parameters. - When in doubt, check how Xcode lists the method in the jump bar
(CTRL + 6)
.
Example—Prose
Call convertPointAt(column:row:) from your own init implementation.
If you call date(from:), make sure that you provide a string with the "yyyy-MM-dd" format.
You should not call the data source method tableView(_:cellForRowAt:) directly.
Typealias
- Use
typealias
to make parameters meaningful.
Example—Typealias:
typealias MimeType: String
func fileURL(with mimeType: MimeType, data: Data) -> URL
Class prefixes
- Swift types are automatically namespaced by the module that contains them, and you should not add a class prefix. If two names from different modules collide, you can disambiguate by prefixing the type name with the module name.
Example:
import SomeModule
let myClass = MyModule.UsefulClass()
This guide is mostly copied from Swift API Design Guidelines, Ray Wenderlich Swift guide, and What's new in Swift 3.0, with some minor changes.