Skip to content

Instantly share code, notes, and snippets.

@eofster
Last active April 24, 2016 12:35
Show Gist options
  • Save eofster/62e571d3dd87664935ed to your computer and use it in GitHub Desktop.
Save eofster/62e571d3dd87664935ed to your computer and use it in GitHub Desktop.
Vending machine vend() function refactored
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)
}
}
@eofster
Copy link
Author

eofster commented Apr 24, 2016

@cwagdev, I agree it reads better with guard!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment