Swift API Design Guidelines에서 Parameters/Arguments 를 읽어본다.
/// Return an `Array` containing the elements of `self`
/// that satisfy `predicate`.
func filter(_ predicate: (Element) -> Bool) -> [Generator.Element]
/// Replace the given `subRange` of elements with `newElements`.
mutating func replaceRange(_ subRange: Range, with newElements: [E]) // subrange
// Bad
let order = lastName.compare(
royalFamilyName, options: [], range: nil, locale: nil)
// Good
let order = lastName.compare(royalFamilyName)
min(number1, number2)
zip(sequence1, sequence2)
Int64(someUInt32)
Int(someString)
extension String {
// Convert `x` into its textual representation in the given radix
init(_ x: BigInt, radix: Int = 10) // Note the initial underscore
}
extension UInt32 {
init(_ value: Int16) // 단순 변환, label이 없다.
init(truncating source: UInt64) // 값을 자른다는 추가 동작이 들어간다. label이 있다.
init(saturating valueToApproximate: UInt64) // 가장 가까운 정수로 떨어트리는 동작이 있다.
}
x.removeBoxes(havingLength: 12)
removeBoxes(havingLength length: Int) // 이런식으로 되어 있을 것.
// Bad
a.move(toX: b, y: c)
a.fade(fromRed: b, green: c, blue: d)
a.moveTo(x: b, y: c)
a.fadeFrom(red: b, green: c, blue: d)
x.addSubview(y) // 굳이 subviews라는 label을 붙일 필요가 없다.
// Bad
view.dismiss(false) // Don't dismiss? Dismiss a Bool?
words.split(12) // Split the number 12?
// Good
view.dismiss(animated: false)
let text = words.split(maxSplits: 12)
let studentsByName = students.sorted(isOrderedBefore: Student.namePrecedes)
mutating func ensureUniqueStorage(minimumCapacity requestedCapacity: Int, allocate: (_ byteCount: Int) -> UnsafePointer<Void>) -> (reallocated: Bool, capacityChanged: Bool)
struct Array {
// 가장 뒤에 element를 추가한다.
public mutating func append(_ newElement: Element)
// 가장 뒤에 elements들을 순서대로 넣는다. 얘는 여러개다.
public mutating func append(_ newElements: S)
where S.Generator.Element == Element
}
var values: [Any] = [1, "a"]
values.append([2, 3, 4]) // [1, "a", [2, 3, 4]] or [1, "a", 2, 3, 4]?
struct Array {
public mutating func append(_ newElement: Element)
public mutating func append(contentsOf newElements: S)
where S.Generator.Element == Element
}
// 이런식으로 Range를 함수이름에 붙이고, 파라미터 이름을 숨긴다.
// 추가적으로 받는 인자에 대해 전치사를 사용한다.
mutating func replaceRange(_ r: Range, with: [E])
a.moveTo(x: b, y: c)
a.fadeFrom(red: b, green: c, blue: d)
x.removeBoxes(havingLength: 12)
removeBoxes(havingLength length: Int) // 이런식으로 되어 있을 것.