Unit Test + Functional Programming = Love

  • 7,245 views
Uploaded on

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

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

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
7,245
On Slideshare
0
From Embeds
0
Number of Embeds
3

Actions

Shares
Downloads
24
Comments
0
Likes
7

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Unit Testing + Functional Programming = <3 Alvaro Videla - RabbitMQ
  • 2. About Me • RabbitMQ Developer Advocate • Blog: http://videlalvaro.github.io/ • 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 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. Back to 1976
  • 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. 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. 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. Forward to 1982
  • 16. http://www.amazon.com/Software-Testing-Techniques-2nd-Edition/dp/1850328803/
  • 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. Forward to 1986
  • 19. http://www.amazon.com/Abstraction-Specification-Development-Electrical-Engineering/dp/0262121123/
  • 20. Dogma vs. Reality
  • 21. A world of Trade Offs
  • 22. What should we test?
  • 23. How much should we test?
  • 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. The Hidden Secret Of TDD
  • 26. The Secret of TDD
  • 27. The Secret of TDD
  • 28. Some books by Kent Beck
  • 29. To write good tests first we need to learn how to program
  • 30. We developers are like those users we like to complain so much about
  • 31. NIH Syndrome
  • 32. Design evolves and matures with time
  • 33. Good Code sits in the small details
  • 34. TIPS
  • 35. Separate pure code from impure or stateful
  • 36. Pure Functions
  • 37. Pure Functions • Referential Transparency
  • 38. Pure Functions • Referential Transparency • Don’t modify external state
  • 39. Pure Functions • Referential Transparency • Don’t modify external state • Don’t produce side effects
  • 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. 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. Score calculation can be moved into its own function
  • 43. Score calculation can be tested now
  • 44. First write Pure Code
  • 45. Add impure code step by step when needed
  • 46. Write Composable Code
  • 47. Function Composition http://en.wikipedia.org/wiki/Function_(mathematics)
  • 48. Function Composition http://en.wikipedia.org/wiki/Function_(mathematics)
  • 49. This looks familiar
  • 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. 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. Why don’t we just code in this style?
  • 53. This seems familiar again…
  • 54. Welcome to Functional Programming
  • 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. What can we learn from Functional Programming?
  • 57. The proper use of Types
  • 58. What does ‘null’ mean?
  • 59. What does ‘true|false’ mean?
  • 60. Functions with just one responsibility
  • 61. Radical separation of pure code from impure code
  • 62. Let’s see an example
  • 63. Food for Thought http://thinking-forth.sourceforge.net
  • 64. Food for Thought http://www.amazon.com/Data-Reality-Perspective-Perceiving-Information/dp/1935504215/
  • 65. “Inside every well- written large program is a well-written small program” http://www.linfo.org/q_programming.html
  • 66. Questions?
  • 67. Thanks! http://twitter.com/old_sound http://github.com/videlalvaro http://www.slideshare.net/old_sound