Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Code, Comments, Concepts, Comprehension – Concl...

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.

Tobias Pfeiffer

April 18, 2017
Tweet

More Decks by Tobias Pfeiffer

Other Decks in Programming

Transcript

  1. (…) when you program, you have to think about how

    someone will read your code, not just how a computer will interpret it. Kent Beck
  2. Any fool can write code that a computer can understand.

    Good programmers write code that humans can understand. Martin Fowler
  3. 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
  4. # do one thing ... ... ... ... # do

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

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

    another thing ... ... ... ... # do something more ... ... Cocepts
  7. "The more complex the code, the more comments it should

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

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

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

    pattern(c, o, t, l, s, d) # ... end
  11. # 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
  12. # 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
  13. # 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“
  14. # 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
  15. # 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
  16. # 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
  17. # 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
  18. # 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
  19. # 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
  20. # 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
  21. „If you have a good name for a method you

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

    have to read at all.“ Tom Stuart (Berlin)
  23. 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
  24. 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
  25. 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
  26. 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
  27. Always the code, never the person Say something positive Use

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

    automated linters Use automated linters Everyone reviews Pairing Review
  29. If you still like your code from two years ago,

    then you are not learning fast enough.
  30. Sources • The Pragmatic Programmer • Smalltalk Best Practice Patterns

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