SlideShare a Scribd company logo
Writing
                          Testable Code

                           Alvaro Videla - Cloud Foundry




Wednesday, April 10, 13
About Me


                     •    Cloud Foundry Developer Advocate

                     •    Blog: http://videlalvaro.github.com/

                     •    Twitter: @old_sound




Wednesday, April 10, 13
About Me
                          Co-author

               RabbitMQ in Action
               http://bit.ly/rabbitmq




Wednesday, April 10, 13
I’m not a:




Wednesday, April 10, 13
I’m not a:
                     • Application Testing Guru




Wednesday, April 10, 13
I’m not a:
                     • Application Testing Guru
                     • TDD Advocate




Wednesday, 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
                    the program and show
                   that the individual parts
                          are correct
                           http://en.wikipedia.org/wiki/Unit_testing


Wednesday, April 10, 13
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
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
Dogma
                            vs.
                          Reality

Wednesday, April 10, 13
A world of
                          Trade Offs
Wednesday, 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 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
The Hidden
                            Secret
                           Of TDD



Wednesday, April 10, 13
The Secret of TDD




Wednesday, April 10, 13
The Secret of TDD




Wednesday, April 10, 13
Some books by Kent Beck




Wednesday, April 10, 13
To write good
                   tests first we need
                    to learn how to
                        program

Wednesday, April 10, 13
Wednesday, April 10, 13
We developers are
                 like those users we
                 like to complain so
                     much about

Wednesday, April 10, 13
Design evolves and
                 matures with time


Wednesday, April 10, 13
Good Code sits in
                      the small details


Wednesday, April 10, 13
TIPS



Wednesday, April 10, 13
Separate pure code
                     from impure or stateful


Wednesday, April 10, 13
Pure Functions




Wednesday, April 10, 13
Pure Functions
                     • Referential Transparency




Wednesday, April 10, 13
Pure Functions
                     • Referential Transparency
                     • Don’t modify external state




Wednesday, April 10, 13
Pure Functions
                     • Referential Transparency
                     • Don’t modify external state
                     • Don’t produce side effects




Wednesday, April 10, 13
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
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
Score calculation
                   can be moved into
                    its own function


Wednesday, April 10, 13
Score calculation
                    can be tested now


Wednesday, April 10, 13
First write
                          Pure Code


Wednesday, April 10, 13
Add impure code
                    step by step when
                         needed


Wednesday, April 10, 13
Write
                          Composable
                            Code


Wednesday, 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 familiar



Wednesday, April 10, 13
“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
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
Why don’t we just
                          code in this style?


Wednesday, April 10, 13
This seems familiar
                                again…


Wednesday, April 10, 13
Welcome to
                           Functional
                          Programming


Wednesday, April 10, 13
“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
What can we learn from
                          Functional Programming?




Wednesday, April 10, 13
The proper use of Types




Wednesday, April 10, 13
What does ‘null’ mean?




Wednesday, April 10, 13
What does
                          ‘true|false’ mean?



Wednesday, April 10, 13
Functions with just one
                              responsibility



Wednesday, April 10, 13
Radical separation of pure
                      code from impure code



Wednesday, April 10, 13
Let’s see an example




Wednesday, April 10, 13
Food for Thought




                          http://thinking-forth.sourceforge.net
Wednesday, April 10, 13
“Inside every well-
                          written large program
                          is a well-written small
                                 program”

                                 http://www.linfo.org/q_programming.html




Wednesday, April 10, 13
Questions?


Wednesday, April 10, 13
Thanks!
                              http://twitter.com/old_sound
                              http://github.com/videlalvaro
                          http://www.slideshare.net/old_sound


Wednesday, April 10, 13

More Related Content

PPTX
Η οργάνωση της κοινωνίας - Η ένταξη του ατόμου (Κοινωνικοποίηση)
PPTX
μερη αρχαιου θεατρου τα εργαλεια-το σκηνικο
PPT
δενδροκομια
ODP
το ψεμα
PPSX
Η Ιλιάδα στην Τέχνη
PDF
δραστηριότητες στην τάξη
PPT
κλιση επιθετων και μετοχων
DOCX
συνηρημενα ρηματα θεωρία
Η οργάνωση της κοινωνίας - Η ένταξη του ατόμου (Κοινωνικοποίηση)
μερη αρχαιου θεατρου τα εργαλεια-το σκηνικο
δενδροκομια
το ψεμα
Η Ιλιάδα στην Τέχνη
δραστηριότητες στην τάξη
κλιση επιθετων και μετοχων
συνηρημενα ρηματα θεωρία

