
自从guard语句在Swift中出现以来,就引起了大量的讨论。讲真,guard确实简化了代码,且提高了代码的可读性,但它就是灵丹妙药了吗?
小函数
关于函数的大小,人们也讨论了很多。很明显,函数体应该是短小的,而且是越短越好。没有人想去读、理解或者重构大函数。但是,函数应该多大才是正确的呢?
函数的首要准则是--短。次要准则是--更短。 ---罗伯特`C`马丁
更具体点说,马丁认为函数的长度应该小于六行,绝对不能大于十行。
规则虽简,疗效显著,你可以看到代码立刻就变得易懂多了。以前,你需要记住一个有着30行代码,若干缩进层次,若干中间变量的函数。而现在只需要记住十个名字一目了然的函数。
单一职责
单一职责这件事也被说了很久。这条规则不仅适用于对象,也同样适用于函数。很显然,每一个函数应该只做一件事情,但是人们一次两次地违反此规则,似乎大部分是因为函数的大小。如果将一个30行的函数重构为十个3行的函数,那么在函数层次自然而然地遵循单一职责规则了。
单层抽象
人们对函数的单层抽象讨论得并不多.它是一个有助于写出单一职责函数的工具。
什么是单层抽象? 简单地说,就是高抽象层次的代码,例如控制进程的代码中不应该混杂着小的细节,例如变量自增或者布尔值检验。举个栗子。
下面例子出自The Swift Programming Language book。
- struct Item {
- var price: Int
- var count: Int
- }
- enum VendingMachineError: ErrorType {
- case InvalidSelection
- case InsufficientFunds(coinsNeeded: Int)
- case OutOfStock
- }
- class VendingMachine {
- var inventory = [
- "Candy Bar": Item(price: 12, count: 7),
- "Chips": Item(price: 10, count: 4),
- "Pretzels": Item(price: 7, count: 11)
- ]
- var coinsDeposited = 0
- func dispense(snack: String) {
- print("Dispensing \(snack)")
- }
- func vend(itemNamed name: String) throws {
- guard var item = inventory[name] else {
- throw VendingMachineError.InvalidSelection
- }
- guard item.count > 0 else {
- throw VendingMachineError.OutOfStock
- }
- guard item.price <= coinsDeposited else {
- throw VendingMachineError.InsufficientFunds(coinsNeeded: item.price - coinsDeposited)
- }
- coinsDeposited -= item.price
- --item.count
- inventory[name] = item
- dispense(name)
- }
- }
当然,vend(itemNamed:)只是使用gaurd语句的一个例子,不过你经常能在生产代码中看到相似的函数。这个函数就出现了三个上面说到的问题:
这个函数重构后是什么样呢?
- func vend(itemNamed name: String) throws {
- let item = try validatedItemNamed(name)
- reduceDepositedCoinsBy(item.price)
- removeFromInventory(item, name: name)
- dispense(name)
- }
- private func validatedItemNamed(name: String) throws -> Item {
- let item = try itemNamed(name)
- try validate(item)
- return item
- }
- private func reduceDepositedCoinsBy(price: Int) {
- coinsDeposited -= price
- }
- private func removeFromInventory(var item: Item, name: String) {
- --item.count
- inventory[name] = item
- }
- private func itemNamed(name: String) throws -> Item {
- if let item = inventory[name] {
- return item
- } else {
- throw VendingMachineError.InvalidSelection
- }
- }
- private func validate(item: Item) throws {
- try validateCount(item.count)
- try validatePrice(item.price)
- }
- private func validateCount(count: Int) throws {
- if count == 0 {
- throw VendingMachineError.OutOfStock
- }
- }
- private func validatePrice(price: Int) throws {
- if coinsDeposited < price {
- throw VendingMachineError.InsufficientFunds(coinsNeeded: price - coinsDeposited)
- }
- }
虽然总行数变多了,但你应该记住,代码行数并不是它的最终目的。
重构后的代码相对于旧版的多了几个优点:
结论
Guard语句很便于用来减少结构体和函数的嵌套,但是问题不是guard本身,而是它的使用。Guard语句会促使你写出能做好几件事、有多层抽象层次的大函数。只要保证所写的函数小而明确,你根本无需guard语句。
相关阅读