Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building Robust Apps (Swift Edition)

Building Robust Apps (Swift Edition)

Ragunath Jawahar

October 10, 2020
Tweet

More Decks by Ragunath Jawahar

Other Decks in Programming

Transcript

  1. Ragunath Jawahar
    Building Robust Software
    Small ideas, big impact

    View Slide

  2. Write code that communicates well.

    View Slide

  3. Example 1
    API Call / External Systems

    View Slide

  4. protocol AccountsApi {

    func signup(with email: String, and password: String) #-> Response?

    }
    AccountsApi.swift

    View Slide

  5. protocol AccountsApi {

    func signup(with email: String, and password: String) #-> Response?

    }
    AccountsApi.swift

    View Slide

  6. protocol AccountsApi {

    func signup(with email: String, and password: String) #-> Response?

    }
    AccountsApi.swift

    View Slide

  7. protocol AccountsApi {

    func signup(with email: String, and password: String) #-> Response?

    }
    AccountsApi.swift

    View Slide

  8. protocol AccountsApi {

    func signup(with email: String, and password: String) #-> Response?

    }
    AccountsApi.swift

    View Slide

  9. protocol AccountsApi {

    func signup(with email: String, and password: String) #-> Response?

    }
    AccountsApi.swift

    View Slide

  10. protocol AccountsApi {

    func signup(with email: String, and password: String) #-> Response?

    }
    AccountsApi.swift

    View Slide

  11. protocol AccountsApi {

    func signup(with email: String, and password: String) #-> Response?

    }
    AccountsApi.swift

    View Slide

  12. Response?
    • Successfully creates a user account and returns a Response 🎉

    • nil

    • NSURLErrorCannotConnectToHost

    • NSURLErrorCannotFindHost

    • NSURLError* "

    View Slide

  13. Response?
    • Successfully creates a user account and returns a Response 🎉

    • nil

    • NSURLErrorCannotConnectToHost

    • NSURLErrorCannotFindHost

    • NSURLError* "

    View Slide

  14. Response?
    • Successfully creates a user account and returns a Response 🎉

    • nil

    • NSURLErrorCannotConnectToHost

    • NSURLErrorCannotFindHost

    • NSURLError* "

    View Slide

  15. Response?
    • Successfully creates a user account and returns a Response 🎉

    • nil

    • NSURLErrorCannotConnectToHost

    • NSURLErrorCannotFindHost

    • NSURLError* "

    View Slide

  16. Response?
    • Successfully creates a user account and returns a Response 🎉

    • nil

    • NSURLErrorCannotConnectToHost

    • NSURLErrorCannotFindHost

    • NSURLError* "

    View Slide

  17. 😊 😕 😨
    Caller’s emotions

    View Slide

  18. 😊 😕 😨
    Caller’s emotions

    View Slide

  19. 😊 😕 😨
    Caller’s emotions

    View Slide

  20. 😊 😕 😨
    Caller’s emotions

    View Slide

  21. Be conservative in what you send,
    be liberal in what you accept
    Robustness Principle / Postel’s Law

    View Slide

  22. Be conservative in what you send,
    be liberal in what you accept
    Robustness Principle / Postel’s Law

    View Slide

  23. Robustness Principle / Postel’s Law
    Be conservative in what you send,
    be liberal in what you accept

    View Slide

  24. protocol AccountsApi {

    func signup(with email: String, and password: String) #-> Response?

    }
    AccountsApi.swift

    View Slide

  25. protocol AccountsApi {

    func signup(with email: String, and password: String) #-> Response?

    }
    AccountsApi.swift

    View Slide

  26. protocol AccountsApi {

    func signup(with email: String, and password: String) #-> Response

    }
    AccountsApi.swift

    View Slide

  27. Response
    • Successfully creates a user account

    • Email already registered

    • Validation errors

    • Connection errors

    • 5xx server errors

    • Unknown errors

    View Slide

  28. Response
    • Successfully creates a user account

    • Email already registered

    • Validation errors

    • Connection errors

    • 5xx server errors

    • Unknown errors

    View Slide

  29. Response
    • Successfully creates a user account

    • Email already registered

    • Validation errors

    • Connection errors

    • 5xx server errors

    • Unknown errors

    View Slide

  30. Response
    • Successfully creates a user account

    • Email already registered

    • Validation errors

    • Connection errors

    • 5xx server errors

    • Unknown errors

    View Slide

  31. Response
    • Successfully creates a user account

    • Email already registered

    • Validation errors

    • Connection errors

    • 5xx server errors

    • Unknown errors

    View Slide

  32. Response
    • Successfully creates a user account

    • Email already registered

    • Validation errors

    • Connection errors

    • 5xx server errors

    • Unknown errors

    View Slide

  33. Response
    • Successfully creates a user account

    • Email already registered

    • Validation errors

    • Connection errors

    • 5xx server errors

    • Unknown errors

    View Slide

  34. Response
    • Successfully creates a user account

    • Email already registered

    • Validation errors

    • Connection errors

    • 5xx server errors

    • Unknown errors

    View Slide

  35. Enumerations
    An enumeration defines a common type for a group of related values
    and enables you to work with those values in a type-safe way within
    your code.

    Alternatively, enumeration cases can specify associated values of any
    type to be stored along with each different case value, much as unions
    or variants do in other languages.

    View Slide

  36. Enumerations
    An enumeration defines a common type for a group of related values
    and enables you to work with those values in a type-safe way within
    your code.

    Alternatively, enumeration cases can specify associated values of any
    type to be stored along with each different case value, much as unions
    or variants do in other languages.

    View Slide

  37. Enumerations
    An enumeration defines a common type for a group of related values
    and enables you to work with those values in a type-safe way within
    your code.

    Alternatively, enumeration cases can specify associated values of any
    type to be stored along with each different case value, much as unions
    or variants do in other languages.

    View Slide

  38. Enumerations
    An enumeration defines a common type for a group of related values
    and enables you to work with those values in a type-safe way within
    your code.

    Alternatively, enumeration cases can specify associated values of any
    type to be stored along with each different case value, much as unions
    or variants do in other languages.

    View Slide

  39. Response.swift
    enum Response {

    case accountCreated(String)

    case emailAlreadyRegistered(String)

    case InputValidationFailed([ValidationError])

    case connectionError

    case serverError(Int)

    case unknownError(Int, String?)

    }

    View Slide

  40. Response.swift
    enum Response {

    case accountCreated(String)

    case emailAlreadyRegistered(String)

    case InputValidationFailed([ValidationError])

    case connectionError

    case serverError(Int)

    case unknownError(Int, String?)

    }

    View Slide

  41. Response.swift
    enum Response {

    case accountCreated(String)

    case emailAlreadyRegistered(String)

    case InputValidationFailed([ValidationError])

    case connectionError

    case serverError(Int)

    case unknownError(Int, String?)

    }

    View Slide

  42. Response.swift
    enum Response {

    case accountCreated(String)

    case emailAlreadyRegistered(String)

    case InputValidationFailed([ValidationError])

    case connectionError

    case serverError(Int)

    case unknownError(Int, String?)

    }

    View Slide

  43. Response.swift
    enum Response {

    case accountCreated(String)

    case emailAlreadyRegistered(String)

    case InputValidationFailed([ValidationError])

    case connectionError

    case serverError(Int)

    case unknownError(Int, String?)

    }

    View Slide

  44. Response.swift
    enum Response {

    case accountCreated(String)

    case emailAlreadyRegistered(String)

    case InputValidationFailed([ValidationError])

    case connectionError

    case serverError(Int)

    case unknownError(Int, String?)

    }

    View Slide

  45. Response.swift
    enum Response {

    case accountCreated(String)

    case emailAlreadyRegistered(String)

    case InputValidationFailed([ValidationError])

    case connectionError

    case serverError(Int)

    case unknownError(Int, String?)

    }

    View Slide

  46. Response.swift
    enum Response {

    case accountCreated(String)

    case emailAlreadyRegistered(String)

    case InputValidationFailed([ValidationError])

    case connectionError

    case serverError(Int)

    case unknownError(Int, String?)

    }

    View Slide

  47. Usage - Enum Exhaustiveness
    val response = accountsApi.signup(with: "John Doe", and: "[email protected]")

    switch response {

    case .accountCreated(let userId):

    saveUserId(userId)

    case .emailAlreadyRegistered(let registrationEmail):

    showEmailAlreadyRegistered(registrationEmail)

    case .inputValidationFailed(let errors):

    showValidationErrors(errors)

    case .connectionError:

    showCheckConnectionMessage()

    case .serverError(_),

    .unknownError(_, _):

    showTryAgainInSometimeMessage()

    }

    View Slide

  48. Usage - Enum Exhaustiveness
    val response = accountsApi.signup(with: "John Doe", and: "[email protected]")

    switch response {

    case .accountCreated(let userId):

    saveUserId(userId)

    case .emailAlreadyRegistered(let registrationEmail):

    showEmailAlreadyRegistered(registrationEmail)

    case .inputValidationFailed(let errors):

    showValidationErrors(errors)

    case .connectionError:

    showCheckConnectionMessage()

    case .serverError(_),

    .unknownError(_, _):

    showTryAgainInSometimeMessage()

    }

    View Slide

  49. Usage - Enum Exhaustiveness
    val response = accountsApi.signup(with: "John Doe", and: "[email protected]")

    switch response {

    case .accountCreated(let userId):

    saveUserId(userId)

    case .emailAlreadyRegistered(let registrationEmail):

    showEmailAlreadyRegistered(registrationEmail)

    case .inputValidationFailed(let errors):

    showValidationErrors(errors)

    case .connectionError:

    showCheckConnectionMessage()

    case .serverError(_),

    .unknownError(_, _):

    showTryAgainInSometimeMessage()

    }

    View Slide

  50. Usage - Enum Exhaustiveness
    val response = accountsApi.signup(with: "John Doe", and: "[email protected]")

    switch response {

    case .accountCreated(let userId):

    saveUserId(userId)

    case .emailAlreadyRegistered(let registrationEmail):

    showEmailAlreadyRegistered(registrationEmail)

    case .inputValidationFailed(let errors):

    showValidationErrors(errors)

    case .connectionError:

    showCheckConnectionMessage()

    case .serverError(_),

    .unknownError(_, _):

    showTryAgainInSometimeMessage()

    }

    View Slide

  51. Usage - Enum Exhaustiveness
    val response = accountsApi.signup(with: "John Doe", and: "[email protected]")

    switch response {

    case .accountCreated(let userId):

    saveUserId(userId)

    case .emailAlreadyRegistered(let registrationEmail):

    showEmailAlreadyRegistered(registrationEmail)

    case .inputValidationFailed(let errors):

    showValidationErrors(errors)

    case .connectionError:

    showCheckConnectionMessage()

    case .serverError(_),

    .unknownError(_, _):

    showTryAgainInSometimeMessage()

    }

    View Slide

  52. Usage - Enum Exhaustiveness
    val response = accountsApi.signup(with: "John Doe", and: "[email protected]")

    switch response {

    case .accountCreated(let userId):

    saveUserId(userId)

    case .emailAlreadyRegistered(let registrationEmail):

    showEmailAlreadyRegistered(registrationEmail)

    case .inputValidationFailed(let errors):

    showValidationErrors(errors)

    case .connectionError:

    showCheckConnectionMessage()

    case .serverError(_),

    .unknownError(_, _):

    showTryAgainInSometimeMessage()

    }

    View Slide

  53. Usage - Enum Exhaustiveness
    val response = accountsApi.signup(with: "John Doe", and: "[email protected]")

    switch response {

    case .accountCreated(let userId):

    saveUserId(userId)

    case .emailAlreadyRegistered(let registrationEmail):

    showEmailAlreadyRegistered(registrationEmail)

    case .inputValidationFailed(let errors):

    showValidationErrors(errors)

    case .connectionError:

    showCheckConnectionMessage()

    case .serverError(_),

    .unknownError(_, _):

    showTryAgainInSometimeMessage()

    }

    View Slide

  54. Usage - Enum Exhaustiveness
    val response = accountsApi.signup(with: "John Doe", and: "[email protected]")

    switch response {

    case .accountCreated(let userId):

    saveUserId(userId)

    case .emailAlreadyRegistered(let registrationEmail):

    showEmailAlreadyRegistered(registrationEmail)

    case .inputValidationFailed(let errors):

    showValidationErrors(errors)

    case .connectionError:

    showCheckConnectionMessage()

    case .serverError(_),

    .unknownError(_, _):

    showTryAgainInSometimeMessage()

    }

    View Slide

  55. Usage - Enum Exhaustiveness
    val response = accountsApi.signup(with: "John Doe", and: "[email protected]")

    switch response {

    case .accountCreated(let userId):

    saveUserId(userId)

    case .emailAlreadyRegistered(let registrationEmail):

    showEmailAlreadyRegistered(registrationEmail)

    case .inputValidationFailed(let errors):

    showValidationErrors(errors)

    case .connectionError:

    showCheckConnectionMessage()

    case .serverError(_),

    .unknownError(_, _):

    showTryAgainInSometimeMessage()

    }

    View Slide

  56. 😊 😌
    Caller’s emotions

    View Slide

  57. 😊 😌
    Caller’s emotions

    View Slide

  58. 😊 😌
    Caller’s emotions

    View Slide

  59. What have we achieved?
    • Certainty

    • Convey business ideas in code

    • Improved readability

    • Represent errors as data

    View Slide

  60. What have we learnt?
    • Total functions

    • Algebraic data types

    • Robustness principle

    View Slide

  61. What have we learnt?
    • Total functions

    • Algebraic data types

    • Robustness principle

    View Slide

  62. Total Functions
    Total functions are functions that give you a valid return value for every
    combination of valid arguments.

    View Slide

  63. Partial Functions
    A partial function is a function that is not defined for all possible input
    values; in some cases returns a value, it may never return at all, throw
    an exception or simply crash a system.

    View Slide

  64. Partial Functions
    A partial function is a function that is not defined for all possible input
    values; in some cases returns a value, it may never return at all, throw
    an exception or simply crash a system.

    View Slide

  65. Partial Functions
    A partial function is a function that is not defined for all possible input
    values; in some cases returns a value, it may never return at all, throw
    an exception or simply crash a system.

    View Slide

  66. Partial Functions
    A partial function is a function that is not defined for all possible input
    values; in some cases returns a value, it may never return at all, throw
    an exception or simply crash a system.

    View Slide

  67. Partial Functions
    A partial function is a function that is not defined for all possible input
    values; in some cases returns a value, it may never return at all, throw
    an exception or simply crash a system.

    View Slide

  68. Partial Functions
    A partial function is a function that is not defined for all possible input
    values; in some cases returns a value, it may never return at all, throw
    an exception or simply crash a system.

    View Slide

  69. Partial Functions
    A partial function is a function that is not defined for all possible input
    values; in some cases returns a value, it may never return at all, throw
    an exception or simply crash a system.

    View Slide

  70. Partial Functions
    A partial function is a function that is not defined for all possible input
    values; in some cases returns a value, it may never return at all, throw
    an exception or simply crash a system.

    View Slide

  71. !// Partial function

    func signup(with email: String, and password: String) #-> Response?

    !// Total function

    func signup(with email: String, and password: String) #-> Response
    Partial vs. Total Functions

    View Slide

  72. !// Partial function

    func signup(with email: String, and password: String) #-> Response?

    !// Total function

    func signup(with email: String, and password: String) #-> Response
    Partial vs. Total Functions

    View Slide

  73. !// Partial function

    func signup(with email: String, and password: String) #-> Response?

    !// Total function

    func signup(with email: String, and password: String) #-> Response
    Partial vs. Total Functions

    View Slide

  74. !// Partial function

    func signup(with email: String, and password: String) #-> Response?

    !// Total function

    func signup(with email: String, and password: String) #-> Response
    Partial vs. Total Functions

    View Slide

  75. !// Partial function

    func signup(with email: String, and password: String) #-> Response?

    !// Total function

    func signup(with email: String, and password: String) #-> Response
    Partial vs. Total Functions

    View Slide

  76. !// Partial function

    func signup(with email: String, and password: String) #-> Response?

    !// Total function

    func signup(with email: String, and password: String) #-> Response
    Partial vs. Total Functions

    View Slide

  77. !// Partial function

    func signup(with email: String, and password: String) #-> Response?

    !// Total function

    func signup(with email: String, and password: String) #-> Response
    Partial vs. Total Functions

    View Slide

  78. What have we learnt?
    • Total functions

    • Algebraic data types

    • Robustness principle

    View Slide

  79. What have we learnt?
    • Total functions

    • Algebraic data types

    • Robustness principle

    View Slide

  80. Algebraic Data Types
    An algebraic data type is a kind of composite type, i.e., a type formed
    by combining other types.

    View Slide

  81. Response.swift
    enum Response {

    case accountCreated(String)

    case emailAlreadyRegistered(String)

    case InputValidationFailed([ValidationError])

    case connectionError

    case serverError(Int)

    case unknownError(Int, String?)

    }

    View Slide

  82. Response.swift
    enum Response {

    case accountCreated(String)

    case emailAlreadyRegistered(String)

    case InputValidationFailed([ValidationError])

    case connectionError

    case serverError(Int)

    case unknownError(Int, String?)

    }

    View Slide

  83. Response.swift
    enum Response {

    case accountCreated(String)

    case emailAlreadyRegistered(String)

    case InputValidationFailed([ValidationError])

    case connectionError

    case serverError(Int)

    case unknownError(Int, String?)

    }

    View Slide

  84. Response.swift
    enum Response {

    case accountCreated(String)

    case emailAlreadyRegistered(String)

    case InputValidationFailed([ValidationError])

    case connectionError

    case serverError(Int)

    case unknownError(Int, String?)

    }

    View Slide

  85. What have we learnt?
    • Total functions

    • Algebraic data types

    • Robustness principle

    View Slide

  86. What have we learnt?
    • Total functions

    • Algebraic data types

    • Robustness principle

    View Slide

  87. Where to apply?
    • Use it heavily on integration points with external systems (code that lives
    outside your own application)

    • Anti-corruption layers (both internal and external)

    • Take failure frequency into account (databases and file systems may not be
    good candidates. Network layer is a good starting point.)

    View Slide

  88. Example 2
    Division / Exceptions

    View Slide

  89. func divide(dividend: Int, divisor: Int) #-> Int {

    return dividend / divisor

    }
    Arithmetic.swift

    View Slide

  90. func divide(dividend: Int, divisor: Int) #-> Int {

    return dividend / divisor

    }
    Arithmetic.swift

    View Slide

  91. func divide(dividend: Int, divisor: Int) #-> Int {

    return dividend / divisor

    }
    Arithmetic.swift

    View Slide

  92. func divide(dividend: Int, divisor: Int) #-> Int {

    return dividend / divisor

    }
    Arithmetic.swift

    View Slide

  93. func divide(dividend: Int, divisor: Int) #-> Int {

    return dividend / divisor

    }
    Arithmetic.swift

    View Slide

  94. func divide(dividend: Int, divisor: Int) #-> Int {

    return dividend / divisor

    }
    Arithmetic.swift

    View Slide

  95. func divide(dividend: Int, divisor: Int) #-> Int {

    return dividend / divisor

    }
    Arithmetic.swift

    View Slide

  96. divisor = 0

    View Slide

  97. ERROR : divide by zero

    error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION
    (code=EXC_I386_INVOP, subcode=0x0).

    Fatal error: Division by zero: file Swift/x86_64-apple-ios-
    simulator.swiftinterface, line 32420

    View Slide

  98. Be conservative in what you send,
    be liberal in what you accept

    View Slide

  99. func divide(dividend: Int, divisor: Int) #-> Quotient {

    return divisor #== 0

    ? .infinity

    : .result(dividend / divisor)

    }
    Arithmetic.swift

    View Slide

  100. func divide(dividend: Int, divisor: Int) #-> Quotient {

    return divisor #== 0

    ? .infinity

    : .result(dividend / divisor)

    }
    Arithmetic.swift

    View Slide

  101. func divide(dividend: Int, divisor: Int) #-> Quotient {

    return divisor #== 0

    ? .infinity

    : .result(dividend / divisor)

    }
    Arithmetic.swift

    View Slide

  102. func divide(dividend: Int, divisor: Int) #-> Quotient {

    return divisor #== 0

    ? .infinity

    : .result(dividend / divisor)

    }
    Arithmetic.swift

    View Slide

  103. func divide(dividend: Int, divisor: Int) #-> Quotient {

    return divisor #== 0

    ? .infinity

    : .result(dividend / divisor)

    }
    Arithmetic.swift

    View Slide

  104. func divide(dividend: Int, divisor: Int) #-> Quotient {

    return divisor #== 0

    ? .infinity

    : .result(dividend / divisor)

    }
    Arithmetic.swift

    View Slide

  105. func divide(dividend: Int, divisor: Int) #-> Quotient {

    return divisor #== 0

    ? .infinity

    : .result(dividend / divisor)

    }
    Arithmetic.swift

    View Slide

  106. func divide(dividend: Int, divisor: Int) #-> Quotient {

    return divisor #== 0

    ? .infinity

    : .result(dividend / divisor)

    }
    Arithmetic.swift

    View Slide

  107. func divide(dividend: Int, divisor: Int) #-> Quotient {

    return divisor #== 0

    ? .infinity

    : .result(dividend / divisor)

    }
    Arithmetic.swift

    View Slide

  108. func divide(dividend: Int, divisor: Int) #-> Quotient {

    return divisor #== 0

    ? .infinity

    : .result(dividend / divisor)

    }
    Arithmetic.swift

    View Slide

  109. func divide(dividend: Int, divisor: Int) #-> Quotient {

    return divisor #== 0

    ? .infinity

    : .result(dividend / divisor)

    }
    Arithmetic.swift

    View Slide

  110. enum Quotient {

    case .infinity

    case .result(Int)

    }
    Quotient.swift

    View Slide

  111. enum Quotient {

    case .infinity

    case .result(Int)

    }
    Quotient.swift

    View Slide

  112. enum Quotient {

    case .infinity

    case .result(Int)

    }
    Quotient.swift

    View Slide

  113. enum Quotient {

    case .infinity

    case .result(Int)

    }
    Quotient.swift

    View Slide

  114. enum Quotient {

    case .infinity

    case .result(Int)

    }
    Quotient.swift

    View Slide

  115. 🤔

    View Slide

  116. Implications
    • Places burden on the consumer to handle both Infinity and
    Result types

    • The consumer may pass the Quotient type downstream. If that
    happens, downstream consumers should also deal with the added
    complexity

    View Slide

  117. Be conservative in what you send,
    be liberal in what you accept

    View Slide

  118. Be conservative in what you send,
    be liberal in what you accept
    conservative

    View Slide

  119. func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int {

    return dividend / divisor.value

    }
    Arithmetic.swift

    View Slide

  120. func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int {

    return dividend / divisor.value

    }
    Arithmetic.swift

    View Slide

  121. func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int {

    return dividend / divisor.value

    }
    Arithmetic.swift

    View Slide

  122. func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int {

    return dividend / divisor.value

    }
    Arithmetic.swift

    View Slide

  123. func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int {

    return dividend / divisor.value

    }
    Arithmetic.swift

    View Slide

  124. func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int {

    return dividend / divisor.value

    }
    Arithmetic.swift

    View Slide

  125. func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int {

    return dividend / divisor.value

    }
    Arithmetic.swift

    View Slide

  126. func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int {

    return dividend / divisor.value

    }
    Arithmetic.swift

    View Slide

  127. func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int {

    return dividend / divisor.value

    }
    Arithmetic.swift

    View Slide

  128. func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int {

    return dividend / divisor.value

    }
    Arithmetic.swift

    View Slide

  129. class NaturalNumber {

    class Zero: NaturalNumber { !/* empty !*/ }

    class NonZero: NaturalNumber {

    let value: Int

    fileprivate init(_ value: Int) {

    self.value = value
    }
    }

    !//!!...

    }
    NaturalNumber.swift

    View Slide

  130. class NaturalNumber {

    class Zero: NaturalNumber { !/* empty !*/ }

    class NonZero: NaturalNumber {

    let value: Int

    fileprivate init(_ value: Int) {

    self.value = value
    }
    }

    !//!!...

    }
    NaturalNumber.swift

    View Slide

  131. class NaturalNumber {

    class Zero: NaturalNumber { !/* empty !*/ }

    class NonZero: NaturalNumber {

    let value: Int

    fileprivate init(_ value: Int) {

    self.value = value
    }
    }

    !//!!...

    }
    NaturalNumber.swift

    View Slide

  132. class NaturalNumber {

    class Zero: NaturalNumber { !/* empty !*/ }

    class NonZero: NaturalNumber {

    let value: Int

    fileprivate init(_ value: Int) {

    self.value = value
    }
    }

    !//!!...

    }
    NaturalNumber.swift

    View Slide

  133. class NaturalNumber {

    class Zero: NaturalNumber { !/* empty !*/ }

    class NonZero: NaturalNumber {

    let value: Int

    fileprivate init(_ value: Int) {

    self.value = value
    }
    }

    !//!!...

    }
    NaturalNumber.swift

    View Slide

  134. class NaturalNumber {

    class Zero: NaturalNumber { !/* empty !*/ }

    class NonZero: NaturalNumber {

    let value: Int

    fileprivate init(_ value: Int) {

    self.value = value
    }
    }

    !//!!...

    }
    NaturalNumber.swift

    View Slide

  135. class NaturalNumber {

    class Zero: NaturalNumber { !/* empty !*/ }

    class NonZero: NaturalNumber {

    let value: Int

    fileprivate init(_ value: Int) {

    self.value = value
    }
    }

    !//!!...

    }
    NaturalNumber.swift

    View Slide

  136. class NaturalNumber {

    class Zero: NaturalNumber { !/* empty !*/ }

    class NonZero: NaturalNumber {

    let value: Int

    fileprivate init(_ value: Int) {

    self.value = value
    }
    }

    !//!!...

    }
    NaturalNumber.swift

    View Slide

  137. class NaturalNumber {

    class Zero: NaturalNumber { !/* empty !*/ }

    class NonZero: NaturalNumber {

    let value: Int

    fileprivate init(_ value: Int) {

    self.value = value
    }
    }

    !//!!...

    }
    NaturalNumber.swift

    View Slide

  138. class NaturalNumber {

    class Zero: NaturalNumber { !/* empty !*/ }

    class NonZero: NaturalNumber {

    let value: Int

    fileprivate init(_ value: Int) {

    self.value = value
    }
    }

    !//!!...

    }
    NaturalNumber.swift

    View Slide

  139. class NaturalNumber {

    !//!!...

    class func from(_ value: Int) #-> NaturalNumber {

    return value #== 0

    ? Zero()

    : NonZero(value)

    }

    }
    NaturalNumber.swift

    View Slide

  140. class NaturalNumber {

    !//!!...

    class func from(_ value: Int) #-> NaturalNumber {

    return value #== 0

    ? Zero()

    : NonZero(value)

    }

    }
    NaturalNumber.swift

    View Slide

  141. class NaturalNumber {

    !//!!...

    class func from(_ value: Int) #-> NaturalNumber {

    return value #== 0

    ? Zero()

    : NonZero(value)

    }

    }
    NaturalNumber.swift

    View Slide

  142. class NaturalNumber {

    !//!!...

    class func from(_ value: Int) #-> NaturalNumber {

    return value #== 0

    ? Zero()

    : NonZero(value)

    }

    }
    NaturalNumber.swift

    View Slide

  143. class NaturalNumber {

    !//!!...

    class func from(_ value: Int) #-> NaturalNumber {

    return value #== 0

    ? Zero()

    : NonZero(value)

    }

    }
    NaturalNumber.swift

    View Slide

  144. class NaturalNumber {

    !//!!...

    class func from(_ value: Int) #-> NaturalNumber {

    return value #== 0

    ? Zero()

    : NonZero(value)

    }

    }
    NaturalNumber.swift

    View Slide

  145. let divisor = NaturalNumber.from(4)

    if (divisor is NaturalNumber.NonZero) {

    val quotient = divide(divisor: 8, divisor: divisor)

    !// Do fancy things!

    } else {

    !// Fail fast…

    }
    Usage

    View Slide

  146. let divisor = NaturalNumber.from(4)

    if (divisor is NaturalNumber.NonZero) {

    val quotient = divide(divisor: 8, divisor: divisor)

    !// Do fancy things!

    } else {

    !// Fail fast…

    }
    Usage

    View Slide

  147. let divisor = NaturalNumber.from(4)

    if (divisor is NaturalNumber.NonZero) {

    val quotient = divide(divisor: 8, divisor: divisor)

    !// Do fancy things!

    } else {

    !// Fail fast…

    }
    Usage

    View Slide

  148. let divisor = NaturalNumber.from(4)

    if (divisor is NaturalNumber.NonZero) {

    val quotient = divide(divisor: 8, divisor: divisor)

    !// Do fancy things!

    } else {

    !// Fail fast…

    }
    Usage

    View Slide

  149. let divisor = NaturalNumber.from(4)

    if (divisor is NaturalNumber.NonZero) {

    val quotient = divide(divisor: 8, divisor: divisor)

    !// Do fancy things!

    } else {

    !// Fail fast…

    }
    Usage

    View Slide

  150. let divisor = NaturalNumber.from(4)

    if (divisor is NaturalNumber.NonZero) {

    val quotient = divide(divisor: 8, divisor: divisor)

    !// Do fancy things!

    } else {

    !// Fail fast…

    }
    Usage

    View Slide

  151. let divisor = NaturalNumber.from(4)

    if (divisor is NaturalNumber.NonZero) {

    val quotient = divide(divisor: 8, divisor: divisor)

    !// Do fancy things!

    } else {

    !// Fail fast…

    }
    Usage

    View Slide

  152. let divisor = NaturalNumber.from(4)

    if (divisor is NaturalNumber.NonZero) {

    val quotient = divide(divisor: 8, divisor: divisor)

    !// Do fancy things!

    } else {

    !// Fail fast…

    }
    Usage

    View Slide

  153. let divisor = NaturalNumber.from(4)

    if (divisor is NaturalNumber.NonZero) {

    val quotient = divide(divisor: 8, divisor: divisor)

    !// Do fancy things!

    } else {

    !// Fail fast…

    }
    Usage

    View Slide

  154. !// Liberal inputs, conservative output (original)

    fun divide(dividend: Int, divisor: Int) #-> Quotient

    !// Conservative inputs, conservative output (tweaked)

    fun divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int
    Original vs. Tweaked

    View Slide

  155. !// Liberal inputs, conservative output (original)

    fun divide(dividend: Int, divisor: Int) #-> Quotient

    !// Conservative inputs, conservative output (tweaked)

    fun divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int
    Original vs. Tweaked

    View Slide

  156. !// Liberal inputs, conservative output (original)

    fun divide(dividend: Int, divisor: Int) #-> Quotient

    !// Conservative inputs, conservative output (tweaked)

    fun divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int
    Original vs. Tweaked

    View Slide

  157. !// Liberal inputs, conservative output (original)

    fun divide(dividend: Int, divisor: Int) #-> Quotient

    !// Conservative inputs, conservative output (tweaked)

    fun divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int
    Original vs. Tweaked

    View Slide

  158. !// Liberal inputs, conservative output (original)

    fun divide(dividend: Int, divisor: Int) #-> Quotient

    !// Conservative inputs, conservative output (tweaked)

    fun divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int
    Original vs. Tweaked

    View Slide

  159. !// Liberal inputs, conservative output (original)

    fun divide(dividend: Int, divisor: Int) #-> Quotient

    !// Conservative inputs, conservative output (tweaked)

    fun divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int
    Original vs. Tweaked

    View Slide

  160. !// Liberal inputs, conservative output (original)

    fun divide(dividend: Int, divisor: Int) #-> Quotient

    !// Conservative inputs, conservative output (tweaked)

    fun divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int
    Original vs. Tweaked

    View Slide

  161. !// Liberal inputs, conservative output (original)

    fun divide(dividend: Int, divisor: Int) #-> Quotient

    !// Conservative inputs, conservative output (tweaked)

    fun divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int
    Original vs. Tweaked

    View Slide

  162. !// Liberal inputs, conservative output (original)

    fun divide(dividend: Int, divisor: Int) #-> Quotient

    !// Conservative inputs, conservative output (tweaked)

    fun divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int
    Original vs. Tweaked

    View Slide

  163. !// Liberal inputs, conservative output (original)

    fun divide(dividend: Int, divisor: Int) #-> Quotient

    !// Conservative inputs, conservative output (tweaked)

    fun divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int
    Original vs. Tweaked

    View Slide

  164. !// Liberal inputs, conservative output (original)

    fun divide(dividend: Int, divisor: Int) #-> Quotient

    !// Conservative inputs, conservative output (tweaked)

    fun divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int
    Original vs. Tweaked

    View Slide

  165. !// Liberal inputs, conservative output (original)

    fun divide(dividend: Int, divisor: Int) #-> Quotient

    !// Conservative inputs, conservative output (tweaked)

    fun divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int
    Original vs. Tweaked

    View Slide

  166. !// Liberal inputs, conservative output (original)

    fun divide(dividend: Int, divisor: Int) #-> Quotient

    !// Conservative inputs, conservative output (tweaked)

    fun divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int
    Original vs. Tweaked

    View Slide

  167. What have we learnt?
    • Robustness principle (tweaked)

    View Slide

  168. Where to apply?
    • All layers and components that are within your system boundary

    View Slide

  169. Impact on Overall System Design
    • Enhances certainty on micro and macroscopic levels

    • Errors are treated as first-class citizens

    • Reduced range of motion for your data from its point of origin

    • Easy to move towards a functional core and an imperative shell style of architectural pattern

    • Focus moves towards data over actors

    • Promotes value boundaries over call boundaries

    • Effective cure against primitive obsession

    • Reduces defensive programming

    • Promotes property testing over behaviour testing

    View Slide

  170. Summary
    • Robustness Principle

    • Total Functions

    • Algebraic Data Types

    • Robustness Principle (Tweaked)

    View Slide

  171. Questions?
    @ragunathjawahar

    View Slide