Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
We write code
Isn't it more
about reading?
Written once -
read many times
(…) when you program, you have to think about how
someone will read your code, not just how a
computer will interpret it.
...
Any fool can write code that a computer can
understand. Good programmers write code that
humans can understand.
Martin Fow...
Not about architecture
Methods & Code
Nurturing a code base
Extra effort
Save time!
Your code base?
It's about joy!
Code, Comments, Concepts,
Comprehension – Conclusion?
Tobias Pfeiffer
@PragTob
pragtob.info
Crazy?
Methods & Code
Keep
It
Simple
Stupid
Are comments a code smell?
Comments are an excuse of the
code that it could not be clearer.
Outdated comments are the worst
not the
WHY
WHAT
def paint_control(event)
# some painting code
rescue => e
# Really important to rescue here.
Failures that escape this met...
Comments are the smell that tries
to make other smells seem ok
# do one thing
...
...
...
...
# do another thing
...
...
...
...
# do something more
...
...
# do one thing
...
...
...
...
# do another thing
...
...
...
...
# do something more
...
...
# do one thing
...
...
...
...
# do another thing
...
...
...
...
# do something more
...
...
Cocepts
Method too long
Short Methods
<= 8 LOC
Extract Methods
do_one_thing
do_another_thing
do_something_more
Concepts
People disagree
"The more complex the code, the more comments it
should have."
You should make the code less complex not add
more comments...
# context, outlet, times, time per
step, state, data
def pattern(c, o, t, l, s, d)
# ...
end
Incomprehensible names
# context, outlet, times, time per
step, state, data
def pattern(c, o, t, l, s, d)
# ...
end
# context, outlet, times, time per
step, state, data
def pattern(c, o, t, l, s, d)
# ...
end
Explanatory names
Naming is hard
One Language
def pattern(context, outlet, time,
time_per_step, state,
data)
# ...
end
Argument order dependency
Try to keep it to 2 parameters
Example
# allowed to drink?
if customer.age >= 18
say 'Okay'
drink = prepare_drink requested_drink
say 'here you go'
hand_drink_ov...
# allowed to drink?
if customer.age >= 18
say 'Okay'
drink = prepare_drink requested_drink
say 'here you go'
hand_drink_ov...
# allowed to drink?
if customer.age >= 18
say 'Okay'
drink = prepare_drink requested_drink
say 'here you go'
hand_drink_ov...
NON_ALCOHOLIC_DRINKS = ['cola', 'mate']
MIN_DRINKING_AGE = 18
# allowed to drink?
if customer.age >= MIN_DRINKING_AGE
say 'Okay'
drink = prepare_drink requested_drink
say 'here you go'...
# allowed to drink?
if customer.age >= MIN_DRINKING_AGE
say 'Okay'
drink = prepare_drink requested_drink
say 'here you go'...
# allowed to drink?
if customer.age >= MIN_DRINKING_AGE
say 'Okay'
drink = prepare_drink requested_drink
say 'here you go'...
Query method
Intention revealing method
# ...
text.color = red
# ...
# ...
text.color = red
# ...
# ...
highlight(text)
# ...
def highlight(text)
text.color = red
end
def highlight(text)
text.color = red
text.underline = true
update_highlights
end
# ...
text.color = red
text.underline = true
update_highlights
# ...
# ...
highlight(text)
# ...
# allowed to drink?
if customer.age >= MIN_DRINKING_AGE
say 'Okay'
drink = prepare_drink requested_drink
say 'here you go'...
# allowed to drink?
if customer.age >= MIN_DRINKING_AGE
say 'Okay'
drink = prepare_drink requested_drink
say 'here you go'...
# allowed to drink?
if customer.age >= MIN_DRINKING_AGE
say 'Okay'
drink = prepare_drink requested_drink
say 'here you go'...
# allowed to drink?
if customer.age >= MIN_DRINKING_AGE
say 'Okay'
drink = prepare_drink requested_drink
say 'here you go'...
if allowed_to_drink_alcohol?(customer)
serve_drink requested_drink, customer
else
propose_non_alcoholic_drink
end
„If you have a good name for a
method you don't need to look at
the body.“
Martin Fowler
„The easiest code to understand is
the code you don't have to read at
all.“
Tom Stuart (Berlin)
def serve_alcoholic_drink(customer, requested_drink)
if allowed_to_drink_alcohol?(customer)
serve_drink requested_drink, c...
def serve_alcoholic_drink(customer, requested_drink)
if allowed_to_drink_alcohol?(customer)
serve_drink requested_drink, c...
Method Order
def serve_alcoholic_drink(customer, requested_drink)
if allowed_to_drink_alcohol?(customer)
serve_drink reque...
def serve_alcoholic_drink(customer, requested_drink)
if allowed_to_drink_alcohol?(customer)
serve_drink requested_drink, c...
prepare_drink requested_drink
price = requested_drink.price
check = Check.new
check.add_price price
say 'That whill be ' +...
prepare_drink requested_drink
price = requested_drink.price
check = Check.new
check.add_price price
say 'That whill be ' +...
prepare_drink requested_drink
price = requested_drink.price
check = Check.new
check.add_price price
say 'That whill be ' +...
Same level of abstraction in a
method
prepare_drink requested_drink
prepare_check requested_drink
@left ||= 0
@top ||= 0
@width ||= 1.0
@height ||= 0
Code Formatting
double character: 'something weird',
stateMask: CTRL | modifier,
KeyCode: character.downcase.ord
Code Formatting
80 character width limit
80 character width limit
80 character width limit
80 character width limit
80 character width limit
Identify concepts
Don't
Repeat
Yourself
Don't
Repeat
Yourself
(In Concepts)
Virtues
Simplicity
The least powerful construct
Explicit vs Implicit
person = Person.new(attributes)
do_something(person)
insert_in_db(person)
Immutable Data
person = Person.new(attributes)
person = do_something(person)
insert_in_db(person)
Immutable Data
Transformation of data
Reads like a book
Nurturing a code base
Code bases detoriate
No broken windows!
Magical time?
The boyscout rule
Opportunistic Refactoring
Confident Tests
80% Code Coverage
20% never executed
Code Review Culture
Always the code, never the person
Always the code, never the person
Say something positive
Always the code, never the person
Say something positive
Use automated linters
Always the code, never the person
Say something positive
Use automated lintersUse automated linters
Everyone reviews
Always the code, never the person
Say something positive
Use automated lintersUse automated linters
Everyone reviews
Pairi...
Pair Programming
The power of magic hats
Reaping the benefits
When to break the rules
If you still like your code from two
years ago,
then you are not learning fast
enough.
Enjoy writing readable code!
Tobias Pfeiffer
@PragTob
pragtob.info
RUG::B
benchmarking
Open Source
Testing
Ruby Implementations
Sources
● The Pragmatic Programmer
● Smalltalk Best Practice Patterns
● Clean Code
● Practical Object Oriented Design in R...
Photo Credit
● http://officeimg.vo.msecnd.net/en-us/images/MP900439313.jpg
●
http://officeimg.vo.msecnd.net/en-us/images/M...
Code, Comments, Concepts, Comprehension – Conclusion?
Code, Comments, Concepts, Comprehension – Conclusion?
Code, Comments, Concepts, Comprehension – Conclusion?
Code, Comments, Concepts, Comprehension – Conclusion?
Code, Comments, Concepts, Comprehension – Conclusion?
Code, Comments, Concepts, Comprehension – Conclusion?
Code, Comments, Concepts, Comprehension – Conclusion?
Upcoming SlideShare
Loading in …5
×

