-
Notifications
You must be signed in to change notification settings - Fork 1
Code Convention
Applecider edited this page May 23, 2022
·
1 revision
-
Styleshare 컨벤션을 따름
-
기타 세부 컨벤션은 Airbnb를 따름
- 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) }
-
잊기 쉬운 것들 (항상 체크 잘하자!)
- 상속하지 않을 클래스는 final 설정
- Nested Type에 private 설정
- 자주 쓰는 UI는 extension으로 메서드를 구현하여 style 일괄 설정 (StackVIew, Label 등)
- 테스트코드
- 들여쓰기는 4줄로 유지
- 한 줄의 최대 column width는 135자
- else는 guard let 문의 마지막 줄에 작성 (줄바꿈 X)
- 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은 줄바꿈하지 않음 (함수에서는 줄바꿈함)
- 여러 줄의 표현식 뒤에 중괄호를 사용하는 경우 중괄호를 줄바꿈하지 않음
- 함수 매개변수가 길어지더라도 반환타입을 줄바꿈하지 않음
- 라이브러리는 최소화 (then은 따로 사용하지 않음)