API Design Guidelines πŸ“™

YaR LabΒ·2023λ…„ 12μ›” 4일
0

swiftΒ πŸ“™

λͺ©λ‘ 보기
13/16
post-thumbnail

1️⃣ Naming

  • λͺ¨ν˜Έν•¨μ„ ν”Όν•˜κΈ° μœ„ν•΄ λ³€μˆ˜λ‚˜ ν•¨μˆ˜κ°€ ν•˜λŠ” 일의 λͺ¨λ“  단어λ₯Ό 포함해야함
extension List {
  public mutating func remove(at position: Index) -> Element
}
employees.remove(at: x)
  • μ€‘μš”ν•œ μ •λ³΄λ§Œ λ“€μ–΄κ°€μ•Όν•˜κ³  μ€‘λ³΅μ΄λ‚˜ λΆˆν•„μš”ν•œ 정보가 이름에 λ“€μ–΄κ°€λ©΄ μ•ˆλ¨
public mutating func remove(_ member: Element) -> Element?

allViews.remove(cancelButton) // clearer
  • νƒ€μž…μ΄ 이름에 λ“€μ–΄κ°€λ©΄ μ•ˆλ˜κ³ , 역할이 이름에 듀어가야함
// var string = "Hello"
var greeting = "Hello"
protocol ViewController {
  // associatedtype ViewType : View
  associatedtype ContentView : View
}
class ProductionLine {
  // func restock(from widgetFactory: WidgetFactory)
  func restock(from supplier: WidgetFactory)
}
  • ν”„λ‘œν† μ½œ 이름이 역할이 λ˜λŠ” 경우 ν”„λ‘œν† μ½œμ΄λ¦„μ— Protocol을 μΆ”κ°€ν•˜μ—¬ μΆ©λŒμ„ 피해야함
protocol Sequence {
  associatedtype Iterator : IteratorProtocol
}
protocol IteratorProtocol { ... }
  • λ©”μ„œλ“œλ₯Ό μ„ μ–Έν•˜λŠ”κ³³κ³Ό μ‚¬μš©ν•˜λŠ” κ³³μ—μ„œ λ§€κ°œλ³€μˆ˜μ˜ 역할을 λͺ…ν™•νžˆ λ‚˜νƒ€λ‚Ό 수 μžˆμ–΄μ•Όν•¨
// func add(_ observer: NSObject, for keyPath: String)
func addObserver(_ observer: NSObject, forKeyPath path: String)
// grid.add(self, for: graphics) // vague
grid.addObserver(self, forKeyPath: graphics) // clear

2️⃣ Strive for Fluent Usage

Tip

  • initailize, get, set, make λŒ€μ‹  generate, configureλ₯Ό μ‚¬μš©

μΆœμ²˜πŸ“š

🍎Apple Docs: api-design-guidelines

0개의 λŒ“κΈ€