83
by @_md emergent design with

Emergent design with phpspec

Embed Size (px)

DESCRIPTION

Where does design fit in agile projects? What is refactoring and what isn't refactoring? What is the relationship between design and refactoring, and design and architecture? In this talk we will clarify these and other questions around emergent design and how a TDD/BDD tool like phpspec can help you with the process. We will look into how phpspec helps uncover smelly code and point in the direction of refactoring techniques to guide the design of flexible, testable and easy to extend object oriented PHP code – enabling developers to focus on what’s more valuable for the customer at all times and re-steer the direction of the product.

Citation preview

Page 1: Emergent design with phpspec

by @_md

emergent design with

Page 2: Emergent design with phpspec

I work here

I contribute here

I tweet here @_md

Marcello Duarte

Page 3: Emergent design with phpspec

it’s 2007

Page 4: Emergent design with phpspec

@padraicb

@tswicegood

Page 5: Emergent design with phpspec
Page 6: Emergent design with phpspec

a port for rspec

Page 8: Emergent design with phpspec

bowling.score.should eq(0)

Page 9: Emergent design with phpspec

in ruby everything is an object

and all objects are open

bowling.score.should eq(0)

Page 10: Emergent design with phpspec

in ruby everything is an object

and all objects are open

http://www.flickr.com/photos/owenbooth/126288240/

bowling.score.should eq(0)

Page 11: Emergent design with phpspec

in ruby everything is an object

and all objects are open

http://www.flickr.com/photos/owenbooth/126288240/

bowling.score.should eq(0)

Page 12: Emergent design with phpspec

$this->spec($bowling->getScore()) ->shouldEqual(0);

PHPSPEC EARLY SYNTAX EXAMPLE

Page 13: Emergent design with phpspec

http://www.flickr.com/photos/jamesrbowe/8403688026/

amber... red... green...

Page 14: Emergent design with phpspec

md@bossa:tests $ phpunit .PHPUnit 3.7.14 by Sebastian Bergmann.

PHP Fatal error: Class 'Customer' not found in /Users/mduarte/Documents/NoSync/Lab/phpunit/tests/CustomerTest.php on line 8

Fatal error: Class 'Customer' not found in /Users/mduarte/Documents/NoSync/Lab/phpunit/tests/CustomerTest.php on line 8

Page 15: Emergent design with phpspec

you gotta be mocking me!

Page 16: Emergent design with phpspec

