Share
Embed
Swift Eye for the Stringly Typed API
This was the talk I gave at try! Swift NYC on September 2nd, 2016.
Andyy Hope
September 02, 2016
Transcript
82 slides
-
SWIFT EYE FOR THE STRINGLY TYPED API Medium: Andyy Hope
Twitter: @andyyhope -
Stringly Typed? "A riff on "strongly typed". Used to describe
an implementation that needlessly relies on strings when programmer & refactor friendly options are available." — Internet -
Stringly Typed? "A riff on "strongly typed". Used to describe
an implementation that needlessly relies on strings when programmer & refactor friendly options are available." — Internet // Stringly typed let image = UIImage(named: "PalletTown") // Strongly typed let tableView = UITableView(style: .Plain) -
Stringly typed • Typos let notification = "userAlertNotificaton" • Side
effects let image = UIImage(named: "Charmandr") • Collisions let keyA = "isUserLoggedIn" let keyB = "isUserLoggedIn" keyA == keyB -
Stringly typed • Runtime crashes tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "CellIdentifer") ... let
cell: UITableViewCell = tableView .dequeueReusableCellWithIdentifier("CelIdentifer") -
Stringly typed • Disorderly • Title cased "IsUserLoggedIn" • Camel
cased "isUserLoggedIn" -
UserDefaults • Used to store small persistent pieces of information:
• Settings • Arrays • Booleans -
UserDefaults • Used to store small persistent pieces of information:
• Settings • Arrays • Booleans • (AKA) Diet CoreData -
UserDefaults • Used to store small persistent pieces of information:
• Settings • Arrays • Booleans • (AKA) Diet CoreData • Stringly typed APIs -
Swift 3: Set API Old .setBool(true, forKey: "isUserLoggedIn") .setInt(1, forKey:
"pokeballCount") .setObject(pikachu, forKey: "pikachu") New .set(true, forKey: "isUserLoggedIn") .set(1, forKey: "pokeballCount") .set(pikachu, forKey: "pikachu") -
Swift 3: Get API Old .boolForKey("isUserLoggedIn") .integerForKey("pokeballCount") .objectForKey("pikachu") New .bool(forKey:
"isUserLoggedIn") .integer(forKey: "pokeballCount") .object(forKey: "pikachu") -
UserDefaults Stringly typed API // Setter UserDefaults.standard.set(true, forKey: "isUserLoggedIn") //
Getter UserDefaults.standard.bool(forKey: "isUserLoggedIn") -
Fix: Constants // UserDefaults.standard.set(true, forKey: "isUserLoggedIn") // UserDefaults.standard.bool(forKey: "isUserLoggedIn") ...
let isUserLoggedInKey = "isUserLoggedIn" UserDefaults.standard.set(true, forKey: isUserLoggedInKey) UserDefaults.standard.bool(forKey: isUserLoggedInKey) -
Fix: Unify struct Constants { struct Keys { // Account
static let isUserLoggedIn = "isUserLoggedIn" // Onboarding ... } } -
// Set UserDefaults.standard .set(true, forKey: Constants.Keys.isUserLoggedIn) // Get UserDefaults.standard .bool(forKey:
Constants.Keys.isUserLoggedIn) -
Fix: Unify with context struct Constants { struct Keys {
struct Account { static let isUserLoggedIn = "isUserLoggedIn" } } } -
// Set UserDefaults.standard .set(true, forKey: Constants.Keys.Account.isUserLoggedIn) // Get UserDefaults.standard .bool(forKey:
Constants.Keys.Account.isUserLoggedIn) -
Fix: Unify Constants.swift struct Account { static let isUserLoggedIn =
"isUserLoggedIn" } -
Fix: Unify with enums Constants.swift enum Account : String {
case isUserLoggedIn } -
struct Constants { struct Keys { enum Account : String
{ case isUserLoggedIn } } } -
// Set UserDefaults.standard .set(true, forKey: Constants.Keys.Account.isUserLoggedIn.rawValue) // Get UserDefaults.standard .bool(forKey:
Constants.Keys.Account.isUserLoggedIn.rawValue) -
Recap // Strings let key = "isUserLoggedIn" // Constant &
Grouped let key = Constants.isUserLoggedIn // Context let key = Constants.Keys.Account.isUserLoggedIn // Safety let key = Constants.Keys.Account.isUserLoggedIn.rawValue -
// Set UserDefaults.standard .set(true, forKey: Constants.Keys.Account.isUserLoggedIn.rawValue) // Get UserDefaults.standard .bool(forKey:
Constants.Keys.Account.isUserLoggedIn.rawValue) -
tl;dw // Set UserDefaults.standard .set(true, forKey: Constants.Keys.Account.isUserLoggedIn.rawValue) // Get UserDefaults.standard
.bool(forKey: Constants.Keys.Account.isUserLoggedIn.rawValue) -
tl;dw // Set // UserDefaults.standard // .set(true, forKey: Constants.Keys.Account.isUserLoggedIn.rawValue) UserDefaults.standard.set(true,
forKey: .isUserLoggedIn) // Get // UserDefaults.standard // .bool(forKey: Constants.Keys.Account.isUserLoggedIn.rawValue) UserDefaults.standard.bool(forKey: .isUserLoggedIn) -
"For every protocol, there is an equal and corresponding protocol
extension" — Crusty's Third Law -
Protocol: BoolDefaultSettable protocol BoolDefaultSettable { associatedtype BoolKey : RawRepresentable }
extension BoolDefaultSettable where BoolKey.RawValue == String { } -
Protocol: BoolDefaultSettable extension BoolDefaultSettable where BoolKey.RawValue == String { //
Setter func set(_ value: Bool, forKey key: BoolKey) { let key = key.rawValue UserDefaults.standard.set(value, forKey: key) } } -
Protocol: BoolDefaultSettable extension BoolDefaultSettable where BoolKey.RawValue == String { //
Getter func bool(forKey key: BoolKey) -> Bool { let key = key.rawValue return UserDefaults.standard.bool(forKey: key) } } -
protocol BoolDefaultSettable { associatedtype BoolKey : RawRepresentable } extension BoolDefaultSettable
where BoolKey.RawValue == String { func set(_ value: Bool, forKey key: BoolKey) { let key = key.rawValue UserDefaults.standard.set(value, forKey: key) } func bool(forKey key: BoolKey) -> Bool { let key = key.rawValue return UserDefaults.standard.bool(forKey: key) } } -
Protocol: DefaultSettable Family protocol IntegerDefaultSettable { ... } protocol DoubleDefaultSettable
{ ... } protocol FloatDefaultSettable { ... } protocol ObjectDefaultSettable { ... } protocol URLDefaultSettable { ... } -
UserDefaults extension extension UserDefaults : BoolDefaultSettable { enum BoolKey :
String { case isUserLoggedIn } } -
UserDefaults extension extension UserDefaults : BoolDefaultSettable { enum BoolKey :
String { case isUserLoggedIn } } ... UserDefaults.standard.set(true, forKey: .isUserLoggedIn) UserDefaults.standard.bool(forKey: .isUserLoggedIn) -
Account extension extension Account : BoolDefaultSettable { enum BoolKey :
String { case isUserLoggedIn } } ... Account.set(true, forKey: .isUserLoggedIn) Account.bool(forKey: .isUserLoggedIn) -
Protocol: KeyNamespaceable protocol KeyNamespaceable { func namespaced<T: RawRepresentable>(_ key: T)
-> String } extension KeyNamespaceable { func namespaced<T: RawRepresentable>(_ key: T) -> String { return "\(Self.self).\(key.rawValue)" } } -
protocol KeyNamespaceable { func namespaced<T: RawRepresentable>(_ key: T) -> String
} extension KeyNamespaceable { func namespaced<T: RawRepresentable>(_ key: T) -> String { return "\(Self.self).\(key.rawValue)" } } // key: "UserDefaults.isUserLoggedIn" -
extension BoolDefaultSettable where BoolKey.RawValue == String { func set(_ value:
Bool, forKey key: BoolKey) { let key = key.rawValue UserDefaults.standard.set(value, forKey: key) } func bool(forKey key: BoolKey) -> Bool { let key = key.rawValue return UserDefaults.standard.bool(forKey: key) } } -
extension BoolDefaultSettable where BoolKey.RawValue == String { func set(_ value:
Bool, forKey key: BoolKey) { let key = namespaced(key) UserDefaults.standard.set(value, forKey: key) } func bool(forKey key: BoolKey) -> Bool { let key = namespaced(key) return UserDefaults.standard.bool(forKey: key) } } -
Collision free UserDefaults.set(true, forKey: .isUserLoggedIn) // key: "UserDefaults.isUserLoggedIn" Account.set(true, forKey:
.isUserLoggedIn) // key: "Account.isUserLoggedIn" -
Constants.swift extension Account : BoolDefaultSettable { ... } extension Onboarding
: BoolDefaultSettable { ... } struct Constants { ... } -
Constants.swift extension Account : BoolDefaultSettable { ... } extension Onboarding
: BoolDefaultSettable { ... } struct Constants { ... } ... Account.set(true, forKey: .isUserLoggedIn) Account.bool(forKey: .isUserLoggedIn) -
Defaults.swift struct Defaults { struct Account : BoolDefaultSettable { ...
} struct Onboarding : BoolDefaultSettable { ... } } -
Defaults.swift struct Defaults { struct Account : BoolDefaultSettable { ...
} struct Onboarding : BoolDefaultSettable { ... } } ... Defaults.Account.set(true, forKey: .isUserLoggedIn) Defaults.Onboarding.set(true, forKey: .isUserOnboarded) Defaults.Account.bool(forKey: .isUserLoggedIn) Defaults.Onboarding.bool(forKey: .isUserOnboarded) -
So what have we learned? • Stringly typed API's are
bad • Plenty of room for improvement -
So what have we learned? • Stringly typed API's are
bad • Plenty of room for improvement • Grouping constants can assist uniformity -
So what have we learned? • Stringly typed API's are
bad • Plenty of room for improvement • Grouping constants can assist uniformity • Namespacing gives context -
So what have we learned? • Stringly typed API's are
bad • Plenty of room for improvement • Grouping constants can assist uniformity • Namespacing gives context • Protocols are (so) hot right now -
So what have we learned? • Stringly typed API's are
bad • Plenty of room for improvement • Grouping constants can assist uniformity • Namespacing gives context • Protocols are (so) hot right now • Rethink how we interact with APIs -
SWIFT EYE FOR THE STRINGLY TYPED API Thank You Medium:
Andyy Hope Twitter: @andyyhope