Slide 1

Slide 1 text

Safety Nets How to check your code?

Slide 2

Slide 2 text

_toch toch Hi, I'm Christophe

Slide 3

Slide 3 text

Belgium

Slide 4

Slide 4 text

Belgium

Slide 5

Slide 5 text

Founder of

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Once upon a time...

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

40% of fatalities = no air

Slide 10

Slide 10 text

Peer Review BWRAF

Slide 11

Slide 11 text

15% of fatalities = equipment

Slide 12

Slide 12 text

Make them Test

Slide 13

Slide 13 text

1st cause = Human error

Slide 14

Slide 14 text

Be Equipped Be Assisted

Slide 15

Slide 15 text

We're Human

Slide 16

Slide 16 text

We make mistakes

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

We can be helped

Slide 19

Slide 19 text

Scuba Diving, Civil Engineering, Manufacturing, … Software Development

Slide 20

Slide 20 text

Safety Nets How to check your code?

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

Be Informed Then Decide

Slide 25

Slide 25 text

Tests Code Review Static Analysis

Slide 26

Slide 26 text

Tests Static Analysis Code Review

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

Tests Static Analysis Code Review

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

Tests Static Analysis Code Review

Slide 33

Slide 33 text

❏ tires on? ❏ painted? ❏ tank full? ❏ labelled? ❏ checked on bench?

Slide 34

Slide 34 text

Tests Static Analysis Code Review

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

describe Invoice do let(:item1) { Item.new(20.0, 3) } let(:item2) { Item.new(15.0, 2) } let(:empty_invoice) { Invoice.new } let(:invoice) do tmp = Invoice.new tmp.add_item(item1) tmp.add_item(item2) tmp end

Slide 37

Slide 37 text

it 'returns a zero total when empty' do assert_equal 0, empty_invoice.total end it 'returns a correct total' do assert_equal 90.0, invoice.total end

Slide 38

Slide 38 text

it 'returns a correct total even after updating a previously added item' do assert_equal 90.0, invoice.total item2.quantity = 3 assert_equal 105, invoice.total end end

Slide 39

Slide 39 text

class Invoice def initialize @items = [] end def add_item(item) @items << item end #...

Slide 40

Slide 40 text

#... def total total = 0 @items.each do |item| total += item.price * item.quantity end total end end

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

class Invoice def initialize @items = [] @total = 0 end def add_item(item) @items << item end #...

Slide 43

Slide 43 text

#... def total @items.each do |item| @total += item.price * item.quantity end @total end end

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

#... def total @total = 0 @items.each do |item| @total += item.price * item.quantity end @total end end

Slide 46

Slide 46 text

Check Functionality & Regression

Slide 47

Slide 47 text

BUT What are we Testing?

Slide 48

Slide 48 text

SimpleCov

Slide 49

Slide 49 text

BUT Has someone Launched the Tests

Slide 50

Slide 50 text

BUT "We’re not shipping your machine!"

Slide 51

Slide 51 text

CI & Close to Prod

Slide 52

Slide 52 text

BUT "Who tests the tests?"

Slide 53

Slide 53 text

Mutant

Slide 54

Slide 54 text

Tests Static Analysis Code Review

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

class Invoice #... def total @total = 0 @items.each do |item| @total += item.price * item.quantity end

Slide 57

Slide 57 text

if @country_code == 'BE' vat = 0.21 * @total elsif ['IT','FR','NL','LU','DE'].include?(@country_code) if valid_vat_number? vat = 0 else vat = 0.21 * @total end else vat = 0 end

Slide 58

Slide 58 text

@total += vat @total end #... end

Slide 59

Slide 59 text

$ flog lib/invoice.rb 27.4: flog total 5.5: flog/method average 17.2: Invoice#total lib/invoice.rb:13

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

class Quote #... def total if @country_code == 'BE' vat = 0.21 * @total elsif ['IT','FR','NL','LU','DE'].include?(@country_code) if valid_vat_number? vat = 0 else vat = 0.21 * @total end

Slide 62

Slide 62 text

else vat = 0 end @total + vat end #... end

Slide 63

Slide 63 text