public function testUpdateWithEqualTypes(){ $installer = $this->createInstallerMock(); $manager = new InstallationManager('vendor'); $manager->addInstaller($installer);

$initial = $this->createPackageMock(); $target = $this->createPackageMock(); $operation = new UpdateOperation($initial, $target, 'test');

$initial ->expects($this->once()) ->method('getType') ->will($this->returnValue('library')); $target ->expects($this->once()) ->method('getType') ->will($this->returnValue('library'));

$installer ->expects($this->once()) ->method('supports') ->with('library') ->will($this->returnValue(true));

$installer ->expects($this->once()) ->method('update') ->with($this->repository, $initial, $target);

Page 17: Emergent design with phpspec

@_md

@everzet

Page 18: Emergent design with phpspec

goals{fun to work with

Page 19: Emergent design with phpspec

goals{development toolfun to work with

Page 20: Emergent design with phpspec

goals{development toollet’s not get in the way

fun to work with

Page 21: Emergent design with phpspec

goals{enforce (mockist) TDD

development toollet’s not get in the way

fun to work with

Page 22: Emergent design with phpspec

goals{enforce (mockist) TDDdo it the PHP way

development toollet’s not get in the way

fun to work with

Page 23: Emergent design with phpspec
Page 24: Emergent design with phpspec

{ "require-dev": { "phpspec/phpspec": "2.0.*@dev" }, "config": { "bin-dir": "bin" }, "autoload": { "psr-0": { "": "src" } }}

Page 25: Emergent design with phpspec

$ bin/phpspec describe Some/Class$ bin/phpspec describe Some/Class

Page 26: Emergent design with phpspec

$ bin/phpspec describe Some/Class

$ bin/phpspec run

$ bin/phpspec describe Some/Class

$ bin/phpspec run

Page 27: Emergent design with phpspec

$ bin/phpspec describe Some/Class

$ bin/phpspec run

phpspec generators

$ bin/phpspec describe Some/Class

$ bin/phpspec run

phpspec generators

Page 28: Emergent design with phpspec

$ bin/phpspec describe Some/Class

$ bin/phpspec run

$ editor_of_choice . phpspec generators

$ bin/phpspec describe Some/Class

$ bin/phpspec run

$ editor_of_choice . phpspec generators

Page 29: Emergent design with phpspec

$ bin/phpspec describe Some/Class

$ bin/phpspec run

$ editor_of_choice . phpspec generators

$ bin/phpspec describe Some/Class

$ bin/phpspec run

$ editor_of_choice . phpspec generators

Page 30: Emergent design with phpspec

$ bin/phpspec describe Some/Class

$ bin/phpspec run

$ editor_of_choice . phpspec generators

$ bin/phpspec describe Some/Class

$ bin/phpspec run

$ editor_of_choice . phpspec generators

Page 31: Emergent design with phpspec

little demo

Page 32: Emergent design with phpspec

emergent design

Page 33: Emergent design with phpspec

design emerge iteratively driven by tests

Page 34: Emergent design with phpspec

designis...

Page 35: Emergent design with phpspec

design softwareis...

Page 36: Emergent design with phpspec

design softwareis

to describe how we’re solving a problem

Page 37: Emergent design with phpspec

calculate cost variance for a period

Page 38: Emergent design with phpspec

calculate cost variance for a period

Period

Page 39: Emergent design with phpspec

first learn design, then emergent design

Page 40: Emergent design with phpspec

“Key in making great and growable systems is to

design how its modules communicate

[and not] what their properties and behaviours should be.”

Messaging

View

poin

ts R

esea

rch

Inst

itute

Sou

rce

- Bon

nie

Mac

bird

UR

L -h

ttp://

ww

w.vp

ri.or

g

Page 41: Emergent design with phpspec

messaging

Page 42: Emergent design with phpspec

$this->person->getCar()->getEngine()->ignite();

Page 43: Emergent design with phpspec

focus on messagingmakes the code flexible

Page 44: Emergent design with phpspec

$this->person->startCar();

Page 45: Emergent design with phpspec

describing how objects interact to solve problemswhich roles, responsibilities and messages

design softwareis

Page 46: Emergent design with phpspec

big design up front

Page 47: Emergent design with phpspec

IT’S HARD TO CHANGE LATER.

Page 48: Emergent design with phpspec

WE NEED TO THINK ABOUT THINGS BEFORE DEVELOPING.

Page 49: Emergent design with phpspec

WE NEED TO MAKE SURE WE DON’T MISS ANYTHING.

Page 50: Emergent design with phpspec

THIS IS JUST THE WAY WE DO IT.

Page 51: Emergent design with phpspec

relative cost of repaircos

t

time

analysis design code test deploy rework

$

Page 52: Emergent design with phpspec

yagni

Page 53: Emergent design with phpspec

61%of the requested features are actually delivered

[Standish Group Report 06]

Page 54: Emergent design with phpspec

27%of requested features are actually used

Page 55: Emergent design with phpspec

5% to 10%are responsible for realising the benefits envisioned

Page 56: Emergent design with phpspec

design for the high priority itemsand make it easy to change later

Page 57: Emergent design with phpspec

describing how objects interact to solve problemswhich roles, responsibilities and messages

in a change-friendly way

design software in agileis

Page 58: Emergent design with phpspec

easier said than done?

Page 59: Emergent design with phpspec

test

coderefactor

Page 60: Emergent design with phpspec

use simple design rules to refactor

1. All tests run and pass2. Remove duplication3. Remove opacity4. Remove complexity

Page 61: Emergent design with phpspec

results in code

1. Testable2. Modular3. Expressive4. Simple

Page 62: Emergent design with phpspec

lack of tests breaks inner quality

1. Viscosity2. Immobility, Rigidity, Fragility3. Unreadable4. Complex

Page 63: Emergent design with phpspec

simple design is great to detect smells

Page 64: Emergent design with phpspec

simple design1. All tests run and pass2. Remove duplication3. Remove opacity4. Remove complexity

smells1. Any tests smells?2. Any DRY smells?3. Any opacity smells?4. Any complexity smells?

Page 65: Emergent design with phpspec

Test Smells

• Lack of tests

• Tests are not unitary

• Setup is too complex

• Unclear exercise

• More than one expectation in a test

• No expectation

• Too many paths

Page 66: Emergent design with phpspec

Dry Smells

• Simple duplication

• Logic duplication

• Duplication of constant

• Alternative classes with different interfaces

Page 67: Emergent design with phpspec

Opacity Smells

• Name not from domain

• Name not expressing intent

• Feature envy

• Method does more than one thing

• Method too long

• Primitive obsession

• Comments in the code

• Middle Man

Page 68: Emergent design with phpspec

Complexity Smells

• Unnecessary else

• Unnecessary if

• Unnecessary switch

• Too many arguments passed

• Inappropriate Intimacy

• Work in construction

• Static context

Page 69: Emergent design with phpspec

use design patterns to refactor

Page 70: Emergent design with phpspec

creational? behavioural? structural?

Page 71: Emergent design with phpspec

what can happen in a method?

Page 72: Emergent design with phpspec

return a value modify stateprint somethingthrow an exceptiondelegate{methods

Page 73: Emergent design with phpspec

return a value modify stateprint somethingthrow an exceptiondelegate{methods

not the final behaviour

Page 74: Emergent design with phpspec

return a value print somethingthrow an exceptiondelegate{methods

we should probably delegate that too

Page 75: Emergent design with phpspec

return a value throw an exceptiondelegate{methods

Page 76: Emergent design with phpspec

design delegation with mocks

Page 77: Emergent design with phpspec

start by defining behaviour

Page 78: Emergent design with phpspec

internally delegate to another method

Page 79: Emergent design with phpspec

Finally

• Define new role• Extract collaborators using mocks• Move behaviour definition to new collaborator test

Page 80: Emergent design with phpspec

phpspec

emergent design

simple design and smells

designing composition with mocks

Page 81: Emergent design with phpspec

Marcello Duarte

I work here

I contribute here

I tweet here @_md

Page 82: Emergent design with phpspec

Thank you !

Page 83: Emergent design with phpspec

Questions or Comments?

want to learn more? bitly.com/inviqa-bdd-training

@_md joind.in/8459