Slide 1

Slide 1 text

Safety Nets: Learn to code with confidence

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

Co-founder of

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Once upon a time...

Slide 8

Slide 8 text

My wife's job:

Slide 9

Slide 9 text

My wife's job: curing cancer

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

With Ionizing Radiation

Slide 12

Slide 12 text

Side Effects BUT

Slide 13

Slide 13 text

BUT =

Slide 14

Slide 14 text

BUT Complex

Slide 15

Slide 15 text

BUT Long

Slide 16

Slide 16 text

BUT

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Ineffective

Slide 19

Slide 19 text

Casualties Ineffective

Slide 20

Slide 20 text

Casualties Ineffective Costly Time consuming

Slide 21

Slide 21 text

Casualties Ineffective Broken Equipment Costly Time consuming

Slide 22

Slide 22 text

Casualties Ineffective Broken Equipment Non Availability Costly Time consuming

Slide 23

Slide 23 text

As an Engineer What CAN she do?

Slide 24

Slide 24 text

Catch Errors & Mitigate Risks

Slide 25

Slide 25 text

How?

Slide 26

Slide 26 text

Is it Working?

Slide 27

Slide 27 text

Test Before & During

Slide 28

Slide 28 text

Is it Correct?

Slide 29

Slide 29 text

Peer-Review & Cross-Check

Slide 30

Slide 30 text

Have the Requirements been met?

Slide 31

Slide 31 text

Peer-Review & Process

Slide 32

Slide 32 text

Sounds Familiar?

Slide 33

Slide 33 text

We're Human

Slide 34

Slide 34 text

We make mistakes

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

We can be helped

Slide 37

Slide 37 text

Radiotherapy, Civil Engineering, Manufacturing, … Software Development

Slide 38

Slide 38 text

Safety Nets

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

Be Informed Then Decide

Slide 43

Slide 43 text

Tests Static Analysis Code Review

Slide 44

Slide 44 text

Tests Static Analysis Code Review

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 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 47

Slide 47 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 48

Slide 48 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 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

Check Functionality & Regression

Slide 57

Slide 57 text

BUT What are we Testing?

Slide 58

Slide 58 text

SimpleCov

Slide 59

Slide 59 text

BUT Has someone Launched the Tests

Slide 60

Slide 60 text

BUT "We’re not shipping your machine!"

Slide 61

Slide 61 text

CI & Close to Prod

Slide 62

Slide 62 text

BUT "Who tests the tests?"

Slide 63

Slide 63 text

Mutant

Slide 64

Slide 64 text

Tests Static Analysis Code Review

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

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

Slide 67

Slide 67 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 68

Slide 68 text

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

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 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 72

Slide 72 text

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

Slide 73

Slide 73 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 74

Slide 74 text

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

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 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 79

Slide 79 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 80

Slide 80 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 81

Slide 81 text

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

Slide 82

Slide 82 text

Check Flaws & Smells

Slide 83

Slide 83 text

BUT You could Forget to run

Slide 84

Slide 84 text

CI + devtool/rake

Slide 85

Slide 85 text

BUT False Positives

Slide 86

Slide 86 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 87

Slide 87 text

Tests Static Analysis Code Review

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 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 90

Slide 90 text

else return 0 end end #... end

Slide 91

Slide 91 text

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

Slide 92

Slide 92 text

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

Slide 93

Slide 93 text

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

Slide 94

Slide 94 text

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

Slide 95

Slide 95 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 96

Slide 96 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 97

Slide 97 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 98

Slide 98 text

Spreading Knowledge

Slide 99

Slide 99 text

Check Anything

Slide 100

Slide 100 text

BUT Cannot Check Everything Everytime

Slide 101

Slide 101 text

Test + Static Analysis

Slide 102

Slide 102 text

What now?

Slide 103

Slide 103 text

Cost & Risk

Slide 104

Slide 104 text

Don't wait

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

We're only Human

Slide 107

Slide 107 text

No content

Slide 108

Slide 108 text

No content

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

No content

Slide 111

Slide 111 text

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