Date post: | 12-May-2015 |
Category: |
Technology |
Upload: | alvaro-videla |
View: | 4,610 times |
Download: | 0 times |
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
Dogmavs.
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 justcode 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 reinventingfunctional 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.netWednesday, April 10, 13
“Inside every well- written large programis 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_soundhttp://github.com/videlalvaro
http://www.slideshare.net/old_sound
Wednesday, April 10, 13