Slide 1

Slide 1 text

The Math Every Programmer Needs

Slide 2

Slide 2 text

You must be really good at math!

Slide 3

Slide 3 text

Not "good at math" Joël Quenneville @joelquen Principal developer at thoughtbot

Slide 4

Slide 4 text

I mostly use basic arithmetic

Slide 5

Slide 5 text

What is the most useful CS class for the working Rails developer?

Slide 6

Slide 6 text

Not Data Structures and Algorithms

Slide 7

Slide 7 text

Discrete Math I use this daily

Slide 8

Slide 8 text

Discrete Math Propositional Logic Boolean Algebra Graph Theory Set Theory Combinatorics Probability Predicate Logic

Slide 9

Slide 9 text

Practical problems not theory

Slide 10

Slide 10 text

BOOLEAN ALGEBRA Unnecessary Conditionals

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

def can_edit? if admin? true else false end end Written code like this

Slide 13

Slide 13 text

def can_edit? admin? end Can be simplified to this

Slide 14

Slide 14 text

LESSON FROM BOOLEAN ALGEBRA Operators

Slide 15

Slide 15 text

Boolean operators identity (admin?) negation (!admin?) and (admin? && moderator?) or (admin? || moderator?)

Slide 16

Slide 16 text

admin? Identity

Slide 17

Slide 17 text

if admin? false else true end Negation

Slide 18

Slide 18 text

!admin? Negation

Slide 19

Slide 19 text

def can_edit_admin_post? return owner? unless admin? true end Two variables

Slide 20

Slide 20 text

def can_edit_admin_post? owner? || admin? end Two variables

Slide 21

Slide 21 text

Use operators!

Slide 22

Slide 22 text

PROPOSITIONAL LOGIC Difficult code review

Slide 23

Slide 23 text

def can_edit_admin_post? return owner? unless admin? true end Complex condition

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

TOOL FROM PROPOSITIONAL LOGIC Truth Table

Slide 26

Slide 26 text

Truth Table owner? admin? can_edit_admin_post? true true true true false true false true true false false false

Slide 27

Slide 27 text

Two expressions are equal if their truth tables match

Slide 28

Slide 28 text

Truth Table owner? admin? can_edit_admin_post? true true true true false true false true true false false false owner? || admin? true true true false

Slide 29

Slide 29 text

Use truth tables in GitHub comments

Slide 30

Slide 30 text

Use truth tables in GitHub comments

Slide 31

Slide 31 text

Understand

Slide 32

Slide 32 text

Improve communication

Slide 33

Slide 33 text

BOOLEAN ALGEBRA Broken Authorization Logic

Slide 34

Slide 34 text

Forbid signed out users and untrusted IPs

Slide 35

Slide 35 text

def allow_access_to_site? !(signed_out? && untrusted_ip?) end Confusing negated code

Slide 36

Slide 36 text

def allow_access_to_site? !signed_out? && !untrusted_ip? end Individual negation: cleaner?

Slide 37

Slide 37 text

Broken

Slide 38

Slide 38 text

Expected behavior signed_out? untrusted_ip? Correct true true false true false true false true true false false true

Slide 39

Slide 39 text

Individual negation signed_out? untrusted_ip? Correct true true false true false true false true true false false true individual negation false false false true

Slide 40

Slide 40 text

LESSON FROM BOOLEAN ALGEBRA DeMorgan's laws

Slide 41

Slide 41 text

DeMorgan's laws To negate compound conditions Negate each clause Invert operator 1 2

Slide 42

Slide 42 text

DeMorgan's laws To negate compound conditions !(a && b) == !a || !b !(a || b) == !a && !b 1 2

Slide 43

Slide 43 text

def allow_access_to_site? !(signed_out? && untrusted_ip?) end Applying DeMorgan: Original

Slide 44

Slide 44 text

def allow_access_to_site? !signed_out? && !untrusted_ip? end Applying DeMorgan: Step 1 NEGATE INDIVIDUAL CLAUSES

Slide 45

Slide 45 text

def allow_access_to_site? !signed_out? || !untrusted_ip? end Applying DeMorgan: Step 2 INVERT OPERATOR

Slide 46

Slide 46 text

DeMorgan signed_out? untrusted_ip? Correct true true false true false true false true true false false true DeMorgan false true true true

Slide 47

Slide 47 text

def allow_access_to_site? !signed_out? || !untrusted_ip? end DeMorganized code

Slide 48

Slide 48 text

def allow_access_to_site? signed_in? || trusted_ip? end Alias methods

Slide 49

Slide 49 text

Make code more readable

Slide 50

Slide 50 text

Catch bugs

Slide 51

Slide 51 text

unless signed_out? && untrusted_ip? render :not_authorized end Unless is also negation

Slide 52

Slide 52 text

COMBINATORICS How many test cases do I need?

Slide 53

Slide 53 text

def can_read? article.access_policy&.public? || user.admin? end Policy Object

Slide 54

Slide 54 text

describe "#can_read?" do it "allows admin to access public article" it "allows admin to access private article" it "allows non-admin to access public article" it "disallows non-admin from accessing private article" end Enough tests?

Slide 55

Slide 55 text

2 inputs User is admin? Article is public? 1 2

Slide 56

Slide 56 text

2 states user.admin? true false 1 2

Slide 57

Slide 57 text

3 States article.access_policy&.public? true false nil 1 2 3

Slide 58

Slide 58 text

LESSON FROM COMBINATORICS Compound states multiply

