Names should be written with their most general part first and their most specific part last
// WRONG
let rightTitleMargin: CGFloat
let leftTitleMargin: CGFloat
let bodyRightMargin: CGFloat
let bodyLeftMargin: CGFloat
// RIGHT
let titleMarginRight: CGFloat
let titleMarginLeft: CGFloat
let bodyMarginRight: CGFloat
let bodyMarginLeft: CGFloat
Event-handling functions should be named like past-tense sentences.
// WRONG
class ExperiencesViewController {
private func handleBookButtonTap() {
// ...
}
private func modelChanged() {
// ...
}
}
// RIGHT
class ExperiencesViewController {
private func didTapBookButton() {
// ...
}
private func modelDidChange() {
// ...
}
}
Don't use self unless it's necessary for disambiguation or required by the language.
final class Listing {
init(capacity: Int, allowsPets: Bool) {
// WRONG
self.capacity = capacity
self.isFamilyFriendly = !allowsPets // `self.` not required here
// RIGHT
self.capacity = capacity
isFamilyFriendly = !allowsPets
}
private let isFamilyFriendly: Bool
private var capacity: Int
private func increaseCapacity(by amount: Int) {
// WRONG
self.capacity += amount
// RIGHT
capacity += amount
// WRONG
self.save()
// RIGHT
save()
}
}
Add a trailing comma on the last element of a multi-line array.
// WRONG
let rowContent = [
listingUrgencyDatesRowContent(),
listingUrgencyBookedRowContent(),
listingUrgencyBookedShortRowContent()
]
// RIGHT
let rowContent = [
listingUrgencyDatesRowContent(),
listingUrgencyBookedRowContent(),
listingUrgencyBookedShortRowContent(),
]
Name members of tuples for extra clarity. Rule of thumb: if you've got more than 3 fields, you should probably be using a struct.
// WRONG
func whatever() -> (Int, Int) {
return (4, 4)
}
let thing = whatever()
print(thing.0)
// RIGHT
func whatever() -> (x: Int, y: Int) {
return (x: 4, y: 4)
}
들여쓰기는 4줄로 유지
한 줄의 최대 column width는 120
자
guard / if 문에서 else에 return만 있을 경우 한 줄로 작성
guard / if 문에서 조건식이 최대 column width를 초과하는 경우에만 leading keyword에 맞게 정렬함
// RIGHT
guard
let earth = unvierse.find(
.planet,
named: "Earth"),
earth.isHabitable
else { … }
Mark주석의 경우 // MARK: - Namespace
형태로 작성함. 위아래 줄바꿈은 따로 하지 않음
주석 최소화를 위해 문서화 주석 (///
)은 사용하지 않음
self는 이니셜라이저 외에는 반드시 사용해야 할 경우만 명시적으로 작성
프로퍼티 초기화 시 return은 줄바꿈하지 않음 (함수에서는 줄바꿈함)
여러 줄의 표현식 뒤에 중괄호를 사용하는 경우 중괄호를 줄바꿈하지 않음
라이브러리는 최소화