Code, Comments, Concepts, Comprehension – Conclusion?

1,455 views

Published on

Clean & readable code as that's aguable more important than writing code really fast. I helps maintainability and so many other things.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Code, Comments, Concepts, Comprehension – Conclusion?

  1. 1. We write code
  2. 2. Isn't it more about reading?
  3. 3. Written once - read many times
  4. 4. (…) when you program, you have to think about how someone will read your code, not just how a computer will interpret it. Kent Beck
  5. 5. Any fool can write code that a computer can understand. Good programmers write code that humans can understand. Martin Fowler
  6. 6. Not about architecture
  7. 7. Methods & Code
  8. 8. Nurturing a code base
  9. 9. Extra effort
  10. 10. Save time!
  11. 11. Your code base?
  12. 12. It's about joy!
  13. 13. Code, Comments, Concepts, Comprehension – Conclusion? Tobias Pfeiffer @PragTob pragtob.info
  14. 14. Crazy?
  15. 15. Methods & Code
  16. 16. Keep It Simple Stupid
  17. 17. Are comments a code smell?
  18. 18. Comments are an excuse of the code that it could not be clearer.
  19. 19. Outdated comments are the worst
  20. 20. not the WHY WHAT
  21. 21. 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
  22. 22. Comments are the smell that tries to make other smells seem ok
  23. 23. # do one thing ... ... ... ... # do another thing ... ... ... ... # do something more ... ...
  24. 24. # do one thing ... ... ... ... # do another thing ... ... ... ... # do something more ... ...
  25. 25. # do one thing ... ... ... ... # do another thing ... ... ... ... # do something more ... ... Cocepts
  26. 26. Method too long
  27. 27. Short Methods
  28. 28. <= 8 LOC
  29. 29. Extract Methods
  30. 30. do_one_thing do_another_thing do_something_more Concepts
  31. 31. People disagree
  32. 32. "The more complex the code, the more comments it should have." You should make the code less complex not add more comments. Tiago Teixeira
  33. 33. # context, outlet, times, time per step, state, data def pattern(c, o, t, l, s, d) # ... end
  34. 34. Incomprehensible names
  35. 35. # context, outlet, times, time per step, state, data def pattern(c, o, t, l, s, d) # ... end
  36. 36. # context, outlet, times, time per step, state, data def pattern(c, o, t, l, s, d) # ... end
  37. 37. Explanatory names
  38. 38. Naming is hard
  39. 39. One Language
  40. 40. def pattern(context, outlet, time, time_per_step, state, data) # ... end
  41. 41. Argument order dependency
  42. 42. Try to keep it to 2 parameters
  43. 43. Example
  44. 44. # 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
  45. 45. # 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
  46. 46. # 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“
  47. 47. NON_ALCOHOLIC_DRINKS = ['cola', 'mate'] MIN_DRINKING_AGE = 18
  48. 48. # 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
  49. 49. # 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
  50. 50. # 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
  51. 51. Query method
  52. 52. Intention revealing method
  53. 53. # ... text.color = red # ...
  54. 54. # ... text.color = red # ...
  55. 55. # ... highlight(text) # ...
  56. 56. def highlight(text) text.color = red end
  57. 57. def highlight(text) text.color = red text.underline = true update_highlights end
  58. 58. # ... text.color = red text.underline = true update_highlights # ...
  59. 59. # ... highlight(text) # ...
  60. 60. # 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
  61. 61. # 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
  62. 62. # 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
  63. 63. # 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
  64. 64. if allowed_to_drink_alcohol?(customer) serve_drink requested_drink, customer else propose_non_alcoholic_drink end
  65. 65. „If you have a good name for a method you don't need to look at the body.“ Martin Fowler
  66. 66. „The easiest code to understand is the code you don't have to read at all.“ Tom Stuart (Berlin)
  67. 67. 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
  68. 68. 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
  69. 69. 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
  70. 70. 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
  71. 71. prepare_drink requested_drink price = requested_drink.price check = Check.new check.add_price price say 'That whill be ' + check.total
  72. 72. prepare_drink requested_drink price = requested_drink.price check = Check.new check.add_price price say 'That whill be ' + check.total
  73. 73. prepare_drink requested_drink price = requested_drink.price check = Check.new check.add_price price say 'That whill be ' + check.total
  74. 74. Same level of abstraction in a method
  75. 75. prepare_drink requested_drink prepare_check requested_drink
  76. 76. @left ||= 0 @top ||= 0 @width ||= 1.0 @height ||= 0 Code Formatting
  77. 77. double character: 'something weird', stateMask: CTRL | modifier, KeyCode: character.downcase.ord Code Formatting
  78. 78. 80 character width limit
  79. 79. 80 character width limit
  80. 80. 80 character width limit
  81. 81. 80 character width limit
  82. 82. 80 character width limit
  83. 83. Identify concepts
  84. 84. Don't Repeat Yourself
  85. 85. Don't Repeat Yourself (In Concepts)
  86. 86. Virtues
  87. 87. Simplicity
  88. 88. The least powerful construct
  89. 89. Explicit vs Implicit
  90. 90. person = Person.new(attributes) do_something(person) insert_in_db(person) Immutable Data
  91. 91. person = Person.new(attributes) person = do_something(person) insert_in_db(person) Immutable Data
  92. 92. Transformation of data
  93. 93. Reads like a book
  94. 94. Nurturing a code base
  95. 95. Code bases detoriate
  96. 96. No broken windows!
  97. 97. Magical time?
  98. 98. The boyscout rule
  99. 99. Opportunistic Refactoring
  100. 100. Confident Tests
  101. 101. 80% Code Coverage
  102. 102. 20% never executed
  103. 103. Code Review Culture
  104. 104. Always the code, never the person
  105. 105. Always the code, never the person Say something positive
  106. 106. Always the code, never the person Say something positive Use automated linters
  107. 107. Always the code, never the person Say something positive Use automated lintersUse automated linters Everyone reviews
  108. 108. Always the code, never the person Say something positive Use automated lintersUse automated linters Everyone reviews Pairing Review
  109. 109. Pair Programming
  110. 110. The power of magic hats
  111. 111. Reaping the benefits
  112. 112. When to break the rules
  113. 113. If you still like your code from two years ago, then you are not learning fast enough.
  114. 114. Enjoy writing readable code! Tobias Pfeiffer @PragTob pragtob.info
  115. 115. RUG::B benchmarking Open Source Testing Ruby Implementations
  116. 116. Sources ● The Pragmatic Programmer ● Smalltalk Best Practice Patterns ● Clean Code ● Practical Object Oriented Design in Ruby
  117. 117. 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/

×