Last active
April 24, 2016 12:35
-
-
Save eofster/62e571d3dd87664935ed to your computer and use it in GitHub Desktop.
Vending machine vend() function refactored
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(item: Item, name: String) { | |
var item = item | |
item.count -= 1 | |
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I love the sentiment here, but why not use
guard
in your validate methods? I feel it reads a little cleaner as you get to write your condition to what you actually want, rather than thinking of the inverse and writing what you don't want.