Share
Embed
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
171 slides
-
protocol AccountsApi { func signup(with email: String, and password: String)
#-> Response? } AccountsApi.swift -
protocol AccountsApi { func signup(with email: String, and password: String)
#-> Response? } AccountsApi.swift -
protocol AccountsApi { func signup(with email: String, and password: String)
#-> Response? } AccountsApi.swift -
protocol AccountsApi { func signup(with email: String, and password: String)
#-> Response? } AccountsApi.swift -
protocol AccountsApi { func signup(with email: String, and password: String)
#-> Response? } AccountsApi.swift -
protocol AccountsApi { func signup(with email: String, and password: String)
#-> Response? } AccountsApi.swift -
protocol AccountsApi { func signup(with email: String, and password: String)
#-> Response? } AccountsApi.swift -
protocol AccountsApi { func signup(with email: String, and password: String)
#-> Response? } AccountsApi.swift -
Response? • Successfully creates a user account and returns a
Response 🎉 • nil • NSURLErrorCannotConnectToHost • NSURLErrorCannotFindHost • NSURLError* " -
Response? • Successfully creates a user account and returns a
Response 🎉 • nil • NSURLErrorCannotConnectToHost • NSURLErrorCannotFindHost • NSURLError* " -
Response? • Successfully creates a user account and returns a
Response 🎉 • nil • NSURLErrorCannotConnectToHost • NSURLErrorCannotFindHost • NSURLError* " -
Response? • Successfully creates a user account and returns a
Response 🎉 • nil • NSURLErrorCannotConnectToHost • NSURLErrorCannotFindHost • NSURLError* " -
Response? • Successfully creates a user account and returns a
Response 🎉 • nil • NSURLErrorCannotConnectToHost • NSURLErrorCannotFindHost • NSURLError* " -
Be conservative in what you send, be liberal in what
you accept Robustness Principle / Postel’s Law -
Be conservative in what you send, be liberal in what
you accept Robustness Principle / Postel’s Law -
Robustness Principle / Postel’s Law Be conservative in what you
send, be liberal in what you accept -
protocol AccountsApi { func signup(with email: String, and password: String)
#-> Response? } AccountsApi.swift -
protocol AccountsApi { func signup(with email: String, and password: String)
#-> Response? } AccountsApi.swift -
protocol AccountsApi { func signup(with email: String, and password: String)
#-> Response } AccountsApi.swift -
Response • Successfully creates a user account • Email already
registered • Validation errors • Connection errors • 5xx server errors • Unknown errors -
Response • Successfully creates a user account • Email already
registered • Validation errors • Connection errors • 5xx server errors • Unknown errors -
Response • Successfully creates a user account • Email already
registered • Validation errors • Connection errors • 5xx server errors • Unknown errors -
Response • Successfully creates a user account • Email already
registered • Validation errors • Connection errors • 5xx server errors • Unknown errors -
Response • Successfully creates a user account • Email already
registered • Validation errors • Connection errors • 5xx server errors • Unknown errors -
Response • Successfully creates a user account • Email already
registered • Validation errors • Connection errors • 5xx server errors • Unknown errors -
Response • Successfully creates a user account • Email already
registered • Validation errors • Connection errors • 5xx server errors • Unknown errors -
Response • Successfully creates a user account • Email already
registered • Validation errors • Connection errors • 5xx server errors • Unknown errors -
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. -
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. -
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. -
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. -
Response.swift enum Response { case accountCreated(String) case emailAlreadyRegistered(String) case InputValidationFailed([ValidationError])
case connectionError case serverError(Int) case unknownError(Int, String?) } -
Response.swift enum Response { case accountCreated(String) case emailAlreadyRegistered(String) case InputValidationFailed([ValidationError])
case connectionError case serverError(Int) case unknownError(Int, String?) } -
Response.swift enum Response { case accountCreated(String) case emailAlreadyRegistered(String) case InputValidationFailed([ValidationError])
case connectionError case serverError(Int) case unknownError(Int, String?) } -
Response.swift enum Response { case accountCreated(String) case emailAlreadyRegistered(String) case InputValidationFailed([ValidationError])
case connectionError case serverError(Int) case unknownError(Int, String?) } -
Response.swift enum Response { case accountCreated(String) case emailAlreadyRegistered(String) case InputValidationFailed([ValidationError])
case connectionError case serverError(Int) case unknownError(Int, String?) } -
Response.swift enum Response { case accountCreated(String) case emailAlreadyRegistered(String) case InputValidationFailed([ValidationError])
case connectionError case serverError(Int) case unknownError(Int, String?) } -
Response.swift enum Response { case accountCreated(String) case emailAlreadyRegistered(String) case InputValidationFailed([ValidationError])
case connectionError case serverError(Int) case unknownError(Int, String?) } -
Response.swift enum Response { case accountCreated(String) case emailAlreadyRegistered(String) case InputValidationFailed([ValidationError])
case connectionError case serverError(Int) case unknownError(Int, String?) } -
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() } -
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() } -
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() } -
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() } -
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() } -
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() } -
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() } -
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() } -
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() } -
What have we achieved? • Certainty • Convey business ideas
in code • Improved readability • Represent errors as data -
What have we learnt? • Total functions • Algebraic data
types • Robustness principle -
What have we learnt? • Total functions • Algebraic data
types • Robustness principle -
Total Functions Total functions are functions that give you a
valid return value for every combination of valid arguments. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
!// 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 -
!// 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 -
!// 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 -
!// 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 -
!// 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 -
!// 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 -
!// 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 -
What have we learnt? • Total functions • Algebraic data
types • Robustness principle -
What have we learnt? • Total functions • Algebraic data
types • Robustness principle -
Algebraic Data Types An algebraic data type is a kind
of composite type, i.e., a type formed by combining other types. -
Response.swift enum Response { case accountCreated(String) case emailAlreadyRegistered(String) case InputValidationFailed([ValidationError])
case connectionError case serverError(Int) case unknownError(Int, String?) } -
Response.swift enum Response { case accountCreated(String) case emailAlreadyRegistered(String) case InputValidationFailed([ValidationError])
case connectionError case serverError(Int) case unknownError(Int, String?) } -
Response.swift enum Response { case accountCreated(String) case emailAlreadyRegistered(String) case InputValidationFailed([ValidationError])
case connectionError case serverError(Int) case unknownError(Int, String?) } -
Response.swift enum Response { case accountCreated(String) case emailAlreadyRegistered(String) case InputValidationFailed([ValidationError])
case connectionError case serverError(Int) case unknownError(Int, String?) } -
What have we learnt? • Total functions • Algebraic data
types • Robustness principle -
What have we learnt? • Total functions • Algebraic data
types • Robustness principle -
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.) -
func divide(dividend: Int, divisor: Int) #-> Int { return dividend
/ divisor } Arithmetic.swift -
func divide(dividend: Int, divisor: Int) #-> Int { return dividend
/ divisor } Arithmetic.swift -
func divide(dividend: Int, divisor: Int) #-> Int { return dividend
/ divisor } Arithmetic.swift -
func divide(dividend: Int, divisor: Int) #-> Int { return dividend
/ divisor } Arithmetic.swift -
func divide(dividend: Int, divisor: Int) #-> Int { return dividend
/ divisor } Arithmetic.swift -
func divide(dividend: Int, divisor: Int) #-> Int { return dividend
/ divisor } Arithmetic.swift -
func divide(dividend: Int, divisor: Int) #-> Int { return dividend
/ divisor } Arithmetic.swift -
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 -
func divide(dividend: Int, divisor: Int) #-> Quotient { return divisor
#== 0 ? .infinity : .result(dividend / divisor) } Arithmetic.swift -
func divide(dividend: Int, divisor: Int) #-> Quotient { return divisor
#== 0 ? .infinity : .result(dividend / divisor) } Arithmetic.swift -
func divide(dividend: Int, divisor: Int) #-> Quotient { return divisor
#== 0 ? .infinity : .result(dividend / divisor) } Arithmetic.swift -
func divide(dividend: Int, divisor: Int) #-> Quotient { return divisor
#== 0 ? .infinity : .result(dividend / divisor) } Arithmetic.swift -
func divide(dividend: Int, divisor: Int) #-> Quotient { return divisor
#== 0 ? .infinity : .result(dividend / divisor) } Arithmetic.swift -
func divide(dividend: Int, divisor: Int) #-> Quotient { return divisor
#== 0 ? .infinity : .result(dividend / divisor) } Arithmetic.swift -
func divide(dividend: Int, divisor: Int) #-> Quotient { return divisor
#== 0 ? .infinity : .result(dividend / divisor) } Arithmetic.swift -
func divide(dividend: Int, divisor: Int) #-> Quotient { return divisor
#== 0 ? .infinity : .result(dividend / divisor) } Arithmetic.swift -
func divide(dividend: Int, divisor: Int) #-> Quotient { return divisor
#== 0 ? .infinity : .result(dividend / divisor) } Arithmetic.swift -
func divide(dividend: Int, divisor: Int) #-> Quotient { return divisor
#== 0 ? .infinity : .result(dividend / divisor) } Arithmetic.swift -
func divide(dividend: Int, divisor: Int) #-> Quotient { return divisor
#== 0 ? .infinity : .result(dividend / divisor) } Arithmetic.swift -
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 -
Be conservative in what you send, be liberal in what
you accept conservative -
func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int { return dividend
/ divisor.value } Arithmetic.swift -
func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int { return dividend
/ divisor.value } Arithmetic.swift -
func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int { return dividend
/ divisor.value } Arithmetic.swift -
func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int { return dividend
/ divisor.value } Arithmetic.swift -
func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int { return dividend
/ divisor.value } Arithmetic.swift -
func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int { return dividend
/ divisor.value } Arithmetic.swift -
func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int { return dividend
/ divisor.value } Arithmetic.swift -
func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int { return dividend
/ divisor.value } Arithmetic.swift -
func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int { return dividend
/ divisor.value } Arithmetic.swift -
func divide(dividend: Int, divisor: NaturalNumber.NonZero) #-> Int { return dividend
/ divisor.value } Arithmetic.swift -
class NaturalNumber { class Zero: NaturalNumber { !/* empty !*/
} class NonZero: NaturalNumber { let value: Int fileprivate init(_ value: Int) { self.value = value } } !//!!... } NaturalNumber.swift -
class NaturalNumber { class Zero: NaturalNumber { !/* empty !*/
} class NonZero: NaturalNumber { let value: Int fileprivate init(_ value: Int) { self.value = value } } !//!!... } NaturalNumber.swift -
class NaturalNumber { class Zero: NaturalNumber { !/* empty !*/
} class NonZero: NaturalNumber { let value: Int fileprivate init(_ value: Int) { self.value = value } } !//!!... } NaturalNumber.swift -
class NaturalNumber { class Zero: NaturalNumber { !/* empty !*/
} class NonZero: NaturalNumber { let value: Int fileprivate init(_ value: Int) { self.value = value } } !//!!... } NaturalNumber.swift -
class NaturalNumber { class Zero: NaturalNumber { !/* empty !*/
} class NonZero: NaturalNumber { let value: Int fileprivate init(_ value: Int) { self.value = value } } !//!!... } NaturalNumber.swift -
class NaturalNumber { class Zero: NaturalNumber { !/* empty !*/
} class NonZero: NaturalNumber { let value: Int fileprivate init(_ value: Int) { self.value = value } } !//!!... } NaturalNumber.swift -
class NaturalNumber { class Zero: NaturalNumber { !/* empty !*/
} class NonZero: NaturalNumber { let value: Int fileprivate init(_ value: Int) { self.value = value } } !//!!... } NaturalNumber.swift -
class NaturalNumber { class Zero: NaturalNumber { !/* empty !*/
} class NonZero: NaturalNumber { let value: Int fileprivate init(_ value: Int) { self.value = value } } !//!!... } NaturalNumber.swift -
class NaturalNumber { class Zero: NaturalNumber { !/* empty !*/
} class NonZero: NaturalNumber { let value: Int fileprivate init(_ value: Int) { self.value = value } } !//!!... } NaturalNumber.swift -
class NaturalNumber { class Zero: NaturalNumber { !/* empty !*/
} class NonZero: NaturalNumber { let value: Int fileprivate init(_ value: Int) { self.value = value } } !//!!... } NaturalNumber.swift -
class NaturalNumber { !//!!... class func from(_ value: Int) #->
NaturalNumber { return value #== 0 ? Zero() : NonZero(value) } } NaturalNumber.swift -
class NaturalNumber { !//!!... class func from(_ value: Int) #->
NaturalNumber { return value #== 0 ? Zero() : NonZero(value) } } NaturalNumber.swift -
class NaturalNumber { !//!!... class func from(_ value: Int) #->
NaturalNumber { return value #== 0 ? Zero() : NonZero(value) } } NaturalNumber.swift -
class NaturalNumber { !//!!... class func from(_ value: Int) #->
NaturalNumber { return value #== 0 ? Zero() : NonZero(value) } } NaturalNumber.swift -
class NaturalNumber { !//!!... class func from(_ value: Int) #->
NaturalNumber { return value #== 0 ? Zero() : NonZero(value) } } NaturalNumber.swift -
class NaturalNumber { !//!!... class func from(_ value: Int) #->
NaturalNumber { return value #== 0 ? Zero() : NonZero(value) } } NaturalNumber.swift -
let divisor = NaturalNumber.from(4) if (divisor is NaturalNumber.NonZero) { val
quotient = divide(divisor: 8, divisor: divisor) !// Do fancy things! } else { !// Fail fast… } Usage -
let divisor = NaturalNumber.from(4) if (divisor is NaturalNumber.NonZero) { val
quotient = divide(divisor: 8, divisor: divisor) !// Do fancy things! } else { !// Fail fast… } Usage -
let divisor = NaturalNumber.from(4) if (divisor is NaturalNumber.NonZero) { val
quotient = divide(divisor: 8, divisor: divisor) !// Do fancy things! } else { !// Fail fast… } Usage -
let divisor = NaturalNumber.from(4) if (divisor is NaturalNumber.NonZero) { val
quotient = divide(divisor: 8, divisor: divisor) !// Do fancy things! } else { !// Fail fast… } Usage -
let divisor = NaturalNumber.from(4) if (divisor is NaturalNumber.NonZero) { val
quotient = divide(divisor: 8, divisor: divisor) !// Do fancy things! } else { !// Fail fast… } Usage -
let divisor = NaturalNumber.from(4) if (divisor is NaturalNumber.NonZero) { val
quotient = divide(divisor: 8, divisor: divisor) !// Do fancy things! } else { !// Fail fast… } Usage -
let divisor = NaturalNumber.from(4) if (divisor is NaturalNumber.NonZero) { val
quotient = divide(divisor: 8, divisor: divisor) !// Do fancy things! } else { !// Fail fast… } Usage -
let divisor = NaturalNumber.from(4) if (divisor is NaturalNumber.NonZero) { val
quotient = divide(divisor: 8, divisor: divisor) !// Do fancy things! } else { !// Fail fast… } Usage -
let divisor = NaturalNumber.from(4) if (divisor is NaturalNumber.NonZero) { val
quotient = divide(divisor: 8, divisor: divisor) !// Do fancy things! } else { !// Fail fast… } Usage -
!// 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 -
!// 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 -
!// 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 -
!// 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 -
!// 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 -
!// 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 -
!// 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 -
!// 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 -
!// 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 -
!// 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 -
!// 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 -
!// 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 -
!// 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 -
Where to apply? • All layers and components that are
within your system boundary -
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 -
Summary • Robustness Principle • Total Functions • Algebraic Data
Types • Robustness Principle (Tweaked)