Slide 1

Slide 1 text

The 4 Rules of Simple Design and Category Theory The 4 Rules of Simple Design and Category Theory Uberto Barbini @ramtop

Slide 2

Slide 2 text

“What is the relation between Category Theory and Code Quality? They seem two completely different things to me” Marcus Biel @MarcusBiel

Slide 3

Slide 3 text

Object-Oriented Functional TL;DR

Slide 4

Slide 4 text

Code Quality and Categories: Category Theory helps us To improve our functional design

Slide 5

Slide 5 text

Code Quality and Categories Why Functional Programming? How Categories can help?

Slide 6

Slide 6 text

The Alternative

Slide 7

Slide 7 text

Kent Beck’s Four Rules of Simple Design: Passes the tests (It works) Reveals intention (Easy to read and understand) No duplication (DRY: Don’t Repeat Yourself) Fewest elements (remove anything that doesn't serve the three previous rules)

Slide 8

Slide 8 text

Functional Programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. Wikipedia

Slide 9

Slide 9 text

Alan Kay John McCarthy Smalltalk Lisp Conversations Transformations

Slide 10

Slide 10 text

A Story of Two Books

Slide 11

Slide 11 text

Category Tinted Glasses

Slide 12

Slide 12 text

The proof of the pudding is in the eating

Slide 13

Slide 13 text

The proof of the Functional Programmer is in the coding

Slide 14

Slide 14 text

What we need is: Higher Order Functions Immutable Data Lazyness

Slide 15

Slide 15 text

But at the end… it’s all about morphisms “If I haven’t convinced you yet that category theory is all about morphisms then I haven’t done my job properly.” Bartosz Milewski https://bartoszmilewski.com/2015/11/17/its-all-about-morphisms/

Slide 16

Slide 16 text

Morphism examples in code Date → String User → Date String → Int (User → Date) → (User → String)

Slide 17

Slide 17 text

Morphism examples in code Date → String dateFormat() User → Date getBirthday() String → Int length() (User → Date) → (User → String)

Slide 18

Slide 18 text

What is a Category

Slide 19

Slide 19 text

London Tube Category

Slide 20

Slide 20 text

Event Source Category for pizza delivery service NewOrder Ready Cancelled Returned Dispatched Closed https://skillsmatter.com/skillscasts/11486-functional-cqrs AddItem Cancel Dispatch Return Close

Slide 21

Slide 21 text

Birthday Greetings Kata Problem: write a program that loads a set of employee records from a flat file and then sends a greetings email to all employees whose birthday is today. The flat file is a sequence of records, separated by newlines; this are the first few lines: Doe, John, 1982/10/08, [email protected] Ann, Mary, 1975/09/11, [email protected] http://matteo.vaccari.name/blog/archives/154

Slide 22

Slide 22 text

Hexagonal OO Design Problem: write a program that loads a set of employee records from a flat file and then sends a greetings email to all employees whose birthday is today. The flat file is a sequence of records, separated by newlines; this are the first few lines:

Slide 23

Slide 23 text

Categories Functional Design Our weapons: Immutable Types Pure Functions Laziness No Exceptions

Slide 24

Slide 24 text

Define Arrows From the Outside Filename → Emails sent

Slide 25

Slide 25 text

Define Arrows From the Outside Filename → Emails sent Filename → Text → EmailData → Emails sent

Slide 26

Slide 26 text

Define Arrows From the Outside Filename → Emails sent Filename → Text → EmailData → Emails sent … → Text → CSVrows → Employee → EmailData → …

Slide 27

Slide 27 text

Define Arrows From the Outside Filename → Emails sent Filename → Text → EmailData → Emails sent … → Text → CSVrows → Employee → EmailData → … … → CSVrows → Employee → isTodayBirthday → …

Slide 28

Slide 28 text

Filename TextFile EmailData Emails sent CSVrow Employee TodayBirthday GreetingTemplate Today

Slide 29

Slide 29 text

data class Employee(val firstName: String, val lastName: String, val dateOfBirth: LocalDate, val email: EmailAddress) data class Email (val recipient: EmailAddress, val subject: String, val text: String) inline class EmailAddress(val raw: String) inline class CsvRow(val raw: String) Objects → Types

Slide 30

Slide 30 text

typealias EmployeeToEmail = (Employee) -> Email class EmailTemplate(val msgTemplate: String): EmployeeToEmail { override fun invoke(e: Employee): Email = Email(e.email, "Greetings", msgTemplate.replace("%", e.firstName)) } fun rowToEmployee(csv: CsvRow): Employee = csv.raw.split(",").let{ Employee( firstName = it[1].trim(), lastName = it[0].trim(), email = EmailAddress(it[3].trim()), dateOfBirth = LocalDate.parse(it[2].trim(), LOCAL_DATE)) } Morphisms → Pure Functions

Slide 31

Slide 31 text

Filename TextFile EmailData Emails sent CSVrow Employee TodayBirthday GreetingTemplate Today What about the rest? IO, collection, time, errors...

Slide 32

Slide 32 text

Filename TextFile EmailData Emails sent CSVrow Employee TodayBirthday GreetingTemplate Today Categories What about the rest? IO, collection, time, errors...

Slide 33

Slide 33 text

But, but… how can we switch category?

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

Simplest functor in our code: string.length()

Slide 36

Slide 36 text

Generics are type builders List is not a type but you can build types List List List Etc.

Slide 37

Slide 37 text

Generics are type builders List is not a type but you can build types List List List Etc. Employee → List is a Functor to the Category of List List is said to have a Functor instance

Slide 38

Slide 38 text

Employee → List is a Functor to the Category of List List is said to have a Functor instance map is how you translate functions (morphisms) val employees: List = … val names: List = employees.map { emp → emp.name } List.map(f: (T) -> R): List

Slide 39

Slide 39 text

Functors are like Fork lifts, they lift a function from Employee → String to List → List val employees: List = … val names: List = employees.map { emp → emp.name } List.map(f: (T) -> R): List

Slide 40

Slide 40 text

Functor for Birthday Filter

Slide 41

Slide 41 text

Functor for Errors

Slide 42

Slide 42 text

Functor to Read from IO

Slide 43

Slide 43 text

The Result

Slide 44

Slide 44 text

CHECK! Passes the tests Reveals intention No duplication Fewest elements

Slide 45

Slide 45 text

Takeouts Functional Programming goals are the same than Object-Oriented Programming You don’t need a special language or library You need to study and practice a different paradigm github.com/uberto/birthdaykata

Slide 46

Slide 46 text

Uberto Barbini Blog (jvm performance, kotlin, functional programming): medium.com/@ramtop Twitter: @ramtop Programmer OOP TDD Kotlin Agile Functional Programming Finance Industry

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content