„(…) 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
Not about
Architecture
Slide 6
Slide 6 text
Methods & Code
Slide 7
Slide 7 text
Extra effort
Slide 8
Slide 8 text
Save time
Slide 9
Slide 9 text
Your code base?
Slide 10
Slide 10 text
No content
Slide 11
Slide 11 text
It's about joy!
Slide 12
Slide 12 text
Code is read many more times
than written
Tobias Pfeiffer
@PragTob
pragtob.wordpress.com
Slide 13
Slide 13 text
Sources
Slide 14
Slide 14 text
Crazy?
Slide 15
Slide 15 text
Are comments a smell?
Slide 16
Slide 16 text
Outdated comments are the
worst
Slide 17
Slide 17 text
The why not the what
Slide 18
Slide 18 text
Comments are an excuse of
the code that it could not be
clearer.
Slide 19
Slide 19 text
# do one thing
…
…
…
# do another thing
…
…
…
# do something more
…
…
Slide 20
Slide 20 text
Extract Methods
Slide 21
Slide 21 text
do_one_thing
do_another_thing
do_something_more
Slide 22
Slide 22 text
# context, outlet, times, time per
step, state, data
def pattern(c, o, t, l, s, d)
# …
end
Slide 23
Slide 23 text
Explanatory and meaningful
names
Slide 24
Slide 24 text
def pattern(context, outlet, time,
time_per_step, state, data)
# …
end
Slide 25
Slide 25 text
Try to keep it to 2 parameters
Slide 26
Slide 26 text
Argument order dependency
Slide 27
Slide 27 text
No magic numbers
Slide 28
Slide 28 text
Explanatory variable
Slide 29
Slide 29 text
Short Methods (<= 8 LOC)
Slide 30
Slide 30 text
# allowed to drink?
if customer.age > 18
say 'Okay'
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 31
Slide 31 text
# allowed to drink?
if customer.age > 18
say 'Okay'
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 32
Slide 32 text
Query method
Slide 33
Slide 33 text
Intention revealing method
Slide 34
Slide 34 text
# …
text.color = red
# …
Slide 35
Slide 35 text
def highlight(text)
text.color = red
end
Slide 36
Slide 36 text
# …
highlight(text)
# …
Slide 37
Slide 37 text
if customer.age > 18
say 'Okay'
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 38
Slide 38 text
if customer.age > 18
say 'Okay'
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 39
Slide 39 text
if customer.age > 18
say 'Okay'
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 40
Slide 40 text
if customer.age > 18
say 'Okay'
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 41
Slide 41 text
if allowed_to_drink_alcohol?(customer)
serve_drink requested_drink,
customer
else
propose_non_alcoholic_drink
end
Slide 42
Slide 42 text
„The easiest code to
understand is the code you
don't have to read at all.“
Tom Stuart (Berlin)
Slide 43
Slide 43 text
Same level of abstraction in a
method
Slide 44
Slide 44 text
prepare_drink requested_drink
price = requested_drink.price
Check = Check.new
check.add_price price
say 'That whill be ' + check.total
Slide 45
Slide 45 text
prepare_drink requested_drink
price = requested_drink.price
Check = Check.new
check.add_price price
say 'That whill be ' + check.total
Slide 46
Slide 46 text
prepare_drink requested_drink
price = requested_drink.price
Check = Check.new
check.add_price price
say 'That whill be ' + check.total
„Incoming messages should be tested for
the state they return. Outgoing
command messages should be tested
to ensure they get sent. Outgoing query
messages should not be tested.“
Sandi Metz
Slide 69
Slide 69 text
Know when to
break the rules
Slide 70
Slide 70 text
If you still like your code from
two years ago,
then you are not learning fast
enough.