Slide 1

Slide 1 text

We write code

Slide 2

Slide 2 text

Isn't it more about reading?

Slide 3

Slide 3 text

Written once - read many times

Slide 4

Slide 4 text

(…) when you program, you have to think about how someone will read your code, not just how a computer will interpret it. Kent Beck

Slide 5

Slide 5 text

Any fool can write code that a computer can understand. Good programmers write code that humans can understand. Martin Fowler

Slide 6

Slide 6 text

Not about architecture

Slide 7

Slide 7 text

Methods & Code

Slide 8

Slide 8 text

Nurturing a code base

Slide 9

Slide 9 text

Extra effort

Slide 10

Slide 10 text

Save time!

Slide 11

Slide 11 text

Your code base?

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

It's about joy!

Slide 14

Slide 14 text

Code, Comments, Concepts, Comprehension – Conclusion? Tobias Pfeiffer @PragTob pragtob.info

Slide 15

Slide 15 text

Crazy?

Slide 16

Slide 16 text

Methods & Code

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Keep It Simple Stupid

Slide 19

Slide 19 text

Are comments a code smell?

Slide 20

Slide 20 text

Comments are an excuse of the code that it could not be clearer.

Slide 21

Slide 21 text

Outdated comments are the worst

Slide 22

Slide 22 text

not the WHY WHAT

Slide 23

Slide 23 text

def paint_control(event) # some painting code rescue => e # Really important to rescue here. Failures that escape this method # cause odd-ball hangs with no backtraces. See #559 for an example. # puts "SWALLOWED PAINT EXCEPTION ON #{@obj} - go take care of it: " + e.to_s puts 'Unfortunately we have to swallow it because it causes odd failures :(' end WHY comment

Slide 24

Slide 24 text

Comments are the smell that tries to make other smells seem ok

Slide 25

Slide 25 text

# do one thing ... ... ... ... # do another thing ... ... ... ... # do something more ... ...

Slide 26

Slide 26 text

# do one thing ... ... ... ... # do another thing ... ... ... ... # do something more ... ...

Slide 27

Slide 27 text

# do one thing ... ... ... ... # do another thing ... ... ... ... # do something more ... ... Cocepts

Slide 28

Slide 28 text

Method too long

Slide 29

Slide 29 text

Short Methods

Slide 30

Slide 30 text

<= 8 LOC

Slide 31

Slide 31 text

Extract Methods

Slide 32

Slide 32 text

do_one_thing do_another_thing do_something_more Concepts

Slide 33

Slide 33 text

People disagree

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

"The more complex the code, the more comments it should have." You should make the code less complex not add more comments. Tiago Teixeira

Slide 36

Slide 36 text

# context, outlet, times, time per step, state, data def pattern(c, o, t, l, s, d) # ... end

Slide 37

Slide 37 text

Incomprehensible names

Slide 38

Slide 38 text

# context, outlet, times, time per step, state, data def pattern(c, o, t, l, s, d) # ... end

Slide 39

Slide 39 text

# context, outlet, times, time per step, state, data def pattern(c, o, t, l, s, d) # ... end

Slide 40

Slide 40 text

Explanatory names

Slide 41

Slide 41 text

Naming is hard

Slide 42

Slide 42 text

One Language

Slide 43

Slide 43 text

def pattern(context, outlet, time, time_per_step, state, data) # ... end

Slide 44

Slide 44 text

Argument order dependency

Slide 45

Slide 45 text

Try to keep it to 2 parameters

Slide 46

Slide 46 text

Example

Slide 47

Slide 47 text

# allowed to drink? if customer.age >= 18 say 'Okay' drink = prepare_drink requested_drink say 'here you go' hand_drink_over drink, customer else say 'I am sorry you are not legally allowed rather to drink here' say "Would you rather have a #{['cola', 'mate'].sample}?" end

Slide 48

Slide 48 text

# allowed to drink? if customer.age >= 18 say 'Okay' drink = prepare_drink requested_drink say 'here you go' hand_drink_over drink, customer else say 'I am sorry you are not legally allowed rather to drink here' say "Would you rather have a #{['cola', 'mate'].sample}?" end

Slide 49

Slide 49 text

# allowed to drink? if customer.age >= 18 say 'Okay' drink = prepare_drink requested_drink say 'here you go' hand_drink_over drink, customer else say 'I am sorry you are not legally allowed rather to drink here' say "Would you rather have a #{['cola', 'mate'].sample}?" end Magic „Numbers“

Slide 50

Slide 50 text

NON_ALCOHOLIC_DRINKS = ['cola', 'mate'] MIN_DRINKING_AGE = 18

Slide 51

Slide 51 text

# allowed to drink? if customer.age >= MIN_DRINKING_AGE say 'Okay' drink = prepare_drink requested_drink say 'here you go' hand_drink_over drink, customer else say 'I am sorry you are not legally allowed rather to drink here' say "Would you rather have a #{NON_ALCOHOLIC_DRINKS.sample}?" end

Slide 52

Slide 52 text