What's hot (20)

PPTX
Τάσεις-Καταπονήσεις
PPT
ατμοηλεκτρικα εργοστασια
PPT
ΧΑΡΑΚΤΗΡΙΣΤΙΚΑ ΖΩΝΤΑΝΏΝ ΟΡΓΑΝΙΣΜΩΝ
PDF
Οδύσσεια, ραψωδία α στίχοι 361 497
PPTX
βια γυναικων
DOCX
Ελένη ,Ευριπίδη,Β΄Επεισόδιο,4η σκηνή,Φύλλο δράσεων ,Νέα προγράμματα σπουδών,Ε...
PPT
ιλιαδα επιτραπέζιο παιχνίδι
PDF
Η Φιλική Εταιρεία
PPT
ΚΠΑ Γ' ΓΥΜΝΑΣΙΟΥ 8.4.1 ΒΑΣΙΚΕΣ ΑΡΧΕΣ ΣΥΝΤΑΓΜΑΤΟΣ-ΑΡΧΗ ΛΑΪΚΗΣ ΚΥΡΙΑΡΧΙΑΣ (ΕΚΜΑ...
DOC
αντιπολεμικά μηνύματα
PPSX
Συνέντευξη από την γιαγιά μου
PPT
Διδασκαλία Αγγλικής Γλώσσας στα ΕΠΑ.Λ 2016-17
PDF
"ολυμπιακος υμνος" κωστης παλαμας
PPTX
Αρχαίοι Έλληνες Ιστοριογράφοι
DOCX
ΑΝΘΡΩΠΙΣΜΟΣ -ΑΝΘΡΩΠΙΑ
PDF
Eπαναληπτικές Ασκήσεις Ιστορίας Γ΄ 1η Ενότητά: Η δημιουργία του κόσμου
DOC
lyo-energitiki-phoni.doc
PPTX
Ομήρου Οδύσσεια, ενότητες 19, 20, 21, 22
PDF
ΟΜΗΡΟΥ ΟΔΥΣΣΕΙΑ α 26-497 (comics)
Τάσεις-Καταπονήσεις
ατμοηλεκτρικα εργοστασια
ΧΑΡΑΚΤΗΡΙΣΤΙΚΑ ΖΩΝΤΑΝΏΝ ΟΡΓΑΝΙΣΜΩΝ
Οδύσσεια, ραψωδία α στίχοι 361 497
βια γυναικων
Ελένη ,Ευριπίδη,Β΄Επεισόδιο,4η σκηνή,Φύλλο δράσεων ,Νέα προγράμματα σπουδών,Ε...
ιλιαδα επιτραπέζιο παιχνίδι
Η Φιλική Εταιρεία
ΚΠΑ Γ' ΓΥΜΝΑΣΙΟΥ 8.4.1 ΒΑΣΙΚΕΣ ΑΡΧΕΣ ΣΥΝΤΑΓΜΑΤΟΣ-ΑΡΧΗ ΛΑΪΚΗΣ ΚΥΡΙΑΡΧΙΑΣ (ΕΚΜΑ...
αντιπολεμικά μηνύματα
Συνέντευξη από την γιαγιά μου
Διδασκαλία Αγγλικής Γλώσσας στα ΕΠΑ.Λ 2016-17
"ολυμπιακος υμνος" κωστης παλαμας
Αρχαίοι Έλληνες Ιστοριογράφοι
ΑΝΘΡΩΠΙΣΜΟΣ -ΑΝΘΡΩΠΙΑ
Eπαναληπτικές Ασκήσεις Ιστορίας Γ΄ 1η Ενότητά: Η δημιουργία του κόσμου
lyo-energitiki-phoni.doc
Ομήρου Οδύσσεια, ενότητες 19, 20, 21, 22
ΟΜΗΡΟΥ ΟΔΥΣΣΕΙΑ α 26-497 (comics)
Ad

Similar to Writing testable code (20)

