0
Writing                          Testable Code                           Alvaro Videla - Cloud FoundryWednesday, April 10,...
About Me                     •    Cloud Foundry Developer Advocate                     •    Blog: http://videlalvaro.githu...
About Me                          Co-author               RabbitMQ in Action               http://bit.ly/rabbitmqWednesday...
I’m not a:Wednesday, April 10, 13
I’m not a:                     • Application Testing GuruWednesday, April 10, 13
I’m not a:                     • Application Testing Guru                     • TDD AdvocateWednesday, April 10, 13
Why is it so hard                           to write tests?Wednesday, April 10, 13
Unit Testing                   The goal of unit testing is                    to isolate each part of                    t...
Unit Testing               […] unit testing by definition only               tests the functionality of the units          ...
Unit Testing                     […] Therefore, it will not catch                      integration errors or broader      ...
Dogma                            vs.                          RealityWednesday, April 10, 13
A world of                          Trade OffsWednesday, April 10, 13
What should                       we test?Wednesday, April 10, 13
How much                           should                           we test?Wednesday, April 10, 13
“I get paid for code that works,                 not for tests, so my philosophy is                   to test as little as...
The Hidden                            Secret                           Of TDDWednesday, April 10, 13
The Secret of TDDWednesday, April 10, 13
The Secret of TDDWednesday, April 10, 13
Some books by Kent BeckWednesday, April 10, 13
To write good                   tests first we need                    to learn how to                        programWednes...
Wednesday, April 10, 13
We developers are                 like those users we                 like to complain so                     much aboutWe...
Design evolves and                 matures with timeWednesday, April 10, 13
Good Code sits in                      the small detailsWednesday, April 10, 13
TIPSWednesday, April 10, 13
Separate pure code                     from impure or statefulWednesday, April 10, 13
Pure FunctionsWednesday, April 10, 13
Pure Functions                     • Referential TransparencyWednesday, April 10, 13
Pure Functions                     • Referential Transparency                     • Don’t modify external stateWednesday, ...
Pure Functions                     • Referential Transparency                     • Don’t modify external state           ...
What’s wrong with this code?                          if($player->getScore() > 0) {                            $player->se...
What’s wrong with this code?         $newScore = $player->getScore() > 0                                                ? ...
Score calculation                   can be moved into                    its own functionWednesday, April 10, 13
Score calculation                    can be tested nowWednesday, April 10, 13
First write                          Pure CodeWednesday, April 10, 13
Add impure code                    step by step when                         neededWednesday, April 10, 13
Write                          Composable                            CodeWednesday, April 10, 13
Function Composition                           http://en.wikipedia.org/wiki/Function_(mathematics)Wednesday, April 10, 13
Function Composition                           http://en.wikipedia.org/wiki/Function_(mathematics)Wednesday, April 10, 13
This looks familiarWednesday, April 10, 13
“Many UNIX programs do quite                    trivial tasks in isolation, but,                   combined with other pro...
Number of open connections per IP           netstat -ntu | awk {print $5} |            cut -d: -f1 | sort | uniq -c | sort...
Why don’t we just                          code in this style?Wednesday, April 10, 13
This seems familiar                                again…Wednesday, April 10, 13
Welcome to                           Functional                          ProgrammingWednesday, April 10, 13
“Writing unit tests is reinventing                     functional programming                  in non-functional languages...
What can we learn from                          Functional Programming?Wednesday, April 10, 13
The proper use of TypesWednesday, April 10, 13
What does ‘null’ mean?Wednesday, April 10, 13
What does                          ‘true|false’ mean?Wednesday, April 10, 13
Functions with just one                              responsibilityWednesday, April 10, 13
Radical separation of pure                      code from impure codeWednesday, April 10, 13
Let’s see an exampleWednesday, April 10, 13
Food for Thought                          http://thinking-forth.sourceforge.netWednesday, April 10, 13
“Inside every well-                          written large program                          is a well-written small       ...
Questions?Wednesday, April 10, 13
Thanks!                              http://twitter.com/old_sound                              http://github.com/videlalva...
Upcoming SlideShare
Loading in...5
×

Writing testable code

4,941

Published on

We've been told many times that we should write unit tests for our code. We have read the theory and we have applied automatic testing to our projects, sometimes successfully but often times not so.

Why it seems to be so hard to test our code? However we look at it, automatic testing doesn't work like a "plug & play" peripheral. It just doesn't seem to fit with our project. A dependency is missing here; we have a hard to mock object there; and so on.

What is _that_ thing we might be doing wrong but we fail to notice?

In this talk we will argue that the problem lays in our code, in its structure, in the way we pass data around and even how we write for loops!

This won't be your everyday "code quality" tech talk, since we are going to attack the problem of code quality from different points of view and paradigms like Functional Programming and the Unix philosophy of simplicity and reuse.

Published in: Technology

Transcript of "Writing testable code"

  1. 1. Writing Testable Code Alvaro Videla - Cloud FoundryWednesday, April 10, 13
  2. 2. About Me • Cloud Foundry Developer Advocate • Blog: http://videlalvaro.github.com/ • Twitter: @old_soundWednesday, April 10, 13
  3. 3. About Me Co-author RabbitMQ in Action http://bit.ly/rabbitmqWednesday, April 10, 13
  4. 4. I’m not a:Wednesday, April 10, 13
  5. 5. I’m not a: • Application Testing GuruWednesday, April 10, 13
  6. 6. I’m not a: • Application Testing Guru • TDD AdvocateWednesday, April 10, 13
  7. 7. Why is it so hard to write tests?Wednesday, April 10, 13
  8. 8. Unit Testing The goal of unit testing is to isolate each part of the program and show that the individual parts are correct http://en.wikipedia.org/wiki/Unit_testingWednesday, April 10, 13
  9. 9. Unit Testing […] unit testing by definition only tests the functionality of the units themselves. http://en.wikipedia.org/wiki/Unit_testingWednesday, April 10, 13
  10. 10. Unit Testing […] Therefore, it will not catch integration errors or broader system-level errors (such as functions performed across multiple units, or non-functional test areas such as performance) http://en.wikipedia.org/wiki/Unit_testingWednesday, April 10, 13
  11. 11. Dogma vs. RealityWednesday, April 10, 13
  12. 12. A world of Trade OffsWednesday, April 10, 13
  13. 13. What should we test?Wednesday, April 10, 13
  14. 14. How much should we test?Wednesday, April 10, 13
  15. 15. “I get paid for code that works, not for tests, so my philosophy is to test as little as possible to reach a given level of confidence” – Kent Beck http://stackoverflow.com/questions/153234/how-deep-are-your-unit-tests/153565#153565Wednesday, April 10, 13
  16. 16. The Hidden Secret Of TDDWednesday, April 10, 13
  17. 17. The Secret of TDDWednesday, April 10, 13
  18. 18. The Secret of TDDWednesday, April 10, 13
  19. 19. Some books by Kent BeckWednesday, April 10, 13
  20. 20. To write good tests first we need to learn how to programWednesday, April 10, 13
  21. 21. Wednesday, April 10, 13
  22. 22. We developers are like those users we like to complain so much aboutWednesday, April 10, 13
  23. 23. Design evolves and matures with timeWednesday, April 10, 13
  24. 24. Good Code sits in the small detailsWednesday, April 10, 13
  25. 25. TIPSWednesday, April 10, 13
  26. 26. Separate pure code from impure or statefulWednesday, April 10, 13
  27. 27. Pure FunctionsWednesday, April 10, 13
  28. 28. Pure Functions • Referential TransparencyWednesday, April 10, 13
  29. 29. Pure Functions • Referential Transparency • Don’t modify external stateWednesday, April 10, 13
  30. 30. Pure Functions • Referential Transparency • Don’t modify external state • Don’t produce side effectsWednesday, April 10, 13
  31. 31. What’s wrong with this code? if($player->getScore() > 0) { $player->setSwizzle(7); } else { $player->setSwizzle( $player->getSwizzle() + 1 ); } https://dl.dropboxusercontent.com/u/7810909/docs/what-does-fp-mean/what-does-fp-mean/chunk-html/ar01s05.htmlWednesday, April 10, 13
  32. 32. What’s wrong with this code? $newScore = $player->getScore() > 0 ? 7 : $player->getSwizzle() + 1; $player->setSwizzle($newScore); https://dl.dropboxusercontent.com/u/7810909/docs/what-does-fp-mean/what-does-fp-mean/chunk-html/ar01s05.htmlWednesday, April 10, 13
  33. 33. Score calculation can be moved into its own functionWednesday, April 10, 13
  34. 34. Score calculation can be tested nowWednesday, April 10, 13
  35. 35. First write Pure CodeWednesday, April 10, 13
  36. 36. Add impure code step by step when neededWednesday, April 10, 13
  37. 37. Write Composable CodeWednesday, April 10, 13
  38. 38. Function Composition http://en.wikipedia.org/wiki/Function_(mathematics)Wednesday, April 10, 13
  39. 39. Function Composition http://en.wikipedia.org/wiki/Function_(mathematics)Wednesday, April 10, 13
  40. 40. This looks familiarWednesday, April 10, 13
  41. 41. “Many UNIX programs do quite trivial tasks in isolation, but, combined with other programs, become general and useful tools.” http://math.albany.edu/math/pers/hammond/unixphil.htmlWednesday, April 10, 13
  42. 42. Number of open connections per IP netstat -ntu | awk {print $5} | cut -d: -f1 | sort | uniq -c | sort -n http://www.commandlinefu.com/commands/view/1767/number-of-open-connections-per-ip.Wednesday, April 10, 13
  43. 43. Why don’t we just code in this style?Wednesday, April 10, 13
  44. 44. This seems familiar again…Wednesday, April 10, 13
  45. 45. Welcome to Functional ProgrammingWednesday, April 10, 13
  46. 46. “Writing unit tests is reinventing functional programming in non-functional languages” http://noss.github.io/2009/02/25/writing-unit-tests-is-reinventing-functional-programming-in-non-functional-languages.htmlWednesday, April 10, 13
  47. 47. What can we learn from Functional Programming?Wednesday, April 10, 13
  48. 48. The proper use of TypesWednesday, April 10, 13
  49. 49. What does ‘null’ mean?Wednesday, April 10, 13
  50. 50. What does ‘true|false’ mean?Wednesday, April 10, 13
  51. 51. Functions with just one responsibilityWednesday, April 10, 13
  52. 52. Radical separation of pure code from impure codeWednesday, April 10, 13
  53. 53. Let’s see an exampleWednesday, April 10, 13
  54. 54. Food for Thought http://thinking-forth.sourceforge.netWednesday, April 10, 13
  55. 55. “Inside every well- written large program is a well-written small program” http://www.linfo.org/q_programming.htmlWednesday, April 10, 13
  56. 56. Questions?Wednesday, April 10, 13
  57. 57. Thanks! http://twitter.com/old_sound http://github.com/videlalvaro http://www.slideshare.net/old_soundWednesday, April 10, 13
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×