函数声明/Function Declarations
Keep short function declarations on one line including the opening brace:
保持函数的声明语句在一行以内,包括左括号:
func reticulateSplines(spline: [Double]) -> Bool {
// reticulate code goes here
}
For functions with long signatures, put each parameter on a new line and add an extra indent on subsequent lines:
对于函数名较长的情况,需要保证每个参数新起一行,且在该行的开始处添加一个缩进符:
func reticulateSplines(
spline: [Double],
adjustmentFactor: Double,
translateConstant: Int,
comment: String
) -> Bool {
// reticulate code goes here
}
Don't use (Void)
to represent the lack of an input; simply use ()
. Use Void
instead of ()
for closure and function outputs.
不要使用 (Void)
来表示入参为空的情况,用 ()
即可。在闭包和函数的输出参数为空的情况时,推荐使用 Void
, 而不是 ()
。
推荐(Preferred):
func updateConstraints() -> Void {
// magic happens here
}
typealias CompletionHandler = (result) -> Void
不推荐(Not Preferred):
func updateConstraints() -> () {
// magic happens here
}
typealias CompletionHandler = (result) -> ()
函数调用/Function Calls
Mirror the style of function declarations at call sites. Calls that fit on a single line should be written as such:
调用代码应该和函数声明风格一致。如果调用函数只需要一行就可以完成,应当像下面这样组织代码:
let success = reticulateSplines(splines)
If the call site must be wrapped, put each parameter on a new line, indented one additional level:
当函数调用必须换行时,需要让每个参数新起一行并添加缩进:
let success = reticulateSplines(
spline: splines,
adjustmentFactor: 1.3,
translateConstant: 2,
comment: "normalize the display")