Successfully reported this slideshow.

Writing testable code

21

Share

Upcoming SlideShare
Writing the docs
Writing the docs
Loading in …3
×
1 of 57
1 of 57

Writing testable code

21

Share

Download to read offline

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.

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.

More Related Content

Related Books

Free with a 14 day trial from Scribd

See all

Writing testable code

  1. 1. Writing Testable Code Alvaro Videla - Cloud Foundry Wednesday, April 10, 13
  2. 2. About Me • Cloud Foundry Developer Advocate • Blog: http://videlalvaro.github.com/ • Twitter: @old_sound Wednesday, April 10, 13
  3. 3. About Me Co-author RabbitMQ in Action http://bit.ly/rabbitmq Wednesday, April 10, 13
  4. 4. I’m not a: Wednesday, April 10, 13
  5. 5. I’m not a: • Application Testing Guru Wednesday, April 10, 13
  6. 6. I’m not a: • Application Testing Guru • TDD Advocate Wednesday, 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_testing Wednesday, 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_testing Wednesday, 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_testing Wednesday, April 10, 13
  11. 11. Dogma vs. Reality Wednesday, April 10, 13
  12. 12. A world of Trade Offs Wednesday, 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#153565 Wednesday, April 10, 13
  16. 16. The Hidden Secret Of TDD Wednesday, April 10, 13
  17. 17. The Secret of TDD Wednesday, April 10, 13
  18. 18. The Secret of TDD Wednesday, April 10, 13
  19. 19. Some books by Kent Beck Wednesday, April 10, 13
  20. 20. To write good tests first we need to learn how to program Wednesday, April 10, 13
  21. 21. Wednesday, April 10, 13
  22. 22. We developers are like those users we like to complain so much about Wednesday, April 10, 13
  23. 23. Design evolves and matures with time Wednesday, April 10, 13
  24. 24. Good Code sits in the small details Wednesday, April 10, 13
  25. 25. TIPS Wednesday, April 10, 13
  26. 26. Separate pure code from impure or stateful Wednesday, April 10, 13
  27. 27. Pure Functions Wednesday, April 10, 13
  28. 28. Pure Functions • Referential Transparency Wednesday, April 10, 13
  29. 29. Pure Functions • Referential Transparency • Don’t modify external state Wednesday, April 10, 13
  30. 30. Pure Functions • Referential Transparency • Don’t modify external state • Don’t produce side effects Wednesday, 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.html Wednesday, 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.html Wednesday, April 10, 13
  33. 33. Score calculation can be moved into its own function Wednesday, April 10, 13
  34. 34. Score calculation can be tested now Wednesday, April 10, 13
  35. 35. First write Pure Code Wednesday, April 10, 13
  36. 36. Add impure code step by step when needed Wednesday, April 10, 13
  37. 37. Write Composable Code Wednesday, 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 familiar Wednesday, 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.html Wednesday, 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 Programming Wednesday, 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.html Wednesday, April 10, 13
  47. 47. What can we learn from Functional Programming? Wednesday, April 10, 13
  48. 48. The proper use of Types Wednesday, 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 responsibility Wednesday, April 10, 13
  52. 52. Radical separation of pure code from impure code Wednesday, April 10, 13
  53. 53. Let’s see an example Wednesday, April 10, 13
  54. 54. Food for Thought http://thinking-forth.sourceforge.net Wednesday, April 10, 13
  55. 55. “Inside every well- written large program is a well-written small program” http://www.linfo.org/q_programming.html Wednesday, 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_sound Wednesday, April 10, 13

×