Code, Comments, Concepts, Comprehension – Conclusion?

Code, Comments, Concepts, Comprehension – Conclusion?

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

8480daec7137f28565bc2d2e666b915a?s=128

Tobias Pfeiffer

April 18, 2017
Tweet

Transcript

  1. We write code

  2. Isn't it more about reading?

  3. Written once - read many times

  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. Any fool can write code that a computer can understand.

    Good programmers write code that humans can understand. Martin Fowler
  6. Not about architecture

  7. Methods & Code

  8. Nurturing a code base

  9. Extra effort

  10. Save time!

  11. Your code base?

  12. None
  13. It's about joy!

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

  15. Crazy?

  16. Methods & Code

  17. None
  18. Keep It Simple Stupid

  19. Are comments a code smell?

  20. Comments are an excuse of the code that it could

    not be clearer.
  21. Outdated comments are the worst

  22. not the WHY WHAT

  23. 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
  24. Comments are the smell that tries to make other smells

    seem ok
  25. # do one thing ... ... ... ... # do

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

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

    another thing ... ... ... ... # do something more ... ... Cocepts
  28. Method too long

  29. Short Methods

  30. <= 8 LOC

  31. Extract Methods

  32. do_one_thing do_another_thing do_something_more Concepts

  33. People disagree

  34. None
  35. "The more complex the code, the more comments it should

    have." You should make the code less complex not add more comments. Tiago Teixeira
  36. # context, outlet, times, time per step, state, data def

    pattern(c, o, t, l, s, d) # ... end
  37. Incomprehensible names

  38. # context, outlet, times, time per step, state, data def

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

    pattern(c, o, t, l, s, d) # ... end
  40. Explanatory names

  41. Naming is hard

  42. One Language

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

  44. Argument order dependency

  45. Try to keep it to 2 parameters

  46. Example

  47. # 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
  48. # 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
  49. # 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“
  50. NON_ALCOHOLIC_DRINKS = ['cola', 'mate'] MIN_DRINKING_AGE = 18

  51. # 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
  52. # 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
  53. # 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
  54. Query method

  55. Intention revealing method

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

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

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

  59. def highlight(text) text.color = red end

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

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

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

  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. # 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
  65. # 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
  66. # 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
  67. if allowed_to_drink_alcohol?(customer) serve_drink requested_drink, customer else propose_non_alcoholic_drink end

  68. „If you have a good name for a method you

    don't need to look at the body.“ Martin Fowler
  69. „The easiest code to understand is the code you don't

    have to read at all.“ Tom Stuart (Berlin)
  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 # 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
  71. 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
  72. 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
  73. 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
  74. prepare_drink requested_drink price = requested_drink.price check = Check.new check.add_price price

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

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

    say 'That whill be ' + check.total
  77. Same level of abstraction in a method

  78. prepare_drink requested_drink prepare_check requested_drink

  79. @left ||= 0 @top ||= 0 @width ||= 1.0 @height

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

    Code Formatting
  81. 80 character width limit

  82. 80 character width limit

  83. 80 character width limit

  84. 80 character width limit

  85. 80 character width limit

  86. Identify concepts

  87. Don't Repeat Yourself

  88. Don't Repeat Yourself (In Concepts)

  89. Virtues

  90. Simplicity

  91. The least powerful construct

  92. Explicit vs Implicit

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

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

  95. Transformation of data

  96. Reads like a book

  97. Nurturing a code base

  98. Code bases detoriate

  99. No broken windows!

  100. None
  101. None
  102. None
  103. None
  104. Magical time?

  105. The boyscout rule

  106. Opportunistic Refactoring

  107. Confident Tests

  108. 80% Code Coverage

  109. 20% never executed

  110. Code Review Culture

  111. Always the code, never the person

  112. Always the code, never the person Say something positive

  113. Always the code, never the person Say something positive Use

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

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

    automated linters Use automated linters Everyone reviews Pairing Review
  116. Pair Programming

  117. The power of magic hats

  118. Reaping the benefits

  119. When to break the rules

  120. If you still like your code from two years ago,

    then you are not learning fast enough.
  121. Enjoy writing readable code! Tobias Pfeiffer @PragTob pragtob.info

  122. RUG::B benchmarking Open Source Testing Ruby Implementations

  123. Sources • The Pragmatic Programmer • Smalltalk Best Practice Patterns

    • Clean Code • Practical Object Oriented Design in Ruby
  124. 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/