PDF
When Tdd Goes Awry
PDF
Writing the docs
PDF
Intravert atx meetup_condensed
PDF
ChefConf2014 - Chef TDD
PDF
Keeping your users happy with testable apps - Greg Shackles
PDF
The State of Puppet
PDF
Genestack BioIT-World-2013
PDF
Testable Code ... In Joomla!?
PDF
Test Driven Infrastructure Development
PDF
Test driven infrastructure development
PDF
NodeJS: Writing tests -- A Beginners' Guide
PDF
Eclipse con 2012 - Devops - Luke Kanies
PDF
Engineering culture
PDF
Piccolo coding dojo (milano xpug 2013-04-11)
PDF
Unmoderated User Testing
PDF
Connect and Collaborate C
PDF
Eclipse con 2012 - Frictionless operations with Puppet - Luke Kanies
PDF
Debugging Android - GDG Munich
PDF
Specking Interactors with PHPSpec and YOLO (DDD) at PHPConference Argentina 2013
PDF
Puppet Camp Berlin 2014: Advanced Puppet Design
When Tdd Goes Awry
Writing the docs
Intravert atx meetup_condensed
ChefConf2014 - Chef TDD
Keeping your users happy with testable apps - Greg Shackles
The State of Puppet
Genestack BioIT-World-2013
Testable Code ... In Joomla!?
Test Driven Infrastructure Development
Test driven infrastructure development
NodeJS: Writing tests -- A Beginners' Guide
Eclipse con 2012 - Devops - Luke Kanies
Engineering culture
Piccolo coding dojo (milano xpug 2013-04-11)
Unmoderated User Testing
Connect and Collaborate C
Eclipse con 2012 - Frictionless operations with Puppet - Luke Kanies
Debugging Android - GDG Munich
Specking Interactors with PHPSpec and YOLO (DDD) at PHPConference Argentina 2013
Puppet Camp Berlin 2014: Advanced Puppet Design
Ad

More from Alvaro Videla (20)

PDF
Improvements in RabbitMQ
PDF
Data Migration at Scale with RabbitMQ and Spring Integration
PDF
RabbitMQ Data Ingestion at Craft Conf
PDF
Scaling applications with RabbitMQ at SunshinePHP
PDF
Unit Test + Functional Programming = Love
PDF
RabbitMQ Data Ingestion
PDF
Dissecting the rabbit: RabbitMQ Internal Architecture
PDF
Introduction to RabbitMQ | Meetup at Pivotal Labs
PDF
RabbitMQ Hands On
PDF
Rabbitmq Boot System
PDF
Cloud Foundry Bootcamp
PDF
Cloud Messaging With Cloud Foundry
PDF
Taming the rabbit
PDF
PDF
Código Fácil De Testear
PDF
Desacoplando aplicaciones
PDF
Messaging patterns
PDF
Theres a rabbit on my symfony
PDF
Scaling Web Apps With RabbitMQ - Erlang Factory Lite
PDF
Integrating php withrabbitmq_zendcon
Improvements in RabbitMQ
Data Migration at Scale with RabbitMQ and Spring Integration
RabbitMQ Data Ingestion at Craft Conf
Scaling applications with RabbitMQ at SunshinePHP
Unit Test + Functional Programming = Love
RabbitMQ Data Ingestion
Dissecting the rabbit: RabbitMQ Internal Architecture
Introduction to RabbitMQ | Meetup at Pivotal Labs
RabbitMQ Hands On
Rabbitmq Boot System
Cloud Foundry Bootcamp
Cloud Messaging With Cloud Foundry
Taming the rabbit
Código Fácil De Testear
Desacoplando aplicaciones
Messaging patterns
Theres a rabbit on my symfony
Scaling Web Apps With RabbitMQ - Erlang Factory Lite
Integrating php withrabbitmq_zendcon

Recently uploaded (20)

PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Spectroscopy.pptx food analysis technology
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Approach and Philosophy of On baking technology
PPTX
Cloud computing and distributed systems.
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Advanced methodologies resolving dimensionality complications for autism neur...
Spectral efficient network and resource selection model in 5G networks
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Spectroscopy.pptx food analysis technology
Unlocking AI with Model Context Protocol (MCP)
MIND Revenue Release Quarter 2 2025 Press Release
sap open course for s4hana steps from ECC to s4
Encapsulation_ Review paper, used for researhc scholars
Per capita expenditure prediction using model stacking based on satellite ima...
Understanding_Digital_Forensics_Presentation.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Approach and Philosophy of On baking technology
Cloud computing and distributed systems.
Reach Out and Touch Someone: Haptics and Empathic Computing
Programs and apps: productivity, graphics, security and other tools
Diabetes mellitus diagnosis method based random forest with bat algorithm
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton

Writing testable code

  • 1. Writing Testable Code Alvaro Videla - Cloud Foundry Wednesday, April 10, 13
  • 2. About Me • Cloud Foundry Developer Advocate • Blog: http://videlalvaro.github.com/ • Twitter: @old_sound Wednesday, April 10, 13
  • 3. About Me Co-author RabbitMQ in Action http://bit.ly/rabbitmq Wednesday, April 10, 13
  • 4. I’m not a: Wednesday, April 10, 13
  • 5. I’m not a: • Application Testing Guru Wednesday, April 10, 13
  • 6. I’m not a: • Application Testing Guru • TDD Advocate Wednesday, April 10, 13
  • 7. Why is it so hard to write tests? Wednesday, April 10, 13
  • 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. 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. 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. Dogma vs. Reality Wednesday, April 10, 13
  • 12. A world of Trade Offs Wednesday, April 10, 13
  • 13. What should we test? Wednesday, April 10, 13
  • 14. How much should we test? Wednesday, April 10, 13
  • 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. The Hidden Secret Of TDD Wednesday, April 10, 13
  • 17. The Secret of TDD Wednesday, April 10, 13
  • 18. The Secret of TDD Wednesday, April 10, 13
  • 19. Some books by Kent Beck Wednesday, April 10, 13
  • 20. To write good tests first we need to learn how to program Wednesday, April 10, 13
  • 22. We developers are like those users we like to complain so much about Wednesday, April 10, 13
  • 23. Design evolves and matures with time Wednesday, April 10, 13
  • 24. Good Code sits in the small details Wednesday, April 10, 13
  • 26. Separate pure code from impure or stateful Wednesday, April 10, 13
  • 28. Pure Functions • Referential Transparency Wednesday, April 10, 13
  • 29. Pure Functions • Referential Transparency • Don’t modify external state Wednesday, April 10, 13
  • 30. Pure Functions • Referential Transparency • Don’t modify external state • Don’t produce side effects Wednesday, April 10, 13
  • 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. 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. Score calculation can be moved into its own function Wednesday, April 10, 13
  • 34. Score calculation can be tested now Wednesday, April 10, 13
  • 35. First write Pure Code Wednesday, April 10, 13
  • 36. Add impure code step by step when needed Wednesday, April 10, 13
  • 37. Write Composable Code Wednesday, April 10, 13
  • 38. Function Composition http://en.wikipedia.org/wiki/Function_(mathematics) Wednesday, April 10, 13
  • 39. Function Composition http://en.wikipedia.org/wiki/Function_(mathematics) Wednesday, April 10, 13
  • 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. 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. Why don’t we just code in this style? Wednesday, April 10, 13
  • 44. This seems familiar again… Wednesday, April 10, 13
  • 45. Welcome to Functional Programming Wednesday, April 10, 13
  • 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. What can we learn from Functional Programming? Wednesday, April 10, 13
  • 48. The proper use of Types Wednesday, April 10, 13
  • 49. What does ‘null’ mean? Wednesday, April 10, 13
  • 50. What does ‘true|false’ mean? Wednesday, April 10, 13
  • 51. Functions with just one responsibility Wednesday, April 10, 13
  • 52. Radical separation of pure code from impure code Wednesday, April 10, 13
  • 53. Let’s see an example Wednesday, April 10, 13
  • 54. Food for Thought http://thinking-forth.sourceforge.net Wednesday, April 10, 13
  • 55. “Inside every well- written large program is a well-written small program” http://www.linfo.org/q_programming.html Wednesday, April 10, 13
  • 57. Thanks! http://twitter.com/old_sound http://github.com/videlalvaro http://www.slideshare.net/old_sound Wednesday, April 10, 13