Error Handling in Swift: Might and Magic
Alexandros Salazar puts together an interesting technique for greatly simplifying error propogation through multiple layers of an app. As he summarizes:
The need for error handling during computations is abstracted away. All I need to do is specify what computations I want to apply, and
map will take care of propagating any errors.
This approach has the potential to remove a ton of boilerplate from the typical data flow of an application. As Alexandros summarizes, the flow of a typical app starts with retrieving data from an endpoint, then parses it into a JSON object, builds up model objects from that JSON, and gets that data on the screen. Each operation usually includes its own level of error handling. That error handling is almost always just propogation of the error from one layer to the next. Alexandros’s technique can simplify that flow such that at each level, the code is only concerned with handling the success case. The error case is automatically and transparently propogated to the next level. The error must eventually be handled of course, but that can happen only at the top level where the error needs to impact the UI. It cuts out all the code duplication of error propogation typically included.
Eliminating code, especially meaningless boilerplate code, is one of the most fundamental benefits any programming language can bring. Every line of code is somewhere a bug might be hiding. Objective-C requires a ton of code to do even very simple things. Techniques in Swift like this that hold the potential of eliminating huge swaths of useless code is extremely exciting.