Unit Test + Functional Programming = Love

12,503 views

Published on

Talk given at the SunshinePHP Uncon, about unit testing, unit testing archaeology and functional programming

Published in: Technology
1 Comment
15 Likes
Statistics
Notes
No Downloads
Views
Total views
12,503
On SlideShare
0
From Embeds
0
Number of Embeds
200
Actions
Shares
0
Downloads
71
Comments
1
Likes
15
Embeds 0
No embeds

No notes for slide

Unit Test + Functional Programming = Love

  1. 1. Unit Testing + Functional Programming = <3 Alvaro Videla - RabbitMQ
  2. 2. About Me • RabbitMQ Developer Advocate • Blog: http://videlalvaro.github.io/ • Twitter: @old_sound
  3. 3. About Me Co-author ! RabbitMQ in Action http://bit.ly/rabbitmq
  4. 4. I’m not a:
  5. 5. I’m not a: • Application Testing Guru
  6. 6. I’m not a: • Application Testing Guru • TDD Advocate
  7. 7. Why is it so hard to write tests?
  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
  9. 9. Unit Testing […] unit testing by definition only tests the functionality of the units themselves. http://en.wikipedia.org/wiki/Unit_testing
  10. 10. Unit Testing […] Therefore, it will not catch integration errors or broader systemlevel errors (such as functions performed across multiple units, or non-functional test areas such as performance) http://en.wikipedia.org/wiki/Unit_testing
  11. 11. Back to 1976
  12. 12. Test procedures: A new approach to software verification David J. Panzl http://dl.acm.org/citation.cfm?id=807721&dl=ACM&coll=DL&CFID=290373956&CFTOKEN=26471516
  13. 13. Test Procedures We propose a new medium for software verification tests. Tests described in this medium are complete, compact and fully self-contained http://dl.acm.org/citation.cfm?id=807721&dl=ACM&coll=DL&CFID=290373956&CFTOKEN=26471516
  14. 14. Test Procedures No additional materials, instructions or knowledge about the target program are required to apply these tests. http://dl.acm.org/citation.cfm?id=807721&dl=ACM&coll=DL&CFID=290373956&CFTOKEN=26471516
  15. 15. Forward to 1982
  16. 16. http://www.amazon.com/Software-Testing-Techniques-2nd-Edition/dp/1850328803/
  17. 17. Unit, Unit Testing A unit is the smallest testable piece of software http://www.amazon.com/Software-Testing-Techniques-2nd-Edition/dp/1850328803/
  18. 18. Forward to 1986
  19. 19. http://www.amazon.com/Abstraction-Specification-Development-Electrical-Engineering/dp/0262121123/
  20. 20. Dogma vs. Reality
  21. 21. A world of Trade Offs
  22. 22. What should we test?
  23. 23. How much should we test?
  24. 24. “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
  25. 25. The Hidden Secret Of TDD
  26. 26. The Secret of TDD
  27. 27. The Secret of TDD
  28. 28. Some books by Kent Beck
  29. 29. To write good tests first we need to learn how to program
  30. 30. We developers are like those users we like to complain so much about
  31. 31. NIH Syndrome
  32. 32. Design evolves and matures with time
  33. 33. Good Code sits in the small details
  34. 34. TIPS
  35. 35. Separate pure code from impure or stateful
  36. 36. Pure Functions
  37. 37. Pure Functions • Referential Transparency
  38. 38. Pure Functions • Referential Transparency • Don’t modify external state
  39. 39. Pure Functions • Referential Transparency • Don’t modify external state • Don’t produce side effects
  40. 40. 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
  41. 41. 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
  42. 42. Score calculation can be moved into its own function
  43. 43. Score calculation can be tested now
  44. 44. First write Pure Code
  45. 45. Add impure code step by step when needed
  46. 46. Write Composable Code
  47. 47. Function Composition http://en.wikipedia.org/wiki/Function_(mathematics)
  48. 48. Function Composition http://en.wikipedia.org/wiki/Function_(mathematics)
  49. 49. This looks familiar
  50. 50. “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
  51. 51. 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.
  52. 52. Why don’t we just code in this style?
  53. 53. This seems familiar again…
  54. 54. Welcome to Functional Programming
  55. 55. “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
  56. 56. What can we learn from Functional Programming?
  57. 57. The proper use of Types
  58. 58. What does ‘null’ mean?
  59. 59. What does ‘true|false’ mean?
  60. 60. Functions with just one responsibility
  61. 61. Radical separation of pure code from impure code
  62. 62. Let’s see an example
  63. 63. Food for Thought http://thinking-forth.sourceforge.net
  64. 64. Food for Thought http://www.amazon.com/Data-Reality-Perspective-Perceiving-Information/dp/1935504215/
  65. 65. “Inside every well- written large program is a well-written small program” http://www.linfo.org/q_programming.html
  66. 66. Questions?
  67. 67. Thanks! http://twitter.com/old_sound http://github.com/videlalvaro http://www.slideshare.net/old_sound

×