defer is built into the language to ease handling a finaliziation where multiple code flow paths are available.
But defer is usually complementrarty to an action that is planned to happen at the begining of the method, examples come to mind
- Transcations handling (The defer example used on 106 - What's New in Swift 45:19)
- Timeing execution time (take system time at the begining, take system time at end) of methods
- Pull semaphore lock queue -> ..use.. -> Return semaphore to queue
- and co.
There I think it's wise to add an early keyword that will happen when method starts, which is followed by a defer that will happen when function ends. Then followed by the body of the method itself.
As an example, taken from the mentioned 106 WWDC'15 talk:
func processSale(json: AnyObject) throws {
early { delegate?.didBeginReadingSale() }
defer { delegate?.didEndReadingSale() }
guard let buyerJson = json["buyer"] as? NSDictionaey {
throw DataError.MissingBuyer
}
let buyer = try processPerson(buyerJson)
guard let price = json["price"] as? Int {
throw DataError.MissingPrice
}
return Sale(buyer, price)
}In this example you can clearly distinguishe between code responsible for "wrapping" the logic and the logic inself. I think it's awesome, and would apprecite feedback.
-m