emergent design with phpspec
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.TRANSCRIPT
![Page 1: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/1.jpg)
by @_md
emergent design with
![Page 2: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/2.jpg)
I work here
I contribute here
I tweet here @_md
Marcello Duarte
![Page 3: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/3.jpg)
it’s 2007
![Page 4: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/4.jpg)
@padraicb
@tswicegood
![Page 5: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/5.jpg)
![Page 6: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/6.jpg)
a port for rspec
![Page 7: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/7.jpg)
! = =
http://earthymoon.deviantart.com/art/Elephant-png-122633500 http://sonylisation.deviantart.com/art/Ruby-Nr-2-207831683
![Page 8: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/8.jpg)
bowling.score.should eq(0)
![Page 9: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/9.jpg)
in ruby everything is an object
and all objects are open
bowling.score.should eq(0)
![Page 10: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/10.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/11.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/12.jpg)
$this->spec($bowling->getScore()) ->shouldEqual(0);
PHPSPEC EARLY SYNTAX EXAMPLE
![Page 13: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/13.jpg)
http://www.flickr.com/photos/jamesrbowe/8403688026/
amber... red... green...
![Page 14: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/14.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/15.jpg)
you gotta be mocking me!
![Page 16: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/16.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/17.jpg)
@_md
@everzet
![Page 18: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/18.jpg)
goals{fun to work with
![Page 19: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/19.jpg)
goals{development toolfun to work with
![Page 20: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/20.jpg)
goals{development toollet’s not get in the way
fun to work with
![Page 21: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/21.jpg)
goals{enforce (mockist) TDD
development toollet’s not get in the way
fun to work with
![Page 22: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/22.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/23.jpg)
![Page 24: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/24.jpg)
{ "require-dev": { "phpspec/phpspec": "2.0.*@dev" }, "config": { "bin-dir": "bin" }, "autoload": { "psr-0": { "": "src" } }}
![Page 25: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/25.jpg)
$ bin/phpspec describe Some/Class$ bin/phpspec describe Some/Class
![Page 26: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/26.jpg)
$ bin/phpspec describe Some/Class
$ bin/phpspec run
$ bin/phpspec describe Some/Class
$ bin/phpspec run
![Page 27: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/27.jpg)
$ 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](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/28.jpg)
$ 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](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/29.jpg)
$ 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](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/30.jpg)
$ 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](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/31.jpg)
little demo
![Page 32: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/32.jpg)
emergent design
![Page 33: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/33.jpg)
design emerge iteratively driven by tests
![Page 34: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/34.jpg)
designis...
![Page 35: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/35.jpg)
design softwareis...
![Page 36: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/36.jpg)
design softwareis
to describe how we’re solving a problem
![Page 37: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/37.jpg)
calculate cost variance for a period
![Page 38: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/38.jpg)
calculate cost variance for a period
Period
![Page 39: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/39.jpg)
first learn design, then emergent design
![Page 40: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/40.jpg)
“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](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/41.jpg)
messaging
![Page 42: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/42.jpg)
$this->person->getCar()->getEngine()->ignite();
![Page 43: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/43.jpg)
focus on messagingmakes the code flexible
![Page 44: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/44.jpg)
$this->person->startCar();
![Page 45: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/45.jpg)
describing how objects interact to solve problemswhich roles, responsibilities and messages
design softwareis
![Page 46: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/46.jpg)
big design up front
![Page 47: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/47.jpg)
IT’S HARD TO CHANGE LATER.
![Page 48: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/48.jpg)
WE NEED TO THINK ABOUT THINGS BEFORE DEVELOPING.
![Page 49: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/49.jpg)
WE NEED TO MAKE SURE WE DON’T MISS ANYTHING.
![Page 50: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/50.jpg)
THIS IS JUST THE WAY WE DO IT.
![Page 51: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/51.jpg)
relative cost of repaircos
t
time
analysis design code test deploy rework
$
![Page 52: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/52.jpg)
yagni
![Page 53: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/53.jpg)
61%of the requested features are actually delivered
[Standish Group Report 06]
![Page 54: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/54.jpg)
27%of requested features are actually used
![Page 55: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/55.jpg)
5% to 10%are responsible for realising the benefits envisioned
![Page 56: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/56.jpg)
design for the high priority itemsand make it easy to change later
![Page 57: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/57.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/58.jpg)
easier said than done?
![Page 59: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/59.jpg)
test
coderefactor
![Page 60: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/60.jpg)
use simple design rules to refactor
1. All tests run and pass2. Remove duplication3. Remove opacity4. Remove complexity
![Page 61: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/61.jpg)
results in code
1. Testable2. Modular3. Expressive4. Simple
![Page 62: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/62.jpg)
lack of tests breaks inner quality
1. Viscosity2. Immobility, Rigidity, Fragility3. Unreadable4. Complex
![Page 63: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/63.jpg)
simple design is great to detect smells
![Page 64: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/64.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/65.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/66.jpg)
Dry Smells
• Simple duplication
• Logic duplication
• Duplication of constant
• Alternative classes with different interfaces
![Page 67: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/67.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/68.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/69.jpg)
use design patterns to refactor
![Page 70: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/70.jpg)
creational? behavioural? structural?
![Page 71: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/71.jpg)
what can happen in a method?
![Page 72: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/72.jpg)
return a value modify stateprint somethingthrow an exceptiondelegate{methods
![Page 73: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/73.jpg)
return a value modify stateprint somethingthrow an exceptiondelegate{methods
not the final behaviour
![Page 74: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/74.jpg)
return a value print somethingthrow an exceptiondelegate{methods
we should probably delegate that too
![Page 75: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/75.jpg)
return a value throw an exceptiondelegate{methods
![Page 76: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/76.jpg)
design delegation with mocks
![Page 77: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/77.jpg)
start by defining behaviour
![Page 78: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/78.jpg)
internally delegate to another method
![Page 79: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/79.jpg)
Finally
• Define new role• Extract collaborators using mocks• Move behaviour definition to new collaborator test
![Page 80: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/80.jpg)
phpspec
emergent design
simple design and smells
designing composition with mocks
![Page 81: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/81.jpg)
Marcello Duarte
I work here
I contribute here
I tweet here @_md
![Page 82: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/82.jpg)
Thank you !
![Page 83: Emergent design with phpspec](https://reader034.vdocument.in/reader034/viewer/2022042614/554f6581b4c9058a148b4aa9/html5/thumbnails/83.jpg)
Questions or Comments?
want to learn more? bitly.com/inviqa-bdd-training
@_md joind.in/8459