# allowed to drink? if customer.age >= MIN_DRINKING_AGE say 'Okay' drink = prepare_drink requested_drink say 'here you go' hand_drink_over drink, customer else say 'I am sorry you are not legally allowed rather to drink here' say "Would you rather have a #{NON_ALCOHOLIC_DRINKS.sample}?" end

Slide 53

Slide 53 text

# allowed to drink? if customer.age >= MIN_DRINKING_AGE say 'Okay' drink = prepare_drink requested_drink say 'here you go' hand_drink_over drink, customer else say 'I am sorry you are not legally allowed rather to drink here' say "Would you rather have a #{NON_ALCOHOLIC_DRINKS.sample}?" end

Slide 54

Slide 54 text

Query method

Slide 55

Slide 55 text

Intention revealing method

Slide 56

Slide 56 text

# ... text.color = red # ...

Slide 57

Slide 57 text

# ... text.color = red # ...

Slide 58

Slide 58 text

# ... highlight(text) # ...

Slide 59

Slide 59 text

def highlight(text) text.color = red end

Slide 60

Slide 60 text

def highlight(text) text.color = red text.underline = true update_highlights end

Slide 61

Slide 61 text

# ... text.color = red text.underline = true update_highlights # ...

Slide 62

Slide 62 text

# ... highlight(text) # ...

Slide 63

Slide 63 text

# allowed to drink? if customer.age >= MIN_DRINKING_AGE say 'Okay' drink = prepare_drink requested_drink say 'here you go' hand_drink_over drink, customer else say 'I am sorry you are not legally allowed rather to drink here' say "Would you rather have a #{NON_ALCOHOLIC_DRINKS.sample}?" end

Slide 64

Slide 64 text

# allowed to drink? if customer.age >= MIN_DRINKING_AGE say 'Okay' drink = prepare_drink requested_drink say 'here you go' hand_drink_over drink, customer else say 'I am sorry you are not legally allowed rather to drink here' say "Would you rather have a #{NON_ALCOHOLIC_DRINKS.sample}?" end

Slide 65

Slide 65 text

# allowed to drink? if customer.age >= MIN_DRINKING_AGE say 'Okay' drink = prepare_drink requested_drink say 'here you go' hand_drink_over drink, customer else say 'I am sorry you are not legally allowed rather to drink here' say "Would you rather have a #{NON_ALCOHOLIC_DRINKS.sample}?" end

Slide 66

Slide 66 text

# allowed to drink? if customer.age >= MIN_DRINKING_AGE say 'Okay' drink = prepare_drink requested_drink say 'here you go' hand_drink_over drink, customer else say 'I am sorry you are not legally allowed rather to drink here' say "Would you rather have a #{NON_ALCOHOLIC_DRINKS.sample}?" end

Slide 67

Slide 67 text

if allowed_to_drink_alcohol?(customer) serve_drink requested_drink, customer else propose_non_alcoholic_drink end

Slide 68

Slide 68 text

„If you have a good name for a method you don't need to look at the body.“ Martin Fowler

Slide 69

Slide 69 text

„The easiest code to understand is the code you don't have to read at all.“ Tom Stuart (Berlin)

Slide 70

Slide 70 text

def serve_alcoholic_drink(customer, requested_drink) if allowed_to_drink_alcohol?(customer) serve_drink requested_drink, customer else propose_non_alcoholic_drink end end # more public methods private def allowed_to_drink_alcohol?(customer) end def serve_drink(requested_drink, customer) end def propose_non_alcoholic_drink end Method Order

Slide 71

Slide 71 text

def serve_alcoholic_drink(customer, requested_drink) if allowed_to_drink_alcohol?(customer) serve_drink requested_drink, customer else propose_non_alcoholic_drink end end # more public methods private def allowed_to_drink_alcohol?(customer) end def serve_drink(requested_drink, customer) end def propose_non_alcoholic_drink end

Slide 72

Slide 72 text

Method Order def serve_alcoholic_drink(customer, requested_drink) if allowed_to_drink_alcohol?(customer) serve_drink requested_drink, customer else propose_non_alcoholic_drink end end defp allowed_to_drink_alcohol?(customer) end defp serve_drink(requested_drink, customer) end defp propose_non_alcoholic_drink end # more public methods

Slide 73

Slide 73 text

def serve_alcoholic_drink(customer, requested_drink) if allowed_to_drink_alcohol?(customer) serve_drink requested_drink, customer else propose_non_alcoholic_drink end end defp allowed_to_drink_alcohol?(customer) end defp serve_drink(requested_drink, customer) end defp propose_non_alcoholic_drink end # more public methods

Slide 74

Slide 74 text

prepare_drink requested_drink price = requested_drink.price check = Check.new check.add_price price say 'That whill be ' + check.total

Slide 75

Slide 75 text

prepare_drink requested_drink price = requested_drink.price check = Check.new check.add_price price say 'That whill be ' + check.total

Slide 76

Slide 76 text

prepare_drink requested_drink price = requested_drink.price check = Check.new check.add_price price say 'That whill be ' + check.total

Slide 77

Slide 77 text

Same level of abstraction in a method

Slide 78

Slide 78 text

prepare_drink requested_drink prepare_check requested_drink

