{"action":"created","issue":{"url":"https://api.github.com/repos/wordpress-mobile/swift-style-guide/issues/7","repository_url":"https://api.github.com/repos/wordpress-mobile/swift-style-guide","labels_url":"https://api.github.com/repos/wordpress-mobile/swift-style-guide/issues/7/labels{/name}","comments_url":"https://api.github.com/repos/wordpress-mobile/swift-style-guide/issues/7/comments","events_url":"https://api.github.com/repos/wordpress-mobile/swift-style-guide/issues/7/events","html_url":"https://github.com/wordpress-mobile/swift-style-guide/issues/7","id":170534169,"number":7,"title":"Avoid forced downcasting and unwrapping","user":{"login":"nheagy","id":517257,"avatar_url":"https://avatars.githubusercontent.com/u/517257?v=3","gravatar_id":"","url":"https://api.github.com/users/nheagy","html_url":"https://github.com/nheagy","followers_url":"https://api.github.com/users/nheagy/followers","following_url":"https://api.github.com/users/nheagy/following{/other_user}","gists_url":"https://api.github.com/users/nheagy/gists{/gist_id}","starred_url":"https://api.github.com/users/nheagy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nheagy/subscriptions","organizations_url":"https://api.github.com/users/nheagy/orgs","repos_url":"https://api.github.com/users/nheagy/repos","events_url":"https://api.github.com/users/nheagy/events{/privacy}","received_events_url":"https://api.github.com/users/nheagy/received_events","type":"User","site_admin":false},"labels":[{"id":373841711,"url":"https://api.github.com/repos/wordpress-mobile/swift-style-guide/labels/proposal","name":"proposal","color":"fbca04","default":false}],"state":"open","locked":false,"assignee":{"login":"diegoreymendez","id":1836005,"avatar_url":"https://avatars.githubusercontent.com/u/1836005?v=3","gravatar_id":"","url":"https://api.github.com/users/diegoreymendez","html_url":"https://github.com/diegoreymendez","followers_url":"https://api.github.com/users/diegoreymendez/followers","following_url":"https://api.github.com/users/diegoreymendez/following{/other_user}","gists_url":"https://api.github.com/users/diegoreymendez/gists{/gist_id}","starred_url":"https://api.github.com/users/diegoreymendez/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/diegoreymendez/subscriptions","organizations_url":"https://api.github.com/users/diegoreymendez/orgs","repos_url":"https://api.github.com/users/diegoreymendez/repos","events_url":"https://api.github.com/users/diegoreymendez/events{/privacy}","received_events_url":"https://api.github.com/users/diegoreymendez/received_events","type":"User","site_admin":false},"assignees":[{"login":"diegoreymendez","id":1836005,"avatar_url":"https://avatars.githubusercontent.com/u/1836005?v=3","gravatar_id":"","url":"https://api.github.com/users/diegoreymendez","html_url":"https://github.com/diegoreymendez","followers_url":"https://api.github.com/users/diegoreymendez/followers","following_url":"https://api.github.com/users/diegoreymendez/following{/other_user}","gists_url":"https://api.github.com/users/diegoreymendez/gists{/gist_id}","starred_url":"https://api.github.com/users/diegoreymendez/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/diegoreymendez/subscriptions","organizations_url":"https://api.github.com/users/diegoreymendez/orgs","repos_url":"https://api.github.com/users/diegoreymendez/repos","events_url":"https://api.github.com/users/diegoreymendez/events{/privacy}","received_events_url":"https://api.github.com/users/diegoreymendez/received_events","type":"User","site_admin":false}],"milestone":null,"comments":10,"created_at":"2016-08-10T22:30:00Z","updated_at":"2017-01-11T07:08:58Z","closed_at":null,"body":"Avoid using `as!` to force a downcast, or `!` to force unwrapping.\r\n### Examples\r\n\r\nUse `as?` to attempt the cast, then deal with the failure case explicitly\r\n\r\n``` swift\r\n// Correct\r\nfunc process(someObject: Any) {\r\n    guard let element = someObject as? Element else {\r\n        // Optional error handling goes here\r\n        return\r\n    }\r\n    process(element)\r\n}\r\n\r\n// Wrong\r\nfunc process(someObject: Any) {\r\n    process(someObject as! Element)\r\n}\r\n```\r\n\r\nOr use `throw` when the failure is unrecoverable\r\n\r\n``` swift\r\nfunc processedItems() throws -> Int {\r\n    guard items = theseThings as? [Item] else {\r\n        throw ThingItemsError.ProcessingError\r\n    }\r\n    return process(items)\r\n}\r\n```\r\n### Rationale\r\n\r\nErica Sadun says [\"Every time you use an exclamation point in Swift, a kitten dies\"](http://ericasadun.com/2015/11/17/a-handful-of-swift-style-rules-swiftlang/) \r\n\r\nForced casting and unwrapping can make room for unexpected errors, which are often crashes.  They tend to look like this:\r\n\r\n![image](https://cloud.githubusercontent.com/assets/517257/17573323/e18d2162-5f16-11e6-9713-2bb66e613e57.png)\r\n\r\nSwift was designed to avoid these kinds of errors with tools like optionals and `throws`. It's often more effort to account for the possibilities but that's as intended 😛 \r\n### Enforcing the rule\r\n\r\nPeer reviews are likely the only viable method of enforcement.\r\n"},"comment":{"url":"https://api.github.com/repos/wordpress-mobile/swift-style-guide/issues/comments/271795002","html_url":"https://github.com/wordpress-mobile/swift-style-guide/issues/7#issuecomment-271795002","issue_url":"https://api.github.com/repos/wordpress-mobile/swift-style-guide/issues/7","id":271795002,"user":{"login":"koke","id":8739,"avatar_url":"https://avatars.githubusercontent.com/u/8739?v=3","gravatar_id":"","url":"https://api.github.com/users/koke","html_url":"https://github.com/koke","followers_url":"https://api.github.com/users/koke/followers","following_url":"https://api.github.com/users/koke/following{/other_user}","gists_url":"https://api.github.com/users/koke/gists{/gist_id}","starred_url":"https://api.github.com/users/koke/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/koke/subscriptions","organizations_url":"https://api.github.com/users/koke/orgs","repos_url":"https://api.github.com/users/koke/repos","events_url":"https://api.github.com/users/koke/events{/privacy}","received_events_url":"https://api.github.com/users/koke/received_events","type":"User","site_admin":false},"created_at":"2017-01-11T07:08:58Z","updated_at":"2017-01-11T07:08:58Z","body":"> Do you think your suggestion can be worded in a way that makes sense for the guide?\r\n\r\nThe current text already says \"avoid\", which I read as \"try not to use it unless it's really a good fit\". As long as we're on the same page on that, it's fine as it is.\r\n\r\nHowever, if that's the case and we're not going to have it enforced by SwiftLint, I'd move the rule to the \"Must read\" section"}}
{"actor":{"display_login":"koke"}}