August 23, 2016
Everybody likes free stuff, and developers are no exception. Object Oriented Programming (OOP) gives developers a way to get free stuff by subclassing objects. Subclassing is convenient. However, an app that has been subject to “subclass abuse” can be extremely complicated and hard to understand. Complex code has a tendency to be buggy. Is there a way to get free stuff without subclassing and have simple code at the same time? Enter Protocol-Oriented Programming.
Swift is the world’s first Protocol-Oriented Programming language that also supports OOP and Functional Programming. As we know, protocols are not new to iOS applications. In fact, protocols are a critical part of the delegate pattern that is extremely common in iOS apps. Swift 2.0 introduced extension protocols, and this is where the power of Protocol-Oriented Programming begins. Extension protocols give us the ability to provide customization without subclassing.
To test this functionality, I created a simple extension protocol called iCloudSaveable. The idea behind iCloudSavable is to easily add any model object to the iCloud. This works by using the Mirroring API in Swift to introspect the model object. This extension protocol takes advantage of the CloudKit’s just-in-time schema binding, which automatically creates database schemas on the fly. Adopting this behavior is easy; simply conform to this protocol and add the required uuid to the model. Once this is done, simply call saveToICloud to save your model to the iCloud. Free stuff without subclassing.
When the model code is examined, it’s easy to see the behavior that extends our simple struct into something quite powerful. See Figure 1.
Figure 1: Adopting the iCloudSaveable Protocol
Notice that the Person model is a struct and not a class. Struct is the preferred construct for models in Swift because they are value types. Using value types ensures that instances of this model cannot have multiple references to it. This eliminates the need for developers to write defensive copying code. Preferring value types over reference types is another paradigm called Value Type Programming. The combination of Protocol-Oriented Programming and Value Type Programming encourages a concept called Local Reasoning. Apple introduced this term at WWDC 2016. Local Reasoning is the ability to understand code within a context, without having to understand complicated relationships or abstract references. In this simple example, it’s easy to see that this value type of Person has the ability to be saved to the iCloud.
Protocol-Oriented Programming gives developers a way of customizing behavior that is simple and easy to read. Code that is less complex will have fewer bugs, be easier to extend, and easier to maintain. These are all good attributes. Does this mean we can drop OOP entirely? Not exactly. UIKit is built entirely around classes and references, which means we will still have to use reference types and objects in the UI layer.
The most practical way to introduce Protocol-Oriented Programming into your project is to think about your app as having two main layers: an object layer and a value layer. The object layer contains classes that represent UI components. The object layer consumes events and consults the value layer. The value layer contains structs and extension protocols that are responsible for implementing all the business logic. The goal is to make the object layer as thin as possible and push as much code into the value layer.
The combination of Protocol-Oriented Programming and Value Oriented Programming reduces code complexity, improves readability and the quality of software. Extension protocols and delegates allow developers to add customization without subclassing, which promotes Local Reasoning; The ability to quickly understand code in a given context. Splitting an application into two sections allows developers to use reference types and value types, giving them the opportunity to use OOP and Protocol-Oriented Programming.
Marty Burolla is a senior iOS engineer at Enola Labs, an Austin, Texas-based digital studio specializing in custom products for mobile and web.