Slide 79

Slide 79 text

@left ||= 0 @top ||= 0 @width ||= 1.0 @height ||= 0 Code Formatting

Slide 80

Slide 80 text

double character: 'something weird', stateMask: CTRL | modifier, KeyCode: character.downcase.ord Code Formatting

Slide 81

Slide 81 text

80 character width limit

Slide 82

Slide 82 text

80 character width limit

Slide 83

Slide 83 text

80 character width limit

Slide 84

Slide 84 text

80 character width limit

Slide 85

Slide 85 text

80 character width limit

Slide 86

Slide 86 text

Identify concepts

Slide 87

Slide 87 text

Don't Repeat Yourself

Slide 88

Slide 88 text

Don't Repeat Yourself (In Concepts)

Slide 89

Slide 89 text

Virtues

Slide 90

Slide 90 text

Simplicity

Slide 91

Slide 91 text

The least powerful construct

Slide 92

Slide 92 text

Explicit vs Implicit

Slide 93

Slide 93 text

person = Person.new(attributes) do_something(person) insert_in_db(person) Immutable Data

Slide 94

Slide 94 text

person = Person.new(attributes) person = do_something(person) insert_in_db(person) Immutable Data

Slide 95

Slide 95 text

Transformation of data

Slide 96

Slide 96 text

Reads like a book

Slide 97

Slide 97 text

Nurturing a code base

Slide 98

Slide 98 text

Code bases detoriate

Slide 99

Slide 99 text

No broken windows!

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

No content

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

No content

Slide 104

Slide 104 text

Magical time?

Slide 105

Slide 105 text

The boyscout rule

Slide 106

Slide 106 text

Opportunistic Refactoring

Slide 107

Slide 107 text

Confident Tests

Slide 108

Slide 108 text

80% Code Coverage

Slide 109

Slide 109 text

20% never executed

Slide 110

Slide 110 text

Code Review Culture

Slide 111

Slide 111 text

Always the code, never the person

Slide 112

Slide 112 text

Always the code, never the person Say something positive

Slide 113

Slide 113 text

Always the code, never the person Say something positive Use automated linters

Slide 114

Slide 114 text

Always the code, never the person Say something positive Use automated linters Use automated linters Everyone reviews

Slide 115

Slide 115 text

Always the code, never the person Say something positive Use automated linters Use automated linters Everyone reviews Pairing Review

Slide 116

Slide 116 text

Pair Programming

Slide 117

Slide 117 text

The power of magic hats

Slide 118

Slide 118 text

Reaping the benefits

Slide 119

Slide 119 text

When to break the rules

Slide 120

Slide 120 text

If you still like your code from two years ago, then you are not learning fast enough.

Slide 121

Slide 121 text

Enjoy writing readable code! Tobias Pfeiffer @PragTob pragtob.info

Slide 122

Slide 122 text

RUG::B benchmarking Open Source Testing Ruby Implementations

Slide 123

Slide 123 text

Sources ● The Pragmatic Programmer ● Smalltalk Best Practice Patterns ● Clean Code ● Practical Object Oriented Design in Ruby

Slide 124

Slide 124 text

Photo Credit ● http://officeimg.vo.msecnd.net/en-us/images/MP900439313.jpg ● http://officeimg.vo.msecnd.net/en-us/images/MC900021328.wmf ● http://www.osnews.com/story/19266/WTFs_m ● (CC BY-SA 2.0) – http://www.flickr.com/photos/83633410@N07/7658272558/in/photostream/ – http://www.flickr.com/photos/83633410@N07/7658165122/ – https://www.flickr.com/photos/93425126@N00/313056379/ ● (CC BY-NC-ND 2.0) – http://www.flickr.com/photos/andih/86577529/ – http://www.flickr.com/photos/12584908@N08/3293117576/ – http://www.flickr.com/photos/jasonlparks/4525188865/ – http://www.flickr.com/photos/20714221@N04/2293045156/ – https://www.flickr.com/photos/eyewash/2603717864/ – https://www.flickr.com/photos/stevie_gill/3950697539/ – https://www.flickr.com/photos/randar/15787696685/ ● http://www.flickr.com/photos/47833351@N02/5488791911/(CC BY-ND 2.0) ● (CC BY 2.0) – http://www.flickr.com/photos/barry_b/76055201/ – http://www.flickr.com/photos/25165196@N08/7725273678/ – http://www.flickr.com/photos/29254399@N08/3187186308/ – https://www.flickr.com/photos/garryknight/5650367750/ – https://www.flickr.com/photos/alper/10742816123/ ● (CC BY-NC-SA 2.0) – http://www.flickr.com/photos/dolescum/7380616658/ – http://www.flickr.com/photos/antonkovalyov/5795281215/ – http://www.flickr.com/photos/doug88888/2792209612/ – https://www.flickr.com/photos/denverjeffrey/4392418334/ ● (CC BY-NC 2.0) – http://www.flickr.com/photos/37996583811@N01/5757983532/ – http://www.flickr.com/photos/sevendead/5650065458/ – https://www.flickr.com/photos/whitecatsg/3146092196/