„(…) 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 6
Slide 6 text
„Any fool can write code that a computer
can understand. Good programmers write
code that humans can understand.“
Martin Fowler
Slide 7
Slide 7 text
Not about architecture
Slide 8
Slide 8 text
Methods & Code
Slide 9
Slide 9 text
Nurturing a code base
Slide 10
Slide 10 text
Extra effort
Slide 11
Slide 11 text
Save time!
Slide 12
Slide 12 text
Your code base?
Slide 13
Slide 13 text
No content
Slide 14
Slide 14 text
It's about joy!
Slide 15
Slide 15 text
Optimizing for Readability
Tobias Pfeiffer
@PragTob
pragtob.info
Slide 16
Slide 16 text
Crazy?
Slide 17
Slide 17 text
Methods & Code
Slide 18
Slide 18 text
No content
Slide 19
Slide 19 text
Keep It Simple Stupid
Slide 20
Slide 20 text
Are comments a smell?
Slide 21
Slide 21 text
Comments are an excuse of
the code that it could not be
clearer.
Slide 22
Slide 22 text
Outdated comments are the
worst
Slide 23
Slide 23 text
The why not the what
Slide 24
Slide 24 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
Slide 25
Slide 25 text
Also known as the smell that
tries to make other smells
seem ok
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
...
...
Slide 28
Slide 28 text
# do one thing
...
...
...
...
...
# do another thing
...
...
...
...
# do something more
...
...
Cocepts
# context, outlet, times, time per
step, state, data
def pattern(c, o, t, l, s, d)
# ...
end
Slide 35
Slide 35 text
Incomprehensible names
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
# context, outlet, times, time per
step, state, data
def pattern(c, o, t, l, s, d)
# ...
end
Slide 38
Slide 38 text
Explanatory names
Slide 39
Slide 39 text
Naming is hard
Slide 40
Slide 40 text
def pattern(context, outlet, time,
time_per_step, state,
data)
# ...
end
Slide 41
Slide 41 text
Argument order dependency
Slide 42
Slide 42 text
Try to keep it to 2 parameters
Slide 43
Slide 43 text
Example
Slide 44
Slide 44 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 45
Slide 45 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
# 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 49
Slide 49 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 50
Slide 50 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 51
Slide 51 text
Query method
Slide 52
Slide 52 text
Intention revealing method
Slide 53
Slide 53 text
# ...
text.color = red
# ...
Slide 54
Slide 54 text
# ...
text.color = red
# ...
Slide 55
Slide 55 text
# ...
highlight(text)
# ...
Slide 56
Slide 56 text
def highlight(text)
text.color = red
end
Slide 57
Slide 57 text
def highlight(text)
text.color = red
text.underline = true
update_highlights
end
# 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 61
Slide 61 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 62
Slide 62 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 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
if allowed_to_drink_alcohol?(customer)
serve_drink requested_drink,
customer
else
propose_non_alcoholic_drink
end
Slide 65
Slide 65 text
„If you have a good name for
a method you don't need to
look at the body.“
Martin Fowler
Slide 66
Slide 66 text
„The easiest code to
understand is the code you
don't have to read at all.“
Tom Stuart (Berlin)
Slide 67
Slide 67 text
prepare_drink requested_drink
price = requested_drink.price
check = Check.new
check.add_price price
say 'That whill be ' + check.total
Slide 68
Slide 68 text
prepare_drink requested_drink
price = requested_drink.price
check = Check.new
check.add_price price
say 'That whill be ' + check.total
Slide 69
Slide 69 text
prepare_drink requested_drink
price = requested_drink.price
check = Check.new
check.add_price price
say 'That whill be ' + check.total