writing testable code
play

Writing Testable Code Alvaro Videla - Cloud Foundry About Me - PowerPoint PPT Presentation

Writing Testable Code Alvaro Videla - Cloud Foundry About Me Cloud Foundry Developer Advocate Blog: http://videlalvaro.github.com/ Twitter: @old_sound About Me Co-author RabbitMQ in Action http://bit.ly/rabbitmq Im not a:


  1. Writing Testable Code Alvaro Videla - Cloud Foundry

  2. About Me • Cloud Foundry Developer Advocate • Blog: http://videlalvaro.github.com/ • Twitter: @old_sound

  3. About Me Co-author RabbitMQ in Action http://bit.ly/rabbitmq

  4. I’m not a:

  5. I’m not a: • Application Testing Guru

  6. I’m not a: • Application Testing Guru • TDD Advocate

  7. Why is it so hard to write tests?

  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

  9. Unit Testing […] unit testing by definition only tests the functionality of the units themselves. http://en.wikipedia.org/wiki/Unit_testing

  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

  11. Dogma vs. Reality

  12. A world of Trade Offs

  13. What should we test?

  14. How much should we test?

  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

  16. The Hidden Secret Of TDD

  17. The Secret of TDD

  18. The Secret of TDD

  19. Some books by Kent Beck

  20. To write good tests first we need to learn how to program

  21. We developers are like those users we like to complain so much about

  22. Design evolves and matures with time

  23. Good Code sits in the small details

  24. TIPS

  25. Separate pure code from impure or stateful

  26. Pure Functions

  27. Pure Functions • Referential Transparency

  28. Pure Functions • Referential Transparency • Don’t modify external state

  29. Pure Functions • Referential Transparency • Don’t modify external state • Don’t produce side effects

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

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

  32. Score calculation can be moved into its own function

  33. Score calculation can be tested now

  34. First write Pure Code

  35. Add impure code step by step when needed

  36. Write Composable Code

  37. Function Composition http://en.wikipedia.org/wiki/Function_(mathematics)

  38. Function Composition http://en.wikipedia.org/wiki/Function_(mathematics)

  39. This looks familiar

  40. “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

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

  42. Why don’t we just code in this style?

  43. This seems familiar again…

  44. Welcome to Functional Programming

  45. “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

  46. What can we learn from Functional Programming?

  47. The proper use of Types

  48. What does ‘ null ’ mean?

  49. What does ‘ true|false ’ mean?

  50. Functions with just one responsibility

  51. Radical separation of pure code from impure code

  52. Let’s see an example

  53. Food for Thought http://thinking-forth.sourceforge.net

  54. “Inside every well- written large program is a well-written small program” http://www.linfo.org/q_programming.html

  55. Questions?

  56. Thanks! http://twitter.com/old_sound http://github.com/videlalvaro http://www.slideshare.net/old_sound

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend