[swift-evolution] API Guidelines for Asynchronous Completion Handlers?

Alex Migicovsky migi at apple.com
Fri Dec 4 10:57:04 CST 2015


> On Dec 3, 2015, at 2:15 PM, Douglas Gregor <dgregor at apple.com> wrote:
> 
>> On Dec 3, 2015, at 12:32 PM, Dan Stenmark <daniel.j.stenmark at gmail.com <mailto:daniel.j.stenmark at gmail.com>> wrote:
>> 
>> There’s a some of debate in the community regarding best practice for asynchronous completion callbacks.  These practices include:
>> 
>> - Single Block w/ Mutually Exclusive Result and Error Objects (the current standard convention in Cocoa, though originally designed with Objective-C in mind)
>> - Double Block (one for success, one for failure)
>> - Swift Enum w/ Associated Objects (as described here: http://www.developerdave.co.uk/2015/09/better-completion-handlers-in-swift/ <http://www.developerdave.co.uk/2015/09/better-completion-handlers-in-swift/>)
>> 
>> Even prior to Swift, Apple’s code guidelines never explicitly addressed this topic.  Going forward into the brave new world of Swift, are there going to be new preferred API design guidelines for this?
> 
> This is a great point, and there are a number of other issues related to callbacks/closure arguments that would benefit from guidelines. For example, I've seen the “Double Block” case where the second block ends up being a trailing closure, which makes for non-intuitive uses.

Hi Dan!

I think guidelines in this area would be great.

Here are the tradeoffs I think we have for each approach:

1) The single block approach means you’d code against an optional result and an optional error, making it easy to write invalid code (see example in #2). With the single block you can use trailing closure syntax coherently. I think most ObjC APIs use this approach since it works well in ObjC.

2) As Doug mentioned, the double block can be inconvenient / awkward but it does produce more correct code. 

Doug: maybe we can limit using trailing closures from being used if the 2nd to last parameter is also a closure? That would eliminate some confusion at the call site.

Some ObjC APIs use this approach. One positive aspect of the double-block approach is that it always produces code that’s less indented than the single block approach. e.g.

     /*
          Single block.
          Trailing closure syntax works well.
     */
     request.fetch { result, error in
          // More indented code since we need to use guard or if let.
          guard let result = result else {
               // Need to force unwrap `error`.
               handleFetchError(error!) 
               return 
          }

          use(result)
     }

     /*
          Double block.
          Trailing closure syntax is awkward here.
     */
     request.fetch(withCompletionHandler: { result in
          use(result)          
     }, errorHandler: { error in
          // Don’t need to force unwrap `error`.
          handleFetchError(error) 
     })

3) Enums with associated values are conceptually nice, but unless we have a Result<> or an Either<> in the Standard Library I think most people will write one-off enums for each set of methods that return a specific kind of result + error. That adds an unnecessary conceptual burden since you need to know the type of of the value that’s passed to each callback. Also, we don’t have any primarily ObjC APIs that use this approach yet. It would also suffer from the same indentation problem as #1 but without the “invalid code” problem. If we go this route I think we’d want to map the async error ObjC APIs to use this approach similar to what we do with non-async error handling.

Looking at the tradeoffs I think I prefer #2 if we could limit the ability to use a trailing closure for the last parameter. I’d want to look at more code with the change though. We should also consider whether we should map the single block APIs in ObjC into double block APIs. What do you think?

Also, with any of these approaches there’s also the question of whether we pass ErrorType, NSError, or the specific error type.

- Alex


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151204/e429cbf6/attachment-0001.html>


More information about the swift-evolution mailing list