Being new to Optionals, sometimes I struggle for good examples on where and when to use them.

Here’s an example I link that show how one could use an Optional to model an AccountBalance.

struct AccountBalance {
    init(_ ledger: Money, overdraftLimit: Money? = nil) {
        self.ledger = ledger
        self.overdraftLimit = overdraftLimit
    }

    var available: Money {
        if let overdraftLimit = overdraftLimit {
            return ledger + overdraftLimit
        }
        return ledger
    }

    let ledger: Money
    let overdraftLimit: Money?
}

Both the ledger and overdraftLimit are let’s (meaning once assigned they can’t be changed). But the overdraftLimit is also an Optional because the overdraftLimit may or may not exist for this account.

I like this example because it shows how the overdraftLimit exists for some but not others. It truly is Optional. And for those who don’t have it we need some kind of check.

Building on that, here are the methods to increase/decrease the amounts of funds.

func increased(by amount: Money) -> AccountBalance {
    return AccountBalance(ledger + amount, overdraftLimit: overdraftLimit)
}

func decreased(by amount: Money) -> AccountBalance? {
    guard amount <= available else { return nil }
    return AccountBalance(ledger - amount, overdraftLimit: overdraftLimit)
}

Notice here how the AccountBalance in decreasedBy is also an Optional? This is because this operation could fail if the account does not have sufficient funds. This is a really good example of how to use a guard clause to see if something bad might happen, and then instead of throwing an error (i.e. InSufficientFunds) instead simply returning a nil when the client can guard and look for (which we will see in the next step).

We can bring it all together like this.

class Account {
init(identifier: AccountIdentifier, balance: AccountBalance) {
self.identifier = identifier
self.balance = balance
}

func credit(_ amount: Money) {
balance = balance.increased(by: amount)
}

func debit(_ amount: Money) throws {
guard let newBalance = balance.decreased(by: amount) else {
throw AccountError.insufficientBalance
}
balance = newBalance
}

let identifier: AccountIdentifier
private(set) var balance: AccountBalance
}

enum AccountError: Error {
case insufficientBalance
}

Here we can see how:

  • The AccountIdentifer can be a let, but a let that dynamically builds itself based on internal properties
  • The AccountBalance is a private(set) meaning no one can change its value save the AccountBalance itself
  • The AccountBalance continuously creates new instances of itself, always representing itself as a Value object (safer than a reference)
  • The debit method looks for that Optional if anything goes bad (much nicer than having to try catch and error). Here is simply looks for the nil (or Optional) being returned by protects itself with a guard clause at which point it decides to throw an Error

Really nice example showing a lot of the different constructs and how to use them in Swift.

Thanks to Khawer Khaliq for this great example.

Advertisements