Slide 59

Slide 59 text

2 states x 3 states = 6 states

Slide 60

Slide 60 text

Modified truth table Article is public? User is admin? Can read? true true true true false true false true true false false false nil true true nil false false

Slide 61

Slide 61 text

class ArticleQuery def initialize(limit: nil, scope: Article.all) # ... end end Default parameters

Slide 62

Slide 62 text

Optional params add 2 states to your signature

Slide 63

Slide 63 text

2 states x 2 states = 4 states

Slide 64

Slide 64 text

Ways of calling constructor new new(limit: 10) new(scope: Article.published) new(limit: 10, scope: Article.published) 1 2 3 4

Slide 65

Slide 65 text

TOOL FROM SET THEORY Venn diagrams

Slide 66

Slide 66 text

Set of tests is smaller than set of states Tests States

Slide 67

Slide 67 text

Grow set of tests Tests States

Slide 68

Slide 68 text

Shrink set of states Tests States

Slide 69

Slide 69 text

Solution: Add more tests or reduce your states

Slide 70

Slide 70 text

GRAPH THEORY Which RSpec let blocks actually get invoked?

Slide 71

Slide 71 text

describe User do let(:organization) { create(:organization) } let(:user) { create(:user, organization: organization) } let!(:admin) { create(:user, admin: true, organization: organization) } # lots of other tests describe "Invoices" do let(:product) { create(:product) } let(:invoice) { create(:invoice, owner: user, items: [product] } it "is complete" do expect(invoice).to be_complete end it "is an active product" do expect(product).to be_active end end end

Slide 72

Slide 72 text

Visual Model that I like

Slide 73

Slide 73 text

List lets organization user admin product invoice

Slide 74

Slide 74 text

Connect boxes organization user admin product invoice

Slide 75

Slide 75 text

Dependency Graph Terminology

Slide 76

Slide 76 text

Mark let! organization user admin product invoice

Slide 77

Slide 77 text

LESSON FROM GRAPH THEORY Evaluating a node also requires evaluating all downstream nodes

Slide 78

Slide 78 text

Follow dependencies organization user admin product invoice

Slide 79

Slide 79 text

Basic State All tests start with this

Slide 80

Slide 80 text

it "is complete" do expect(invoice).to be_complete end

Slide 81

Slide 81 text

Mark items referenced in test organization user admin product invoice

Slide 82

Slide 82 text

Follow dependencies organization user admin product invoice

Slide 83

Slide 83 text

it "is an active product" do expect(product).to be_active end

Slide 84

Slide 84 text

Mark items referenced in test organization user admin product invoice

Slide 85

Slide 85 text

GRAPH THEORY Working incrementally

Slide 86

Slide 86 text

Linear list Step1 Step 2 Step 3 Step 4 Step 5

Slide 87

Slide 87 text

Actual dependencies Step 1 Step2 Step 3 Step 4 Step 5

Slide 88

Slide 88 text

Actual dependencies Step 1 Step2 Step 3 Step 4 Step 5

Slide 89

Slide 89 text

LESSON FROM GRAPH THEORY Evaluating a node also requires evaluating all downstream nodes

Slide 90

Slide 90 text

Actual dependencies Step 1 Step2 Step 3 Step 4 Step 5

Slide 91

Slide 91 text

Doing tasks with no dependencies Step 1 Step2 Step 3 Step 4 Step 5

Slide 92

Slide 92 text

Doing tasks with no dependencies Step 1 Step2 Step 3 Step 4 Step 5

Slide 93

Slide 93 text

Doing tasks with no dependencies Step 1 Step2 Step 3 Step 4 Step 5

Slide 94

Slide 94 text

Doing tasks with no dependencies Step 1 Step2 Step 3 Step 4 Step 5

Slide 95

Slide 95 text

Doing tasks with no dependencies Step 1 Step2 Step 3 Step 4 Step 5

Slide 96

Slide 96 text

LESSON FROM GRAPH THEORY Dependency graphs must be evaluated bottom-up

Slide 97

Slide 97 text

Topological Sort Terminology

Slide 98

Slide 98 text

Replace image processing gem Change the Gemfile Update User model Update Article model

Slide 99

Slide 99 text

Replace image processing gem Change image processing gem Update User model Update Article model

Slide 100

Slide 100 text

Cycles Change image processing gem Update User model Update Article model

Slide 101

Slide 101 text

LESSON FROM GRAPH THEORY Cycles cannot be evaluated incrementally

Slide 102

Slide 102 text

Breaking Cycles Change image processing gem Update User model Introduce adapter Update Article model

Slide 103

Slide 103 text

Directed Acyclic Graph (DAG) Terminology

Slide 104

Slide 104 text

Strangler Fig Pattern

Slide 105

Slide 105 text

COMBINATORICS What are the risks of a collision?

Slide 106

Slide 106 text

SET THEORY, COMBINATORICS Designing a DB schema

Slide 107

Slide 107 text

PREDICATE LOGIC Why do all? and any? behave like that on an empty array?

Slide 108

Slide 108 text

PREDICATE LOGIC, SET THEORY Improve your communication

Slide 109

Slide 109 text

Ideas from Discrete Math Break cycles in your dependency graphs Compound states multiply DeMorgan laws Truth tables Use Boolean operators Evaluate dependency graph bottom- up Evaluating a graph node requires evaluating downstream nodes

Slide 110

Slide 110 text

Not "good at math" Joël Quenneville @joelquen Principal developer at thoughtbot