$ flay lib Total score (lower is better) = 116 1) IDENTICAL code found in :if (mass*2 = 116) lib/invoice.rb:21 lib/quote.rb:10

Slide 64

Slide 64 text

class Invoice #... def total @total = 0 @items.each do |item| @total += item.price * item.quantity end

Slide 65

Slide 65 text

class Invoice #... def total @total = calculate_subtotal

Slide 66

Slide 66 text

def calculate_subtotal total = 0 @items.reduce(0) do |total, item| total += item.price * item.quantity end end

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

$ ruby -w lib/invoice.rb lib/invoice.rb:41: warning: shadowing outer local variable - total lib/invoice.rb:40: warning: assigned but unused variable - total

Slide 69

Slide 69 text

$ rubocop lib/invoice.rb Inspecting 1 file W Offenses: ------8<------ lib/invoice.rb:40:5: W: Useless assignment to variable - total. total = 0 ^^^^^

Slide 70

Slide 70 text

lib/invoice.rb:41:26: W: Shadowing outer local variable - total. @items.reduce(0) do |total, item| ^^^^^ lib/invoice.rb:42:7: W: Useless assignment to variable - total. Use just operator +. total += item.price * item.quantity ^^^^^ 1 file inspected, 10 offenses detected

Slide 71

Slide 71 text

def calculate_subtotal @items.reduce(0) do |total, item| total + item.price * item.quantity end end

Slide 72

Slide 72 text

Check Flaws & Smells

Slide 73

Slide 73 text

BUT You could Forget to run

Slide 74

Slide 74 text

CI + devtool/rake

Slide 75

Slide 75 text

BUT False Positives

Slide 76

Slide 76 text

It is common to take a sort of smug satisfaction in reports of colossal failures of automatic systems, but for every failure of automation, the failures of humans are legion. Exhortations to “write better code” plans for more code reviews, pair programming, and so on just don’t cut it, especially in an environment with dozens of programmers under a lot of time pressure. The value in catching even the small subset of errors that are tractable to static analysis every single time is huge. John Carmack

Slide 77

Slide 77 text

Tests Static Analysis Code Review

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

module Vat def self.rate(country_code = '', vat_number = '') if country_code == 'BE' return 0.21 elsif %w(IT FR NL LU DE).include?(country_code) if is_valid?(vat_number) return 0 else return 0.21 end

Slide 80

Slide 80 text

else return 0 end end #... end

Slide 81

Slide 81 text

class Invoice # ... def total @total = calculate_subtotal vat = Vat::rate(@country_code, @vat_number) * @total @total += vat end # ... end

Slide 82

Slide 82 text

class Quote # ... def total vat = Vat::rate(@country_code, @vat_number) * @total @total + vat end # ... end

Slide 83

Slide 83 text

$ flay lib/ Total score (lower is better) = 0

Slide 84

Slide 84 text

class Country # ... def belgium? @code == 'BE' end def europe? %w(IT FR NL LU DE).include? @code end end

Slide 85

Slide 85 text

module Vat def self.rate(country, vat_number = '') if country.belgium? return 0.21 elsif country.europe? if is_valid?(vat_number) return 0 else return 0.21 end # ... end

Slide 86

Slide 86 text

34.7: flog total 2.9: flog/method average 7.0: Invoice#calculate_subtotal lib/invoice.rb:28 6.7: Invoice#initialize lib/invoice.rb:5 5.7: Invoice#total lib/invoice.rb:16 4.8: Vat::rate lib/vat.rb:2

Slide 87

Slide 87 text

4.8: Vat::rate lib/vat.rb:2 3.3: branch 1.2: is_valid? 1.1: europe? 1.0: belgium? 1.0: assignment

Slide 88

Slide 88 text

Spreading Knowledge

Slide 89

Slide 89 text

Check Anything

Slide 90

Slide 90 text

BUT Cannot Check Everything Everytime

Slide 91

Slide 91 text

Test + Static Analysis

Slide 92

Slide 92 text

What now?

Slide 93

Slide 93 text

Cost & Risk

Slide 94

Slide 94 text

Don't wait

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

We're only Human

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

? https://github.com/ 8thcolor/rdrc2014-safetynets _toch toch