Slide 1

Slide 1 text

Ragunath Jawahar Building Robust Software Small ideas, big impact

Slide 2

Slide 2 text

Write code that communicates well.

Slide 3

Slide 3 text

Example 1 API Call / External Systems

Slide 4

Slide 4 text

protocol AccountsApi { func signup(with email: String, and password: String) #-> Response? } AccountsApi.swift

Slide 5

Slide 5 text

protocol AccountsApi { func signup(with email: String, and password: String) #-> Response? } AccountsApi.swift

Slide 6

Slide 6 text

protocol AccountsApi { func signup(with email: String, and password: String) #-> Response? } AccountsApi.swift

Slide 7

Slide 7 text

protocol AccountsApi { func signup(with email: String, and password: String) #-> Response? } AccountsApi.swift

Slide 8

Slide 8 text

protocol AccountsApi { func signup(with email: String, and password: String) #-> Response? } AccountsApi.swift

Slide 9

Slide 9 text

protocol AccountsApi { func signup(with email: String, and password: String) #-> Response? } AccountsApi.swift

Slide 10

Slide 10 text

protocol AccountsApi { func signup(with email: String, and password: String) #-> Response? } AccountsApi.swift

Slide 11

Slide 11 text

protocol AccountsApi { func signup(with email: String, and password: String) #-> Response? } AccountsApi.swift

Slide 12

Slide 12 text

Response? • Successfully creates a user account and returns a Response 🎉 • nil • NSURLErrorCannotConnectToHost • NSURLErrorCannotFindHost • NSURLError* "

Slide 13

Slide 13 text

Response? • Successfully creates a user account and returns a Response 🎉 • nil • NSURLErrorCannotConnectToHost • NSURLErrorCannotFindHost • NSURLError* "

Slide 14

Slide 14 text

Response? • Successfully creates a user account and returns a Response 🎉 • nil • NSURLErrorCannotConnectToHost • NSURLErrorCannotFindHost • NSURLError* "

Slide 15

Slide 15 text

Response? • Successfully creates a user account and returns a Response 🎉 • nil • NSURLErrorCannotConnectToHost • NSURLErrorCannotFindHost • NSURLError* "

Slide 16

Slide 16 text

Response? • Successfully creates a user account and returns a Response 🎉 • nil • NSURLErrorCannotConnectToHost • NSURLErrorCannotFindHost • NSURLError* "

Slide 17

Slide 17 text

😊 😕 😨 Caller’s emotions

Slide 18

Slide 18 text

😊 😕 😨 Caller’s emotions

Slide 19

Slide 19 text

😊 😕 😨 Caller’s emotions

Slide 20

Slide 20 text

😊 😕 😨 Caller’s emotions

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

protocol AccountsApi { func signup(with email: String, and password: String) #-> Response? } AccountsApi.swift

Slide 25

Slide 25 text

protocol AccountsApi { func signup(with email: String, and password: String) #-> Response? } AccountsApi.swift

Slide 26

Slide 26 text

protocol AccountsApi { func signup(with email: String, and password: String) #-> Response } AccountsApi.swift

Slide 27

Slide 27 text

Response • Successfully creates a user account • Email already registered • Validation errors • Connection errors • 5xx server errors • Unknown errors

Slide 28

Slide 28 text

Response • Successfully creates a user account • Email already registered • Validation errors • Connection errors • 5xx server errors • Unknown errors

Slide 29

Slide 29 text

Response • Successfully creates a user account • Email already registered • Validation errors • Connection errors • 5xx server errors • Unknown errors

Slide 30

Slide 30 text

Response • Successfully creates a user account • Email already registered • Validation errors • Connection errors • 5xx server errors • Unknown errors

Slide 31

Slide 31 text

Response • Successfully creates a user account • Email already registered • Validation errors • Connection errors • 5xx server errors • Unknown errors

Slide 32

Slide 32 text

Response • Successfully creates a user account • Email already registered • Validation errors • Connection errors • 5xx server errors • Unknown errors

Slide 33

Slide 33 text

Response • Successfully creates a user account • Email already registered • Validation errors • Connection errors • 5xx server errors • Unknown errors

Slide 34

Slide 34 text

Response • Successfully creates a user account • Email already registered • Validation errors • Connection errors • 5xx server errors • Unknown errors

Slide 35

Slide 35 text

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.

Slide 36

Slide 36 text

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.

Slide 37

Slide 37 text

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.

Slide 38

Slide 38 text

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.

Slide 39

Slide 39 text

Response.swift enum Response { case accountCreated(String) case emailAlreadyRegistered(String) case InputValidationFailed([ValidationError]) case connectionError case serverError(Int) case unknownError(Int, String?) }

Slide 40

Slide 40 text

Response.swift enum Response { case accountCreated(String) case emailAlreadyRegistered(String) case InputValidationFailed([ValidationError]) case connectionError case serverError(Int) case unknownError(Int, String?) }

Slide 41

Slide 41 text

Response.swift enum Response { case accountCreated(String) case emailAlreadyRegistered(String) case InputValidationFailed([ValidationError]) case connectionError case serverError(Int) case unknownError(Int, String?) }

Slide 42

Slide 42 text

Response.swift enum Response { case accountCreated(String) case emailAlreadyRegistered(String) case InputValidationFailed([ValidationError]) case connectionError case serverError(Int) case unknownError(Int, String?) }

Slide 43

Slide 43 text

Response.swift enum Response { case accountCreated(String) case emailAlreadyRegistered(String) case InputValidationFailed([ValidationError]) case connectionError case serverError(Int) case unknownError(Int, String?) }

Slide 44

Slide 44 text

Response.swift enum Response { case accountCreated(String) case emailAlreadyRegistered(String) case InputValidationFailed([ValidationError]) case connectionError case serverError(Int) case unknownError(Int, String?) }

Slide 45

Slide 45 text

Response.swift enum Response { case accountCreated(String) case emailAlreadyRegistered(String) case InputValidationFailed([ValidationError]) case connectionError case serverError(Int) case unknownError(Int, String?) }

Slide 46

Slide 46 text

Response.swift enum Response { case accountCreated(String) case emailAlreadyRegistered(String) case InputValidationFailed([ValidationError]) case connectionError case serverError(Int) case unknownError(Int, String?) }

Slide 47

Slide 47 text

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() }

Slide 48

Slide 48 text

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() }

Slide 49

Slide 49 text

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() }

Slide 50

Slide 50 text

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() }

Slide 51

Slide 51 text

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() }

Slide 52

Slide 52 text

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() }

Slide 53

Slide 53 text

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() }

Slide 54

Slide 54 text

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() }

Slide 55

Slide 55 text

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() }

Slide 56

Slide 56 text

😊 😌 Caller’s emotions

Slide 57

Slide 57 text

😊 😌 Caller’s emotions

Slide 58

Slide 58 text

😊 😌 Caller’s emotions

Slide 59

Slide 59 text

What have we achieved? • Certainty • Convey business ideas in code • Improved readability • Represent errors as data

Slide 60

Slide 60 text

What have we learnt? • Total functions • Algebraic data types • Robustness principle

Slide 61

Slide 61 text

What have we learnt? • Total functions • Algebraic data types • Robustness principle

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

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.

Slide 64

Slide 64 text

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.

Slide 65

Slide 65 text

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.

Slide 66

Slide 66 text

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.

Slide 67

Slide 67 text

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.

Slide 68

Slide 68 text

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.

Slide 69

Slide 69 text

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.

Slide 70

Slide 70 text

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.

Slide 71

Slide 71 text

!// 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

Slide 72

Slide 72 text

!// 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

Slide 73

Slide 73 text

!// 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

Slide 74

Slide 74 text

!// 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

Slide 75

Slide 75 text

!// 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

Slide 76

Slide 76 text

!// 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

Slide 77

Slide 77 text

!// 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

Slide 78

Slide 78 text

What have we learnt? • Total functions • Algebraic data types • Robustness principle

Slide 79

Slide 79 text

What have we learnt? • Total functions • Algebraic data types • Robustness principle

Slide 80

Slide 80 text

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

Slide 81

Slide 81 text

Response.swift enum Response { case accountCreated(String) case emailAlreadyRegistered(String) case InputValidationFailed([ValidationError]) case connectionError case serverError(Int) case unknownError(Int, String?) }

Slide 82

Slide 82 text

Response.swift enum Response { case accountCreated(String) case emailAlreadyRegistered(String) case InputValidationFailed([ValidationError]) case connectionError case serverError(Int) case unknownError(Int, String?) }

Slide 83

Slide 83 text

Response.swift enum Response { case accountCreated(String) case emailAlreadyRegistered(String) case InputValidationFailed([ValidationError]) case connectionError case serverError(Int) case unknownError(Int, String?) }

Slide 84

Slide 84 text

Response.swift enum Response { case accountCreated(String) case emailAlreadyRegistered(String) case InputValidationFailed([ValidationError]) case connectionError case serverError(Int) case unknownError(Int, String?) }

Slide 85

Slide 85 text

What have we learnt? • Total functions • Algebraic data types • Robustness principle

Slide 86

Slide 86 text

What have we learnt? • Total functions • Algebraic data types • Robustness principle

Slide 87

Slide 87 text

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.)

Slide 88

Slide 88 text

Example 2 Division / Exceptions

Slide 89

Slide 89 text

func divide(dividend: Int, divisor: Int) #-> Int { return dividend / divisor } Arithmetic.swift

Slide 90

Slide 90 text

func divide(dividend: Int, divisor: Int) #-> Int { return dividend / divisor } Arithmetic.swift

Slide 91

Slide 91 text

func divide(dividend: Int, divisor: Int) #-> Int { return dividend / divisor } Arithmetic.swift

Slide 92

Slide 92 text

func divide(dividend: Int, divisor: Int) #-> Int { return dividend / divisor } Arithmetic.swift

Slide 93

Slide 93 text

func divide(dividend: Int, divisor: Int) #-> Int { return dividend / divisor } Arithmetic.swift

Slide 94

Slide 94 text

func divide(dividend: Int, divisor: Int) #-> Int { return dividend / divisor } Arithmetic.swift

Slide 95

Slide 95 text

func divide(dividend: Int, divisor: Int) #-> Int { return dividend / divisor } Arithmetic.swift

Slide 96

Slide 96 text

divisor = 0

Slide 97

Slide 97 text

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

Slide 98

Slide 98 text

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

Slide 99

Slide 99 text

func divide(dividend: Int, divisor: Int) #-> Quotient { return divisor #== 0 ? .infinity : .result(dividend / divisor) } Arithmetic.swift

Slide 100

Slide 100 text

func divide(dividend: Int, divisor: Int) #-> Quotient { return divisor #== 0 ? .infinity : .result(dividend / divisor) } Arithmetic.swift

Slide 101

Slide 101 text

func divide(dividend: Int, divisor: Int) #-> Quotient { return divisor #== 0 ? .infinity : .result(dividend / divisor) } Arithmetic.swift

Slide 102

Slide 102 text

func divide(dividend: Int, divisor: Int) #-> Quotient { return divisor #== 0 ? .infinity : .result(dividend / divisor) } Arithmetic.swift

Slide 103

Slide 103 text

func divide(dividend: Int, divisor: Int) #-> Quotient { return divisor #== 0 ? .infinity : .result(dividend / divisor) } Arithmetic.swift

Slide 104

Slide 104 text

func divide(dividend: Int, divisor: Int) #-> Quotient { return divisor #== 0 ? .infinity : .result(dividend / divisor) } Arithmetic.swift

Slide 105

Slide 105 text

func divide(dividend: Int, divisor: Int) #-> Quotient { return divisor #== 0 ? .infinity : .result(dividend / divisor) } Arithmetic.swift

Slide 106

Slide 106 text

func divide(dividend: Int, divisor: Int) #-> Quotient { return divisor #== 0 ? .infinity : .result(dividend / divisor) } Arithmetic.swift

Slide 107

Slide 107 text

func divide(dividend: Int, divisor: Int) #-> Quotient { return divisor #== 0 ? .infinity : .result(dividend / divisor) } Arithmetic.swift

Slide 108

Slide 108 text

func divide(dividend: Int, divisor: Int) #-> Quotient { return divisor #== 0 ? .infinity : .result(dividend / divisor) } Arithmetic.swift

Slide 109

Slide 109 text

func divide(dividend: Int, divisor: Int) #-> Quotient { return divisor #== 0 ? .infinity : .result(dividend / divisor) } Arithmetic.swift

Slide 110

Slide 110 text

enum Quotient { case .infinity case .result(Int) } Quotient.swift

Slide 111

Slide 111 text

enum Quotient { case .infinity case .result(Int) } Quotient.swift

Slide 112

Slide 112 text

enum Quotient { case .infinity case .result(Int) } Quotient.swift

Slide 113

Slide 113 text

enum Quotient { case .infinity case .result(Int) } Quotient.swift

Slide 114

Slide 114 text

enum Quotient { case .infinity case .result(Int) } Quotient.swift

Slide 115

Slide 115 text

🤔

Slide 116

Slide 116 text

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

Slide 117

Slide 117 text

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

Slide 118

Slide 118 text

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

Slide 119

Slide 119 text

func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int { return dividend / divisor.value } Arithmetic.swift

Slide 120

Slide 120 text

func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int { return dividend / divisor.value } Arithmetic.swift

Slide 121

Slide 121 text

func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int { return dividend / divisor.value } Arithmetic.swift

Slide 122

Slide 122 text

func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int { return dividend / divisor.value } Arithmetic.swift

Slide 123

Slide 123 text

func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int { return dividend / divisor.value } Arithmetic.swift

Slide 124

Slide 124 text

func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int { return dividend / divisor.value } Arithmetic.swift

Slide 125

Slide 125 text

func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int { return dividend / divisor.value } Arithmetic.swift

Slide 126

Slide 126 text

func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int { return dividend / divisor.value } Arithmetic.swift

Slide 127

Slide 127 text

func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int { return dividend / divisor.value } Arithmetic.swift

Slide 128

Slide 128 text

func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int { return dividend / divisor.value } Arithmetic.swift

Slide 129

Slide 129 text

class NaturalNumber { class Zero: NaturalNumber { !/* empty !*/ } class NonZero: NaturalNumber { let value: Int fileprivate init(_ value: Int) { self.value = value } } !//!!... } NaturalNumber.swift

Slide 130

Slide 130 text

class NaturalNumber { class Zero: NaturalNumber { !/* empty !*/ } class NonZero: NaturalNumber { let value: Int fileprivate init(_ value: Int) { self.value = value } } !//!!... } NaturalNumber.swift

Slide 131

Slide 131 text

class NaturalNumber { class Zero: NaturalNumber { !/* empty !*/ } class NonZero: NaturalNumber { let value: Int fileprivate init(_ value: Int) { self.value = value } } !//!!... } NaturalNumber.swift

Slide 132

Slide 132 text

class NaturalNumber { class Zero: NaturalNumber { !/* empty !*/ } class NonZero: NaturalNumber { let value: Int fileprivate init(_ value: Int) { self.value = value } } !//!!... } NaturalNumber.swift

Slide 133

Slide 133 text

class NaturalNumber { class Zero: NaturalNumber { !/* empty !*/ } class NonZero: NaturalNumber { let value: Int fileprivate init(_ value: Int) { self.value = value } } !//!!... } NaturalNumber.swift

Slide 134

Slide 134 text

class NaturalNumber { class Zero: NaturalNumber { !/* empty !*/ } class NonZero: NaturalNumber { let value: Int fileprivate init(_ value: Int) { self.value = value } } !//!!... } NaturalNumber.swift

Slide 135

Slide 135 text

class NaturalNumber { class Zero: NaturalNumber { !/* empty !*/ } class NonZero: NaturalNumber { let value: Int fileprivate init(_ value: Int) { self.value = value } } !//!!... } NaturalNumber.swift

Slide 136

Slide 136 text

class NaturalNumber { class Zero: NaturalNumber { !/* empty !*/ } class NonZero: NaturalNumber { let value: Int fileprivate init(_ value: Int) { self.value = value } } !//!!... } NaturalNumber.swift

Slide 137

Slide 137 text

class NaturalNumber { class Zero: NaturalNumber { !/* empty !*/ } class NonZero: NaturalNumber { let value: Int fileprivate init(_ value: Int) { self.value = value } } !//!!... } NaturalNumber.swift

Slide 138

Slide 138 text

class NaturalNumber { class Zero: NaturalNumber { !/* empty !*/ } class NonZero: NaturalNumber { let value: Int fileprivate init(_ value: Int) { self.value = value } } !//!!... } NaturalNumber.swift

Slide 139

Slide 139 text

class NaturalNumber { !//!!... class func from(_ value: Int) #-> NaturalNumber { return value #== 0 ? Zero() : NonZero(value) } } NaturalNumber.swift

Slide 140

Slide 140 text

class NaturalNumber { !//!!... class func from(_ value: Int) #-> NaturalNumber { return value #== 0 ? Zero() : NonZero(value) } } NaturalNumber.swift

Slide 141

Slide 141 text

class NaturalNumber { !//!!... class func from(_ value: Int) #-> NaturalNumber { return value #== 0 ? Zero() : NonZero(value) } } NaturalNumber.swift

Slide 142

Slide 142 text

class NaturalNumber { !//!!... class func from(_ value: Int) #-> NaturalNumber { return value #== 0 ? Zero() : NonZero(value) } } NaturalNumber.swift

Slide 143

Slide 143 text

class NaturalNumber { !//!!... class func from(_ value: Int) #-> NaturalNumber { return value #== 0 ? Zero() : NonZero(value) } } NaturalNumber.swift

Slide 144

Slide 144 text

class NaturalNumber { !//!!... class func from(_ value: Int) #-> NaturalNumber { return value #== 0 ? Zero() : NonZero(value) } } NaturalNumber.swift

Slide 145

Slide 145 text

let divisor = NaturalNumber.from(4) if (divisor is NaturalNumber.NonZero) { val quotient = divide(divisor: 8, divisor: divisor) !// Do fancy things! } else { !// Fail fast… } Usage

Slide 146

Slide 146 text

let divisor = NaturalNumber.from(4) if (divisor is NaturalNumber.NonZero) { val quotient = divide(divisor: 8, divisor: divisor) !// Do fancy things! } else { !// Fail fast… } Usage

Slide 147

Slide 147 text

let divisor = NaturalNumber.from(4) if (divisor is NaturalNumber.NonZero) { val quotient = divide(divisor: 8, divisor: divisor) !// Do fancy things! } else { !// Fail fast… } Usage

Slide 148

Slide 148 text

let divisor = NaturalNumber.from(4) if (divisor is NaturalNumber.NonZero) { val quotient = divide(divisor: 8, divisor: divisor) !// Do fancy things! } else { !// Fail fast… } Usage

Slide 149

Slide 149 text

let divisor = NaturalNumber.from(4) if (divisor is NaturalNumber.NonZero) { val quotient = divide(divisor: 8, divisor: divisor) !// Do fancy things! } else { !// Fail fast… } Usage

Slide 150

Slide 150 text

let divisor = NaturalNumber.from(4) if (divisor is NaturalNumber.NonZero) { val quotient = divide(divisor: 8, divisor: divisor) !// Do fancy things! } else { !// Fail fast… } Usage

Slide 151

Slide 151 text

let divisor = NaturalNumber.from(4) if (divisor is NaturalNumber.NonZero) { val quotient = divide(divisor: 8, divisor: divisor) !// Do fancy things! } else { !// Fail fast… } Usage

Slide 152

Slide 152 text

let divisor = NaturalNumber.from(4) if (divisor is NaturalNumber.NonZero) { val quotient = divide(divisor: 8, divisor: divisor) !// Do fancy things! } else { !// Fail fast… } Usage

Slide 153

Slide 153 text

let divisor = NaturalNumber.from(4) if (divisor is NaturalNumber.NonZero) { val quotient = divide(divisor: 8, divisor: divisor) !// Do fancy things! } else { !// Fail fast… } Usage

Slide 154

Slide 154 text

!// 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

Slide 155

Slide 155 text

!// 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

Slide 156

Slide 156 text

!// 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

Slide 157

Slide 157 text

!// 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

Slide 158

Slide 158 text

!// 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

Slide 159

Slide 159 text

!// 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

Slide 160

Slide 160 text

!// 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

Slide 161

Slide 161 text

!// 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

Slide 162

Slide 162 text

!// 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

Slide 163

Slide 163 text

!// 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

Slide 164

Slide 164 text

!// 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

Slide 165

Slide 165 text

!// 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

Slide 166

Slide 166 text

!// 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

Slide 167

Slide 167 text

What have we learnt? • Robustness principle (tweaked)

Slide 168

Slide 168 text

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

Slide 169

Slide 169 text

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

Slide 170

Slide 170 text

Summary • Robustness Principle • Total Functions • Algebraic Data Types • Robustness Principle (Tweaked)

Slide 171

Slide 171 text

Questions? @ragunathjawahar