68
Unit Testing + Functional Programming = <3 Alvaro Videla - RabbitMQ

Unit Test + Functional Programming = Love

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Unit Test + Functional Programming = Love

Unit Testing +

Functional Programming = <3

Alvaro Videla - RabbitMQ

Page 2: Unit Test + Functional Programming = Love

About Me

• RabbitMQ Developer Advocate

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

• Twitter: @old_sound

Page 3: Unit Test + Functional Programming = Love

About Me

Co-author !

RabbitMQ in Action

http://bit.ly/rabbitmq

Page 4: Unit Test + Functional Programming = Love

I’m not a:

Page 5: Unit Test + Functional Programming = Love

I’m not a:

• Application Testing Guru

Page 6: Unit Test + Functional Programming = Love

I’m not a:

• Application Testing Guru

• TDD Advocate

Page 7: Unit Test + Functional Programming = Love

Why is it so hard to write tests?

Page 8: Unit Test + Functional Programming = Love

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

Page 9: Unit Test + Functional Programming = Love

Unit Testing

[…] unit testing by definition only tests the functionality of the units themselves.

http://en.wikipedia.org/wiki/Unit_testing

Page 10: Unit Test + Functional Programming = Love

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

Page 11: Unit Test + Functional Programming = Love

Back to 1976

Page 12: Unit Test + Functional Programming = Love

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

Page 13: Unit Test + Functional Programming = Love

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

Page 14: Unit Test + Functional Programming = Love

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

Page 15: Unit Test + Functional Programming = Love

Forward to 1982

Page 16: Unit Test + Functional Programming = Love

http://www.amazon.com/Software-Testing-Techniques-2nd-Edition/dp/1850328803/

Page 17: Unit Test + Functional Programming = Love

Unit, Unit Testing

A unit is the smallest testable piece of software

http://www.amazon.com/Software-Testing-Techniques-2nd-Edition/dp/1850328803/

Page 18: Unit Test + Functional Programming = Love

Forward to 1986

Page 19: Unit Test + Functional Programming = Love

http://www.amazon.com/Abstraction-Specification-Development-Electrical-Engineering/dp/0262121123/

Page 20: Unit Test + Functional Programming = Love

Dogma vs.

Reality

Page 21: Unit Test + Functional Programming = Love

A world of Trade Offs

Page 22: Unit Test + Functional Programming = Love

What should we test?

Page 23: Unit Test + Functional Programming = Love

How much should

we test?

Page 24: Unit Test + Functional Programming = Love

“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

Page 25: Unit Test + Functional Programming = Love

The Hidden Secret

Of TDD

Page 26: Unit Test + Functional Programming = Love

The Secret of TDD

Page 27: Unit Test + Functional Programming = Love

The Secret of TDD

Page 28: Unit Test + Functional Programming = Love

Some books by Kent Beck

Page 29: Unit Test + Functional Programming = Love

To write good tests first we need to learn

how to program

Page 30: Unit Test + Functional Programming = Love
Page 31: Unit Test + Functional Programming = Love

We developers are like those users we like to complain so

much about

Page 32: Unit Test + Functional Programming = Love

NIH Syndrome

Page 33: Unit Test + Functional Programming = Love

Design evolves and matures with time

Page 34: Unit Test + Functional Programming = Love

Good Code sits in the small details

Page 35: Unit Test + Functional Programming = Love

TIPS

Page 36: Unit Test + Functional Programming = Love

Separate pure code from impure or stateful

Page 37: Unit Test + Functional Programming = Love

Pure Functions

Page 38: Unit Test + Functional Programming = Love

Pure Functions

• Referential Transparency

Page 39: Unit Test + Functional Programming = Love

Pure Functions

• Referential Transparency

• Don’t modify external state

Page 40: Unit Test + Functional Programming = Love

Pure Functions

• Referential Transparency

• Don’t modify external state

• Don’t produce side effects

Page 41: Unit Test + Functional Programming = Love

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

Page 42: Unit Test + Functional Programming = Love

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

Page 43: Unit Test + Functional Programming = Love

Score calculation can be moved into its

own function

Page 44: Unit Test + Functional Programming = Love

Score calculation can be tested now

Page 45: Unit Test + Functional Programming = Love

First write Pure Code

Page 46: Unit Test + Functional Programming = Love

Add impure code step by step when needed

Page 47: Unit Test + Functional Programming = Love

Write Composable

Code

Page 48: Unit Test + Functional Programming = Love

Function Composition

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

Page 49: Unit Test + Functional Programming = Love

Function Composition

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

Page 50: Unit Test + Functional Programming = Love

This looks familiar

Page 51: Unit Test + Functional Programming = Love

“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

Page 52: Unit Test + Functional Programming = Love

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.

Page 53: Unit Test + Functional Programming = Love

Why don’t we just code in this style?

Page 54: Unit Test + Functional Programming = Love

This seems familiar again…

Page 55: Unit Test + Functional Programming = Love

Welcome to Functional

Programming

Page 56: Unit Test + Functional Programming = Love

“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

Page 57: Unit Test + Functional Programming = Love

What can we learn from Functional Programming?

Page 58: Unit Test + Functional Programming = Love

The proper use of Types

Page 59: Unit Test + Functional Programming = Love

What does ‘null’ mean?

Page 60: Unit Test + Functional Programming = Love

What does ‘true|false’ mean?

Page 61: Unit Test + Functional Programming = Love

Functions with just one responsibility

Page 62: Unit Test + Functional Programming = Love

Radical separation of pure code from impure code

Page 63: Unit Test + Functional Programming = Love

Let’s see an example

Page 64: Unit Test + Functional Programming = Love

Food for Thought

http://thinking-forth.sourceforge.net

Page 65: Unit Test + Functional Programming = Love

Food for Thought

http://www.amazon.com/Data-Reality-Perspective-Perceiving-Information/dp/1935504215/

Page 66: Unit Test + Functional Programming = Love

“Inside every well- written large program

is a well-written small program”

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

Page 67: Unit Test + Functional Programming = Love

Questions?

Page 68: Unit Test + Functional Programming = Love

Thanks!http://twitter.com/old_sound http://github.com/videlalvaro

http://www.slideshare.net/old_sound