Writing Testable Code Alvaro Videla - Cloud Foundry About Me - - PowerPoint PPT Presentation

writing testable code
SMART_READER_LITE
LIVE PREVIEW

Writing Testable Code Alvaro Videla - Cloud Foundry About Me - - PowerPoint PPT Presentation

Writing Testable Code Alvaro Videla - Cloud Foundry About Me Cloud Foundry Developer Advocate Blog: http://videlalvaro.github.com/ Twitter: @old_sound About Me Co-author RabbitMQ in Action http://bit.ly/rabbitmq Im not a:


slide-1
SLIDE 1

Writing Testable Code

Alvaro Videla - Cloud Foundry

slide-2
SLIDE 2

About Me

  • Cloud Foundry Developer Advocate
  • Blog: http://videlalvaro.github.com/
  • Twitter: @old_sound
slide-3
SLIDE 3

About Me

Co-author RabbitMQ in Action http://bit.ly/rabbitmq

slide-4
SLIDE 4

I’m not a:

slide-5
SLIDE 5

I’m not a:

  • Application Testing Guru
slide-6
SLIDE 6

I’m not a:

  • Application Testing Guru
  • TDD Advocate
slide-7
SLIDE 7

Why is it so hard to write tests?

slide-8
SLIDE 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

slide-9
SLIDE 9

Unit Testing

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

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

slide-10
SLIDE 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

slide-11
SLIDE 11

Dogma vs. Reality

slide-12
SLIDE 12

A world of Trade Offs

slide-13
SLIDE 13

What should we test?

slide-14
SLIDE 14

How much should we test?

slide-15
SLIDE 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
slide-16
SLIDE 16

The Hidden Secret Of TDD

slide-17
SLIDE 17

The Secret of TDD

slide-18
SLIDE 18

The Secret of TDD

slide-19
SLIDE 19

Some books by Kent Beck

slide-20
SLIDE 20

To write good tests first we need to learn how to program

slide-21
SLIDE 21
slide-22
SLIDE 22

We developers are like those users we like to complain so much about

slide-23
SLIDE 23

Design evolves and matures with time

slide-24
SLIDE 24

Good Code sits in the small details

slide-25
SLIDE 25

TIPS

slide-26
SLIDE 26

Separate pure code from impure or stateful

slide-27
SLIDE 27

Pure Functions

slide-28
SLIDE 28

Pure Functions

  • Referential Transparency
slide-29
SLIDE 29

Pure Functions

  • Referential Transparency
  • Don’t modify external state
slide-30
SLIDE 30

Pure Functions

  • Referential Transparency
  • Don’t modify external state
  • Don’t produce side effects
slide-31
SLIDE 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
slide-32
SLIDE 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
slide-33
SLIDE 33

Score calculation can be moved into its own function

slide-34
SLIDE 34

Score calculation can be tested now

slide-35
SLIDE 35

First write Pure Code

slide-36
SLIDE 36

Add impure code step by step when needed

slide-37
SLIDE 37

Write Composable Code

slide-38
SLIDE 38

Function Composition

http://en.wikipedia.org/wiki/Function_(mathematics)
slide-39
SLIDE 39

Function Composition

http://en.wikipedia.org/wiki/Function_(mathematics)
slide-40
SLIDE 40

This looks familiar

slide-41
SLIDE 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
slide-42
SLIDE 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.
slide-43
SLIDE 43

Why don’t we just code in this style?

slide-44
SLIDE 44

This seems familiar again…

slide-45
SLIDE 45

Welcome to Functional Programming

slide-46
SLIDE 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
slide-47
SLIDE 47

What can we learn from Functional Programming?

slide-48
SLIDE 48

The proper use of Types

slide-49
SLIDE 49

What does ‘null’ mean?

slide-50
SLIDE 50

What does ‘true|false’ mean?

slide-51
SLIDE 51

Functions with just one responsibility

slide-52
SLIDE 52

Radical separation of pure code from impure code

slide-53
SLIDE 53

Let’s see an example

slide-54
SLIDE 54

Food for Thought

http://thinking-forth.sourceforge.net

slide-55
SLIDE 55

“Inside every well- written large program is a well-written small program”

http://www.linfo.org/q_programming.html
slide-56
SLIDE 56

Questions?

slide-57
SLIDE 57

Thanks!

http://twitter.com/old_sound http://github.com/videlalvaro http://www.slideshare.net/old_sound