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
"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“
# 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
# 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