Our Swift Style Guide Is Now Open Source
We’ve been using Swift at Airbnb for four years. The language has changed and our practices have too. Today, we’re sharing how we write Swift.
A brief history of Swift at Airbnb
At WWDC 2014, Apple surprised us with a new language: Swift. At Airbnb, we quickly jumped in head first and wrote our first line of Swift in August 2014 — even before Swift reached 1.0.
Later that year, Apple announced the Apple Watch and WatchKit. Given the community’s excitement for the long-rumored Apple Watch, in April of 2015 we took a bet and started writing the first version of Airbnb’s Apple Watch app completely in Swift 1.1.
At WWDC 2015 Apple officially announced Swift 2.0. So when it was time to build our Apple TV app, we gave it a try and wrote it using Swift 2.0.
After these 2 successful pilots, in January 2016 we decided we wanted to write all of our new features in Swift 2.0. Everything was going swimmingly until the Great Swift Rename landed. To upgrade to Swift 3, we tasked a two-person team to work on migrating our codebase over a 5-week period. Thankfully, upgrading to Swift 4 was much simpler; our entire codebase has been in Swift 4 since October 2017.
Why a Style Guide?
Swift is a young language. When we started using it back in 2014, we had no standardized Swift style guidelines. We let 15 engineers loose in our codebase, each of them writing with their own personal Swift style. It quickly became clear that if we didn’t agree on a standardized style, we’d either spend too much time discussing style in our PRs, or our codebase would resemble a Jackson Pollock painting.
When we were working on our Apple TV app, we started an informal style guide. In January 2016 this was combined with other ad-hoc efforts to become the official Airbnb Swift Style Guide, where we started collaborating to define what was the preferred way to write Swift at Airbnb.
Apple and the Swift community have provided valuable guidance on how to write Swift. Even though they absolutely influenced how we write Swift at Airbnb, we still see value in keeping our own Style Guide as a way to iterate on what feels correct to us, while at the same time keeping in line with the community. That’s why we added Apple’s recommendations to our guiding tenets.
We don’t want to manually identify and correct style guide violations, so we’ve adopted the most popular linter and formatter in the Swift community, SwiftLint and SwiftFormat, respectively. In our Style Guide you’ll find our SwiftLint and our SwiftFormat configurations. If you want to use the same rules as us, just grab them and start using it in your projects!
Why are we open sourcing it?
Years have passed since we first started writing Swift and the community has standardized on some patterns. The Swift language and community has been great to us, so we wanted to contribute back by sharing some of the patterns we’ve arrived at for writing Swift at Airbnb.
We understand not everybody will agree with how we do things at Airbnb. We believe in respectful dissent and we’d love to hear your feedback — we’re happy to share our thoughts. If you disagree or think we’re missing something, we welcome your contributions!
At Airbnb, we consider Swift the future of iOS development, and we’re going to continue pushing the boundaries of cutting-edge native technology. Given that we’re sunsetting React Native, we’re investing even more resources in our internal Swift and Kotlin libraries.
70% of our new native Android code is in Kotlin, and 90% of iOS is written in Swift.* We are constantly migrating to the latest language features.
*Doesn’t include legacy React Native code.
If you’re passionate about Swift and iOS development, we encourage you to apply for any of the different roles we have open! We’d love to have you on the team!