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

Code is read many more times than written

Code is read many more times than written

"Code is read many more times than written." - this statement changed my view of Software Engineering. From that point on I made it my personal mission to write the best code I could possibly write.
This talk is about my very favorite coding wisdoms that I picked up on my journey so far. All of these will help you to write better code, be more productive and have more fun. And good code pays it's interest rate every day.
Also if you're a clean code junkie looking for a fix - this talk is for you.

Tobias Pfeiffer

September 05, 2013
Tweet

More Decks by Tobias Pfeiffer

Other Decks in Programming

Transcript

  1. We write code

    View Slide

  2. Isn't it more about
    reading?

    View Slide

  3. Written once – read many
    times

    View Slide

  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

    View Slide

  5. Not about
    Architecture

    View Slide

  6. Methods & Code

    View Slide

  7. Extra effort

    View Slide

  8. Save time

    View Slide

  9. Your code base?

    View Slide

  10. View Slide

  11. It's about joy!

    View Slide

  12. Code is read many more times
    than written
    Tobias Pfeiffer
    @PragTob
    pragtob.wordpress.com

    View Slide

  13. Sources

    View Slide

  14. Crazy?

    View Slide

  15. Are comments a smell?

    View Slide

  16. Outdated comments are the
    worst

    View Slide

  17. The why not the what

    View Slide

  18. Comments are an excuse of
    the code that it could not be
    clearer.

    View Slide

  19. # do one thing



    # do another thing



    # do something more


    View Slide

  20. Extract Methods

    View Slide

  21. do_one_thing
    do_another_thing
    do_something_more

    View Slide

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

    View Slide

  23. Explanatory and meaningful
    names

    View Slide

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

    View Slide

  25. Try to keep it to 2 parameters

    View Slide

  26. Argument order dependency

    View Slide

  27. No magic numbers

    View Slide

  28. Explanatory variable

    View Slide

  29. Short Methods (<= 8 LOC)

    View Slide

  30. # allowed to drink?
    if customer.age > 18
    say 'Okay'
    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

    View Slide

  31. # allowed to drink?
    if customer.age > 18
    say 'Okay'
    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

    View Slide

  32. Query method

    View Slide

  33. Intention revealing method

    View Slide

  34. # …
    text.color = red
    # …

    View Slide

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

    View Slide

  36. # …
    highlight(text)
    # …

    View Slide

  37. if customer.age > 18
    say 'Okay'
    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

    View Slide

  38. if customer.age > 18
    say 'Okay'
    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

    View Slide

  39. if customer.age > 18
    say 'Okay'
    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

    View Slide

  40. if customer.age > 18
    say 'Okay'
    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

    View Slide

  41. if allowed_to_drink_alcohol?(customer)
    serve_drink requested_drink,
    customer
    else
    propose_non_alcoholic_drink
    end

    View Slide

  42. „The easiest code to
    understand is the code you
    don't have to read at all.“
    Tom Stuart (Berlin)

    View Slide

  43. Same level of abstraction in a
    method

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  47. prepare_drink requested_drink
    prepare_check requested_drink

    View Slide

  48. DRY

    View Slide

  49. Nice code formatting

    View Slide

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

    View Slide

  51. double character: 'something weird',
    stateMask: CTRL | modifier,
    KeyCode: character.downcase.ord

    View Slide

  52. 80 character
    Width limit

    View Slide

  53. 80 character
    Width limit

    View Slide

  54. 80 character
    Width limit

    View Slide

  55. 80 character
    Width limit

    View Slide

  56. 80 character
    Width limit

    View Slide

  57. Don't program by coincedence

    View Slide

  58. Code bases detoriate

    View Slide

  59. No broken windows

    View Slide

  60. View Slide

  61. View Slide

  62. Magical time?

    View Slide

  63. The Boyscout
    Rule

    View Slide

  64. View Slide

  65. TDD

    View Slide

  66. 80% Code Coverage

    View Slide

  67. 20% is never executed

    View Slide

  68. „Incoming messages should be tested for
    the state they return. Outgoing
    command messages should be tested
    to ensure they get sent. Outgoing query
    messages should not be tested.“
    Sandi Metz

    View Slide

  69. Know when to
    break the rules

    View Slide

  70. If you still like your code from
    two years ago,
    then you are not learning fast
    enough.

    View Slide

  71. Enjoy Coding!
    Tobias Pfeiffer
    @PragTob
    pragtob.wordpress.com

    View Slide

  72. Sources

    The Pragmatic Programmer

    Smalltalk Best Practice Patterns

    Clean Code

    Practical Object Oriented Design in Ruby

    View Slide

  73. Photo Credit

    http://officeimg.vo.msecnd.net/en-us/images/MP900439313.jpg

    http://officeimg.vo.msecnd.net/en-us/images/MC900021328.wmf

    (CC BY-SA 2.0)
    – http://www.flickr.com/photos/[email protected]/7658272558/in/photostream/
    – http://www.flickr.com/photos/[email protected]/7658165122/
    – http://en.wikipedia.org/wiki/File:Kent_Beck_no_Workshop_Mapping_XP.jpg

    (CC BY-NC-ND 2.0)
    – http://www.flickr.com/photos/andih/86577529/
    – http://www.flickr.com/photos/[email protected]/3293117576/
    – http://www.flickr.com/photos/jasonlparks/4525188865/
    – http://www.flickr.com/photos/[email protected]/2293045156/

    http://www.flickr.com/photos/[email protected]/5488791911/(CC BY-ND 2.0)

    (CC BY 2.0)
    – http://www.flickr.com/photos/barry_b/76055201/
    – http://www.flickr.com/photos/[email protected]/7725273678/
    – http://www.flickr.com/photos/[email protected]/3187186308/

    (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/

    (CC BY-NC 2.0)
    – http://www.flickr.com/photos/[email protected]/5757983532/
    – http://www.flickr.com/photos/sevendead/5650065458/

    View Slide

  74. Source pictures

    http://imagery.pragprog.com/products/59/tpp.jpg

    http://www.informit.com/ShowCover.aspx?isbn=9780132852111&type=f

    http://coding-in.net/blog/wp-content/uploads/clean_code.jpg

    http://www.informit.com/ShowCover.aspx?isbn=9780321721334&type=f

    http://mendicantuniversity.org/images/logo.png

    View Slide