Skip to content

Instantly share code, notes, and snippets.

@ilyannn
Forked from erica/bangbang.md
Last active June 29, 2017 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilyannn/4c5530c75293db0324a04f50ae691b2a to your computer and use it in GitHub Desktop.
Save ilyannn/4c5530c75293db0324a04f50ae691b2a to your computer and use it in GitHub Desktop.

Instead of

assert(!array.isEmpty, "Array guaranteed to be non-empty because...")
let lastItem = array.last!

guard !array.isEmpty 
    else { fatalError("Array guaranteed to be non-empty because...") }

// ... etc ...

I would use

guard let lastItem = array.last else { ... bail ... }

Instead of

// In a right-click gesture recognizer action handler
let event = NSApp.currentEvent !! "Trying to get current event for right click, but there's no event”

// drawRect:
override draw(_ rect: CGRect) {
    let context = UIGraphicsGetCurrentContext() !! "ಠ_ಠ"
}

I would say, if system frameworks don't do their job it's better to silently fail in redrawing rather than crash on the poor user:

// In a right-click gesture recognizer action handler
guard let event = NSApp.currentEvent else { 
#if DEBUG
  fatalError("Trying to get current event for right click, but there's no event")
#endif
  return 
}

// drawRect:
override draw(_ rect: CGRect) {
    guard let context = UIGraphicsGetCurrentContext() else { return }
}

Instead of checking the existing property on every access

// In a custom view controller subclass that only 
// accepts children of a certain kind:
let existing = childViewControllers as? Array<TableRowViewController> !! "TableViewController must only have TableRowViewControllers as children"

// Providing a value based on an initializer that returns an optional:
lazy var emptyURL: URL = { return URL(string: “myapp://section/\(identifier)") !! "can't create basic empty url” }()

// and then again 
lazy var questionURL: URL = { return URL(string: “myapp://section/\(another_identifier)") !! "can't create basic question url” }()

// Retrieving an image from an embedded framework:
private static let addImage: NSImage = {
    let bundle = Bundle(for: FlagViewController.self)
    let image = bundle.image(forResource: "add") !! "Missing 'add' image"
    image.isTemplate = true
    return image
}()

// Asserting consistency of an internal model:
let flag = command.flag(with: flagID) !! "Unable to retrieve non-custom flag for id \(flagID.string)"

it is better to declare your contract explicitely and check the new properties:

// In a custom view controller subclass that only 
// accepts children of a certain kind:
var tableRowChildren: Array<TableRowViewController>
... 
let existing = tableRowChildren 

// Providing a value based on an initializer that returns an optional:
lazy var emptyURL: URL = { return MyAppURL(forPath: “section/\(identifier)") }() // always succeeds
lazy var questionURL: URL = { return MyAppURL(forPath: “section/\(another_identifier)") }() 

// Retrieving an image from an embedded framework:
private static let addImage = NSImage.myExistingTemplateImage(named: "add", forClass: FlagViewController.self)

// Asserting consistency of an internal model:
let flag = command.flag(existingForId: flagID) // crash if no flagId
let flagWithFallback = command.flag(forId: flagID, fallback: true) // return 'true' if no flagId
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment