{"id":8716,"date":"2014-04-16T13:14:14","date_gmt":"2014-04-16T17:14:14","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=8716"},"modified":"2014-04-16T13:14:14","modified_gmt":"2014-04-16T17:14:14","slug":"tuples-and-nserror","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2014\/04\/16\/tuples-and-nserror\/","title":{"rendered":"Tuples and NSError"},"content":{"rendered":"<p><a href=\"http:\/\/edgecasesshow.com\/088-tuple-for-your-thoughts.html\">Edge Cases episode 88<\/a>:<\/p>\n<blockquote cite=\"http:\/\/edgecasesshow.com\/088-tuple-for-your-thoughts.html\"><p>Andrew Pontious talks with Wolf Rentzsch about the simplest of things, the tuple: what it is, how it is used in other languages (specifically Python), and how, in an alternate universe, it could bring some sanity to Cocoa error handling.<\/p><\/blockquote>\n<p>Tuples in Python are great, particularly because there&rsquo;s syntax for unpacking (a.k.a. destructuring). It even works with <a href=\"http:\/\/robert-lujo.com\/post\/40871820711\/python-destructuring\">nested<\/a> structures.<\/p>\n<p>They hypothesize that <code>NSError<\/code> was introduced in Mac OS X 10.4 with Core Data. My recollection is that it was added with Safari 1.0 and WebKit, which could be installed on Mac OS X 10.2 and was built into 10.2.7.<\/p>\n<p>Although I would certainly welcome Objective-C support for tuples and language-level support for errors, I&rsquo;m not sure that it makes sense to implement the latter using the former.<\/p>\n<p>One of the few virtues of using <code>(NSError **)<\/code> parameters is that you can pass in <code>NULL<\/code> if you only care about success\/failure, not the reason for the failure. I&rsquo;ve found that this is sometimes very useful for performance reasons. There&rsquo;s a tension between putting lots of useful information in the error object and creating the error object quickly. <code>NULL<\/code> lets you have your cake an eat it, too. If you know that you will be making many related calls that could fail, you can do this without creating any <code>NSError<\/code> objects. Then you can generate one higher level <code>NSError<\/code> to represent the whole operation, and possibly retry one of the lower level calls without <code>NULL<\/code> to get a suitable underlying error object. This level of control would not be possible if methods always returned a tuple with a full error object.<\/p>\n<p>Secondly, tuples would require more lines of code because if you want to save the error you can&rsquo;t use the return value in an <code>if<\/code> statement. Instead of:<\/p>\n<pre>if ([self fooAndReturnError:&amp;error])<\/pre>\n<p>you would write something like:<\/p>\n<pre>BOOL ok, NSError *error = [self foo];\nif (ok)\n<\/pre>\n<p>You can see what this is like by calling Cocoa APIs using PyObjC.<\/p>","protected":false},"excerpt":{"rendered":"<p>Edge Cases episode 88: Andrew Pontious talks with Wolf Rentzsch about the simplest of things, the tuple: what it is, how it is used in other languages (specifically Python), and how, in an alternate universe, it could bring some sanity to Cocoa error handling. Tuples in Python are great, particularly because there&rsquo;s syntax for unpacking [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[69,857,54,71,635,232,328],"class_list":["post-8716","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-nserror","tag-objective-c","tag-programming","tag-pyobjc","tag-python","tag-webkit"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/8716","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=8716"}],"version-history":[{"count":0,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/8716\/revisions"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=8716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=8716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=8716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}