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....
Not about
Architecture
Methods & Code
Extra effort
Save time
Your code base?
It's about joy!
Code is read many more times
than written
Tobias Pfeiffer
@PragTob
pragtob.wordpress.com
Sources
Crazy?
Are comments a smell?
Outdated comments are the
worst
The why not the what
Comments are an excuse of
the code that it could not be
clearer.
# do one thing
…
…
…
# do another thing
…
…
…
# do something more
…
…
Extract Methods
do_one_thing
do_another_thing
do_something_more
# context, outlet, times, time per
step, state, data
def pattern(c, o, t, l, s, d)
# …
end
Explanatory and meaningful
names
def pattern(context, outlet, time,
time_per_step, state, data)
# …
end
Try to keep it to 2 parameters
Argument order dependency
No magic numbers
Explanatory variable
Short Methods (<= 8 LOC)
# allowed to drink?
if customer.age > 18
say 'Okay'
prepare_drink requested_drink
say 'here you go'
hand_drink_over drink,...
# allowed to drink?
if customer.age > 18
say 'Okay'
prepare_drink requested_drink
say 'here you go'
hand_drink_over drink,...
Query method
Intention revealing method
# …
text.color = red
# …
def highlight(text)
text.color = red
end
# …
highlight(text)
# …
if customer.age > 18
say 'Okay'
prepare_drink requested_drink
say 'here you go'
hand_drink_over drink, customer
else
say '...
if customer.age > 18
say 'Okay'
prepare_drink requested_drink
say 'here you go'
hand_drink_over drink, customer
else
say '...
if customer.age > 18
say 'Okay'
prepare_drink requested_drink
say 'here you go'
hand_drink_over drink, customer
else
say '...
if customer.age > 18
say 'Okay'
prepare_drink requested_drink
say 'here you go'
hand_drink_over drink, customer
else
say '...
if allowed_to_drink_alcohol?(customer)
serve_drink requested_drink,
customer
else
propose_non_alcoholic_drink
end
„The easiest code to
understand is the code you
don't have to read at all.“
Tom Stuart (Berlin)
Same level of abstraction in a
method
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 ' +...
prepare_drink requested_drink
prepare_check requested_drink
DRY
Nice code formatting
@left ||= 0
@top ||= 0
@width ||= 1.0
@height ||= 0
double character: 'something weird',
stateMask: CTRL | modifier,
KeyCode: character.downcase.ord
80 character
Width limit
80 character
Width limit
80 character
Width limit
80 character
Width limit
80 character
Width limit
Don't program by coincedence
Code bases detoriate
No broken windows
Magical time?
The Boyscout
Rule
TDD
80% Code Coverage
20% is never executed
„Incoming messages should be tested for
the state they return. Outgoing
command messages should be tested
to ensure they g...
Know when to
break the rules
If you still like your code from
two years ago,
then you are not learning fast
enough.
Enjoy Coding!
Tobias Pfeiffer
@PragTob
pragtob.wordpress.com
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...
Source pictures
● http://imagery.pragprog.com/products/59/tpp.jpg
● http://www.informit.com/ShowCover.aspx?isbn=9780132852...
Code is read many more times than written
Code is read many more times than written
Code is read many more times than written
Code is read many more times than written
Upcoming SlideShare
Loading in …5
×

Code is read many more times than written

717 views
556 views

Published on

"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.

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
717
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
7
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Code is read many more times than written

  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. Not about Architecture
  6. 6. Methods & Code
  7. 7. Extra effort
  8. 8. Save time
  9. 9. Your code base?
  10. 10. It's about joy!
  11. 11. Code is read many more times than written Tobias Pfeiffer @PragTob pragtob.wordpress.com
  12. 12. Sources
  13. 13. Crazy?
  14. 14. Are comments a smell?
  15. 15. Outdated comments are the worst
  16. 16. The why not the what
  17. 17. Comments are an excuse of the code that it could not be clearer.
  18. 18. # do one thing … … … # do another thing … … … # do something more … …
  19. 19. Extract Methods
  20. 20. do_one_thing do_another_thing do_something_more
  21. 21. # context, outlet, times, time per step, state, data def pattern(c, o, t, l, s, d) # … end
  22. 22. Explanatory and meaningful names
  23. 23. def pattern(context, outlet, time, time_per_step, state, data) # … end
  24. 24. Try to keep it to 2 parameters
  25. 25. Argument order dependency
  26. 26. No magic numbers
  27. 27. Explanatory variable
  28. 28. Short Methods (<= 8 LOC)
  29. 29. # 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
  30. 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
  31. 31. Query method
  32. 32. Intention revealing method
  33. 33. # … text.color = red # …
  34. 34. def highlight(text) text.color = red end
  35. 35. # … highlight(text) # …
  36. 36. 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
  37. 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
  38. 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
  39. 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
  40. 40. if allowed_to_drink_alcohol?(customer) serve_drink requested_drink, customer else propose_non_alcoholic_drink end
  41. 41. „The easiest code to understand is the code you don't have to read at all.“ Tom Stuart (Berlin)
  42. 42. Same level of abstraction in a method
  43. 43. prepare_drink requested_drink price = requested_drink.price Check = Check.new check.add_price price say 'That whill be ' + check.total
  44. 44. prepare_drink requested_drink price = requested_drink.price Check = Check.new check.add_price price say 'That whill be ' + check.total
  45. 45. prepare_drink requested_drink price = requested_drink.price Check = Check.new check.add_price price say 'That whill be ' + check.total
  46. 46. prepare_drink requested_drink prepare_check requested_drink
  47. 47. DRY
  48. 48. Nice code formatting
  49. 49. @left ||= 0 @top ||= 0 @width ||= 1.0 @height ||= 0
  50. 50. double character: 'something weird', stateMask: CTRL | modifier, KeyCode: character.downcase.ord
  51. 51. 80 character Width limit
  52. 52. 80 character Width limit
  53. 53. 80 character Width limit
  54. 54. 80 character Width limit
  55. 55. 80 character Width limit
  56. 56. Don't program by coincedence
  57. 57. Code bases detoriate
  58. 58. No broken windows
  59. 59. Magical time?
  60. 60. The Boyscout Rule
  61. 61. TDD
  62. 62. 80% Code Coverage
  63. 63. 20% is never executed
  64. 64. „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
  65. 65. Know when to break the rules
  66. 66. If you still like your code from two years ago, then you are not learning fast enough.
  67. 67. Enjoy Coding! Tobias Pfeiffer @PragTob pragtob.wordpress.com
  68. 68. Sources ● The Pragmatic Programmer ● Smalltalk Best Practice Patterns ● Clean Code ● Practical Object Oriented Design in Ruby
  69. 69. 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/83633410@N07/7658272558/in/photostream/ – http://www.flickr.com/photos/83633410@N07/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/12584908@N08/3293117576/ – http://www.flickr.com/photos/jasonlparks/4525188865/ – http://www.flickr.com/photos/20714221@N04/2293045156/ ● 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/ ● (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/37996583811@N01/5757983532/ – http://www.flickr.com/photos/sevendead/5650065458/
  70. 70. 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

×