SlideShare une entreprise Scribd logo
1  sur  62
Things to consider for testable code Frank Kleine, 27.10.2009
The Speaker: Frank Kleine ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Separation of Concerns ,[object Object]
Business logic
Logging
Error handling
Environment handling
Didn't we forget something really important?
Construction of Objects!
Construction of objects ,[object Object]
Construction of objects ,[object Object],[object Object]
Construction of objects ,[object Object],[object Object],[object Object],[object Object]
Construction of objects ,[object Object],[object Object],[object Object],[object Object],[object Object]
Construction of objects II ,[object Object],class Car { public function __construct() { $this->engine = new Engine(); $this->tire  = TireFactory::createTire(); } }
Construction of objects II ,[object Object],class Car { public function __construct() { $this->engine = new Engine(); $this->tire  = TireFactory::createTire(); } } Work in constructor (Anti-Pattern)
Construction of objects II ,[object Object],class Car { public function __construct() { $this->engine = new Engine(); $this->tire  = TireFactory::createTire(); } } Work in constructor (Anti-Pattern) Hard to test
Construction of objects II ,[object Object],[object Object],class Car { public function __construct() { $this->engine = new Engine(); $this->tire  = TireFactory::createTire(); } } class Car { public function __construct(Engine $eng, Tire $tire) { $this->engine = $eng; $this->tire  = $tire; } } Work in constructor (Anti-Pattern) Hard to test
Construction of objects II ,[object Object],[object Object],class Car { public function __construct() { $this->engine = new Engine(); $this->tire  = TireFactory::createTire(); } } class Car { public function __construct(Engine $eng, Tire $tire) { $this->engine = $eng; $this->tire  = $tire; } } Work in constructor (Anti-Pattern) Hard to test Piece of cake to test
Construction of objects II ,[object Object],[object Object],class Car { public function __construct() { $this->engine = new Engine(); $this->tire  = TireFactory::createTire(); } } class Car { public function __construct(Engine $eng, Tire $tire) { $this->engine = $eng; $this->tire  = $tire; } } Work in constructor (Anti-Pattern) Dependency Injection Hard to test Piece of cake to test
Dependency Injection ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object]
Law of Demeter ,[object Object],class Driver { public function drive($miles) { $this->vehicle->engine->start(); $this->vehicle->drive($miles); $this->vehicle->engine->stop(); }
Law of Demeter ,[object Object],class Driver { public function drive($miles) { $this->vehicle->engine->start(); $this->vehicle->drive($miles); $this->vehicle->engine->stop(); } Driver coupled to Engine
Law of Demeter ,[object Object],class Driver { public function drive($miles) { $this->vehicle->engine->start(); $this->vehicle->drive($miles); $this->vehicle->engine->stop(); } Hard to test: test always needs Engine or a mock of it Driver coupled to Engine
Law of Demeter ,[object Object],class Driver { public function drive($miles) { $this->vehicle->engine->start(); $this->vehicle->drive($miles); $this->vehicle->engine->stop(); } Internal state of Vehicle revealed Driver coupled to Engine Hard to test: test always needs Engine or a mock of it
Law of Demeter ,[object Object],[object Object],class Driver { public function drive($miles) { $this->vehicle->engine->start(); $this->vehicle->drive($miles); $this->vehicle->engine->stop(); } Internal state of Vehicle revealed class Driver { public function drive($miles) { $this->vehicle->start(); $this->vehicle->drive($miles); $this->vehicle->stop(); } Hard to test: test always needs Engine or a mock of it Driver coupled to Engine
Law of Demeter ,[object Object],[object Object],class Driver { public function drive($miles) { $this->vehicle->engine->start(); $this->vehicle->drive($miles); $this->vehicle->engine->stop(); } Internal state of Vehicle revealed class Driver { public function drive($miles) { $this->vehicle->start(); $this->vehicle->drive($miles); $this->vehicle->stop(); } Driver coupled to Engine Hard to test: test always needs Engine or a mock of it Driver not coupled to Engine: simpler to maintain
Law of Demeter ,[object Object],[object Object],class Driver { public function drive($miles) { $this->vehicle->engine->start(); $this->vehicle->drive($miles); $this->vehicle->engine->stop(); } Internal state of Vehicle revealed class Driver { public function drive($miles) { $this->vehicle->start(); $this->vehicle->drive($miles); $this->vehicle->stop(); } Piece of cake to test Hard to test: test always needs Engine or a mock of it Driver coupled to Engine Driver not coupled to Engine: simpler to maintain
Law of Demeter ,[object Object],[object Object],class Driver { public function drive($miles) { $this->vehicle->engine->start(); $this->vehicle->drive($miles); $this->vehicle->engine->stop(); } Internal state of Vehicle revealed class Driver { public function drive($miles) { $this->vehicle->start(); $this->vehicle->drive($miles); $this->vehicle->stop(); } Piece of cake to test Less prone to errors Driver coupled to Engine Hard to test: test always needs Engine or a mock of it Driver not coupled to Engine: simpler to maintain
Global state ,[object Object],[object Object]
Global state ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Global state: Singletons ,[object Object],[object Object]
Global state: Singletons ,[object Object],[object Object],[object Object],[object Object],[object Object]
Singleton with DI framework $binder->bind('Session') ->to('PhpSession')
Singleton with DI framework $binder->bind('Session') ->to('PhpSession') Configure the binding
Singleton with DI framework $binder->bind('Session') ->to('PhpSession') ->in(stubBindingScopes::$SINGLETON); Configure the binding Enforce singletoness: DI framework will only create one instance of PhpSession and inject always this same instance
Singleton with DI framework $binder->bind('Session') ->to('PhpSession') ->in(stubBindingScopes::$SINGLETON); class Processor { protected $session; /** * @Inject */ public function __construct(Session $session) { $this->session = $session; } … } Configure the binding Enforce singletoness: DI framework will only create one instance of PhpSession and inject always this same instance
Singleton with DI framework $binder->bind('Session') ->to('PhpSession') ->in(stubBindingScopes::$SINGLETON); class Processor { protected $session; /** * @Inject */ public function __construct(Session $session) { $this->session = $session; } … } Configure the binding Enforce singletoness: DI framework will only create one instance of PhpSession and inject always this same instance Dependency Injection
Singleton with DI framework $binder->bind('Session') ->to('PhpSession') ->in(stubBindingScopes::$SINGLETON); class Processor { protected $session; /** * @Inject */ public function __construct(Session $session) { $this->session = $session; } … } Configure the binding Enforce singletoness: DI framework will only create one instance of PhpSession and inject always this same instance Dependency Injection Tell DI framework to inject required parameters on creation of Processor
Singleton with DI framework $binder->bind('Session') ->to('PhpSession') ->in(stubBindingScopes::$SINGLETON); class Processor { protected $session; /** * @Inject */ public function __construct(Session $session) { $this->session = $session; } … } Configure the binding Enforce singletoness: DI framework will only create one instance of PhpSession and inject always this same instance Dependency Injection Piece of cake to test: independent of PhpSession Tell DI framework to inject required parameters on creation of Processor
Global state:  static  methods ,[object Object],[object Object],[object Object],[object Object]
Global state:  $_GET, $_POST, … ,[object Object]
Global state:  $_GET, $_POST, … ,[object Object],[object Object]
Global state:  $_GET, $_POST, … ,[object Object],[object Object],[object Object],[object Object]
Global state:  $_GET, $_POST, … ,[object Object],[object Object],[object Object],[object Object],[object Object]
Global state:  $_GET, $_POST, … ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Global state: Registry ,[object Object]
Global state: Registry ,[object Object],[object Object]
Global state: Registry ,[object Object],[object Object],[object Object]
Global state: Registry ,[object Object],[object Object],[object Object],[object Object]
 
Modify
Simplify Modify
Simplify Improve Modify
Finally… ,[object Object]
Singletons are really, really (and I mean really)
Singletons are really, really (and I mean really) EVIL
The End ,[object Object]
The End ,[object Object],[object Object]
Commercial break ,[object Object]
Commercial break ,[object Object],[object Object]
Commercial break ,[object Object],[object Object],[object Object]

Contenu connexe

Tendances

Dutch php a short tale about state machine
Dutch php   a short tale about state machineDutch php   a short tale about state machine
Dutch php a short tale about state machineŁukasz Chruściel
 
PHPSpec - the only Design Tool you need - 4Developers
PHPSpec - the only Design Tool you need - 4DevelopersPHPSpec - the only Design Tool you need - 4Developers
PHPSpec - the only Design Tool you need - 4DevelopersKacper Gunia
 
Good karma: UX Patterns and Unit Testing in Angular with Karma
Good karma: UX Patterns and Unit Testing in Angular with KarmaGood karma: UX Patterns and Unit Testing in Angular with Karma
Good karma: UX Patterns and Unit Testing in Angular with KarmaExoLeaders.com
 
Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Nikita Popov
 
jQuery from the very beginning
jQuery from the very beginningjQuery from the very beginning
jQuery from the very beginningAnis Ahmad
 
James Thomas - Serverless Machine Learning With TensorFlow - Codemotion Berli...
James Thomas - Serverless Machine Learning With TensorFlow - Codemotion Berli...James Thomas - Serverless Machine Learning With TensorFlow - Codemotion Berli...
James Thomas - Serverless Machine Learning With TensorFlow - Codemotion Berli...Codemotion
 
Concurrent test frameworks
Concurrent test frameworksConcurrent test frameworks
Concurrent test frameworksAndrea Giuliano
 
Ditching JQuery
Ditching JQueryDitching JQuery
Ditching JQueryhowlowck
 
Testing most things in JavaScript - LeedsJS 31/05/2017
Testing most things in JavaScript - LeedsJS 31/05/2017Testing most things in JavaScript - LeedsJS 31/05/2017
Testing most things in JavaScript - LeedsJS 31/05/2017Colin Oakley
 
Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Fwdays
 
OOP in JavaScript
OOP in JavaScriptOOP in JavaScript
OOP in JavaScriptEyal Vardi
 
Optimizing Magento by Preloading Data
Optimizing Magento by Preloading DataOptimizing Magento by Preloading Data
Optimizing Magento by Preloading DataIvan Chepurnyi
 
Using of TDD practices for Magento
Using of TDD practices for MagentoUsing of TDD practices for Magento
Using of TDD practices for MagentoIvan Chepurnyi
 

Tendances (20)

Dutch php a short tale about state machine
Dutch php   a short tale about state machineDutch php   a short tale about state machine
Dutch php a short tale about state machine
 
PHPSpec - the only Design Tool you need - 4Developers
PHPSpec - the only Design Tool you need - 4DevelopersPHPSpec - the only Design Tool you need - 4Developers
PHPSpec - the only Design Tool you need - 4Developers
 
Good karma: UX Patterns and Unit Testing in Angular with Karma
Good karma: UX Patterns and Unit Testing in Angular with KarmaGood karma: UX Patterns and Unit Testing in Angular with Karma
Good karma: UX Patterns and Unit Testing in Angular with Karma
 
Frontin like-a-backer
Frontin like-a-backerFrontin like-a-backer
Frontin like-a-backer
 
Barely Enough Design
Barely Enough DesignBarely Enough Design
Barely Enough Design
 
Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?
 
jQuery from the very beginning
jQuery from the very beginningjQuery from the very beginning
jQuery from the very beginning
 
James Thomas - Serverless Machine Learning With TensorFlow - Codemotion Berli...
James Thomas - Serverless Machine Learning With TensorFlow - Codemotion Berli...James Thomas - Serverless Machine Learning With TensorFlow - Codemotion Berli...
James Thomas - Serverless Machine Learning With TensorFlow - Codemotion Berli...
 
Concurrent test frameworks
Concurrent test frameworksConcurrent test frameworks
Concurrent test frameworks
 
QA for PHP projects
QA for PHP projectsQA for PHP projects
QA for PHP projects
 
Refactoring
RefactoringRefactoring
Refactoring
 
Ditching JQuery
Ditching JQueryDitching JQuery
Ditching JQuery
 
Testing most things in JavaScript - LeedsJS 31/05/2017
Testing most things in JavaScript - LeedsJS 31/05/2017Testing most things in JavaScript - LeedsJS 31/05/2017
Testing most things in JavaScript - LeedsJS 31/05/2017
 
TDD with PhpSpec
TDD with PhpSpecTDD with PhpSpec
TDD with PhpSpec
 
Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"
 
PHPSpec BDD for PHP
PHPSpec BDD for PHPPHPSpec BDD for PHP
PHPSpec BDD for PHP
 
OOP in JavaScript
OOP in JavaScriptOOP in JavaScript
OOP in JavaScript
 
Optimizing Magento by Preloading Data
Optimizing Magento by Preloading DataOptimizing Magento by Preloading Data
Optimizing Magento by Preloading Data
 
Using of TDD practices for Magento
Using of TDD practices for MagentoUsing of TDD practices for Magento
Using of TDD practices for Magento
 
Test driven development_for_php
Test driven development_for_phpTest driven development_for_php
Test driven development_for_php
 

En vedette

What A Wonderful World Av Annie
What A Wonderful World Av AnnieWhat A Wonderful World Av Annie
What A Wonderful World Av AnnieAnnie Lindgren
 
Frontend-Performance mit PHP
Frontend-Performance mit PHPFrontend-Performance mit PHP
Frontend-Performance mit PHPFrank Kleine
 
Modello - Riccardo Colombo
Modello - Riccardo ColomboModello - Riccardo Colombo
Modello - Riccardo ColomboNaba Design
 
Fake Food - Work In Progress
Fake Food - Work In ProgressFake Food - Work In Progress
Fake Food - Work In ProgressNaba Design
 

En vedette (7)

What A Wonderful World Av Annie
What A Wonderful World Av AnnieWhat A Wonderful World Av Annie
What A Wonderful World Av Annie
 
Vackra Bilder 2
Vackra Bilder  2Vackra Bilder  2
Vackra Bilder 2
 
Frontend-Performance mit PHP
Frontend-Performance mit PHPFrontend-Performance mit PHP
Frontend-Performance mit PHP
 
Gott Nytt År!
Gott Nytt År!Gott Nytt År!
Gott Nytt År!
 
Barnbarn Pps
Barnbarn PpsBarnbarn Pps
Barnbarn Pps
 
Modello - Riccardo Colombo
Modello - Riccardo ColomboModello - Riccardo Colombo
Modello - Riccardo Colombo
 
Fake Food - Work In Progress
Fake Food - Work In ProgressFake Food - Work In Progress
Fake Food - Work In Progress
 

Similaire à Things to consider for testable Code

Symfony 4 Workshop - Limenius
Symfony 4 Workshop - LimeniusSymfony 4 Workshop - Limenius
Symfony 4 Workshop - LimeniusIgnacio Martín
 
"Full Stack frameworks or a story about how to reconcile Front (good) and Bac...
"Full Stack frameworks or a story about how to reconcile Front (good) and Bac..."Full Stack frameworks or a story about how to reconcile Front (good) and Bac...
"Full Stack frameworks or a story about how to reconcile Front (good) and Bac...Fwdays
 
2012 02-04 fosdem 2012 - drools planner
2012 02-04 fosdem 2012 - drools planner2012 02-04 fosdem 2012 - drools planner
2012 02-04 fosdem 2012 - drools plannerGeoffrey De Smet
 
JUDCon London 2011 - Bin packing with drools planner by example
JUDCon London 2011 - Bin packing with drools planner by exampleJUDCon London 2011 - Bin packing with drools planner by example
JUDCon London 2011 - Bin packing with drools planner by exampleGeoffrey De Smet
 
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testabilityPHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testabilityGiorgio Sironi
 
Curso Symfony - Clase 4
Curso Symfony - Clase 4Curso Symfony - Clase 4
Curso Symfony - Clase 4Javier Eguiluz
 
Using Geeklog as a Web Application Framework
Using Geeklog as a Web Application FrameworkUsing Geeklog as a Web Application Framework
Using Geeklog as a Web Application FrameworkDirk Haun
 
Magento Live Australia 2016: Request Flow
Magento Live Australia 2016: Request FlowMagento Live Australia 2016: Request Flow
Magento Live Australia 2016: Request FlowVrann Tulika
 
Getting started with TDD - Confoo 2014
Getting started with TDD - Confoo 2014Getting started with TDD - Confoo 2014
Getting started with TDD - Confoo 2014Eric Hogue
 
Why is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenariosWhy is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenariosDivante
 
4Developers 2015: Be pragmatic, be SOLID - Krzysztof Menżyk
4Developers 2015: Be pragmatic, be SOLID - Krzysztof Menżyk4Developers 2015: Be pragmatic, be SOLID - Krzysztof Menżyk
4Developers 2015: Be pragmatic, be SOLID - Krzysztof MenżykPROIDEA
 
ActiveWeb: Chicago Java User Group Presentation
ActiveWeb: Chicago Java User Group PresentationActiveWeb: Chicago Java User Group Presentation
ActiveWeb: Chicago Java User Group Presentationipolevoy
 
Building Testable PHP Applications
Building Testable PHP ApplicationsBuilding Testable PHP Applications
Building Testable PHP Applicationschartjes
 
Total Compensationbuild.xml Builds, tests, and runs th.docx
Total Compensationbuild.xml      Builds, tests, and runs th.docxTotal Compensationbuild.xml      Builds, tests, and runs th.docx
Total Compensationbuild.xml Builds, tests, and runs th.docxturveycharlyn
 
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020Andrzej Jóźwiak
 
Testing persistence in PHP with DbUnit
Testing persistence in PHP with DbUnitTesting persistence in PHP with DbUnit
Testing persistence in PHP with DbUnitPeter Wilcsinszky
 

Similaire à Things to consider for testable Code (20)

Symfony 4 Workshop - Limenius
Symfony 4 Workshop - LimeniusSymfony 4 Workshop - Limenius
Symfony 4 Workshop - Limenius
 
"Full Stack frameworks or a story about how to reconcile Front (good) and Bac...
"Full Stack frameworks or a story about how to reconcile Front (good) and Bac..."Full Stack frameworks or a story about how to reconcile Front (good) and Bac...
"Full Stack frameworks or a story about how to reconcile Front (good) and Bac...
 
2012 02-04 fosdem 2012 - drools planner
2012 02-04 fosdem 2012 - drools planner2012 02-04 fosdem 2012 - drools planner
2012 02-04 fosdem 2012 - drools planner
 
JUDCon London 2011 - Bin packing with drools planner by example
JUDCon London 2011 - Bin packing with drools planner by exampleJUDCon London 2011 - Bin packing with drools planner by example
JUDCon London 2011 - Bin packing with drools planner by example
 
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testabilityPHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
 
Curso Symfony - Clase 4
Curso Symfony - Clase 4Curso Symfony - Clase 4
Curso Symfony - Clase 4
 
Using Geeklog as a Web Application Framework
Using Geeklog as a Web Application FrameworkUsing Geeklog as a Web Application Framework
Using Geeklog as a Web Application Framework
 
PHP Unit Testing
PHP Unit TestingPHP Unit Testing
PHP Unit Testing
 
Unittests für Dummies
Unittests für DummiesUnittests für Dummies
Unittests für Dummies
 
Magento Live Australia 2016: Request Flow
Magento Live Australia 2016: Request FlowMagento Live Australia 2016: Request Flow
Magento Live Australia 2016: Request Flow
 
Getting started with TDD - Confoo 2014
Getting started with TDD - Confoo 2014Getting started with TDD - Confoo 2014
Getting started with TDD - Confoo 2014
 
Why is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenariosWhy is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenarios
 
4Developers 2015: Be pragmatic, be SOLID - Krzysztof Menżyk
4Developers 2015: Be pragmatic, be SOLID - Krzysztof Menżyk4Developers 2015: Be pragmatic, be SOLID - Krzysztof Menżyk
4Developers 2015: Be pragmatic, be SOLID - Krzysztof Menżyk
 
Be pragmatic, be SOLID
Be pragmatic, be SOLIDBe pragmatic, be SOLID
Be pragmatic, be SOLID
 
ActiveWeb: Chicago Java User Group Presentation
ActiveWeb: Chicago Java User Group PresentationActiveWeb: Chicago Java User Group Presentation
ActiveWeb: Chicago Java User Group Presentation
 
Building Testable PHP Applications
Building Testable PHP ApplicationsBuilding Testable PHP Applications
Building Testable PHP Applications
 
Going web native
Going web nativeGoing web native
Going web native
 
Total Compensationbuild.xml Builds, tests, and runs th.docx
Total Compensationbuild.xml      Builds, tests, and runs th.docxTotal Compensationbuild.xml      Builds, tests, and runs th.docx
Total Compensationbuild.xml Builds, tests, and runs th.docx
 
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
 
Testing persistence in PHP with DbUnit
Testing persistence in PHP with DbUnitTesting persistence in PHP with DbUnit
Testing persistence in PHP with DbUnit
 

Dernier

ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 

Dernier (20)

ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 

Things to consider for testable Code

  • 1. Things to consider for testable code Frank Kleine, 27.10.2009
  • 2.
  • 3.
  • 8. Didn't we forget something really important?
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34. Singleton with DI framework $binder->bind('Session') ->to('PhpSession')
  • 35. Singleton with DI framework $binder->bind('Session') ->to('PhpSession') Configure the binding
  • 36. Singleton with DI framework $binder->bind('Session') ->to('PhpSession') ->in(stubBindingScopes::$SINGLETON); Configure the binding Enforce singletoness: DI framework will only create one instance of PhpSession and inject always this same instance
  • 37. Singleton with DI framework $binder->bind('Session') ->to('PhpSession') ->in(stubBindingScopes::$SINGLETON); class Processor { protected $session; /** * @Inject */ public function __construct(Session $session) { $this->session = $session; } … } Configure the binding Enforce singletoness: DI framework will only create one instance of PhpSession and inject always this same instance
  • 38. Singleton with DI framework $binder->bind('Session') ->to('PhpSession') ->in(stubBindingScopes::$SINGLETON); class Processor { protected $session; /** * @Inject */ public function __construct(Session $session) { $this->session = $session; } … } Configure the binding Enforce singletoness: DI framework will only create one instance of PhpSession and inject always this same instance Dependency Injection
  • 39. Singleton with DI framework $binder->bind('Session') ->to('PhpSession') ->in(stubBindingScopes::$SINGLETON); class Processor { protected $session; /** * @Inject */ public function __construct(Session $session) { $this->session = $session; } … } Configure the binding Enforce singletoness: DI framework will only create one instance of PhpSession and inject always this same instance Dependency Injection Tell DI framework to inject required parameters on creation of Processor
  • 40. Singleton with DI framework $binder->bind('Session') ->to('PhpSession') ->in(stubBindingScopes::$SINGLETON); class Processor { protected $session; /** * @Inject */ public function __construct(Session $session) { $this->session = $session; } … } Configure the binding Enforce singletoness: DI framework will only create one instance of PhpSession and inject always this same instance Dependency Injection Piece of cake to test: independent of PhpSession Tell DI framework to inject required parameters on creation of Processor
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.  
  • 55.
  • 56. Singletons are really, really (and I mean really)
  • 57. Singletons are really, really (and I mean really) EVIL
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.

Notes de l'éditeur

  1. Simplify – remove accidental complexity Improve – smaller methods, better namings. You need unit tests for this.
  2. Simplify – remove accidental complexity Improve – smaller methods, better namings. You need unit tests for this.
  3. Simplify – remove accidental complexity Improve – smaller methods, better namings. You need unit tests for this.
  4. Simplify – remove accidental complexity Improve – smaller methods, better namings. You need unit tests for this.