Upload
ciaranmcnulty
View
576
Download
0
Embed Size (px)
Citation preview
Conscious DecouplingBuilding Software that LastsCiaran McNulty at Lone Star PHP 2016
MeHi, I'm Ciaran and I'm a PHP developer
I am a consultant with Inviqa
I maintain PhpSpec and contribute to Behat
My early period: Write procedural code!
My middle period: Decouple all the things!
My late period: Decouple some of the things!
What is coupling?
class TaxiDispatcher { function dispatch (Car $car, $destination) { $this->chargeCustomer(); $car->goTo($destination); }}
Problems with dependencies4 Changes to Car mean changes to Dispatcher
4 Can't reuse Dispatcher with new vehicles
interface Vehicle{ public function goTo($destination);}
class TaxiDispatcher { function dispatch (Vehicle $vehicle, $destination) { $this->chargeCustomer(); $vehicle->goTo($destination); }}
Benefits of abstraction4 Now we only need to rewrite Dispatcher and Car when Vehicle changes
4 Vehicle can be very stable; it's just an interface
4 Can make new transportation types that implement Vehicle
Defining abstractions4 Start with the abstraction, not the detail
4 Contract should be a simple as possible
4 Contract should focus on resposibilities
4 Hide details of underlying APIs
Example - upgrade eligibilityBad abstraction:
interface HttpClient{ public function getData : array ($url, $parameters);}
Example - upgrade eligibilityBetter abstraction:
interface UpgradeEligibilityChecker{ public function getContractId : int ($phoneNumber); public function isEligibleForUpgrade : bool ($contractId);}
Example - upgrade eligibilityBest abstraction:
interface UpgradeEligibilityChecker{ public function isEligibleForUpgrade : bool ($phoneNumber);}
Or, get on the bus4 Migrate actions from method calls to objects
4 Depend on a bus rather than depending on contracts
4 Don't know anything about what handles the action
EventsDirect coupling:
$notifier->notifyLawyersAboutPurchase($id, $item, $cost);
Event style:
$eventBus->dispatch( new ItemWasPurchased($id, $item, $cost););
CommandsUse case / service style:
$invoiceApprover->approveInvoice(1234);
Command style:
$commandBus->dispatch( new ApproveInvoice(1234););
Advantages of decoupling via abstractions4 Cleaner APIs
4 Swappable components
4 Separation of concerns
4 Easier to test
4 No 'ripple effect' around changes
Disadvantages of decoupling via abstractions4 Makes execution flow harder to follow
4 Adds more complexity (more files in codebase)
4 Cognitive cost of thinking of good abstractions
Architectural boundaries4 Abstractions are going to change when the use cases
change
4 Interfaces are more tightly coupled to code that uses them
4 Interfaces 'belong' in the same architectural boundary as the code that uses them.
FrameworksCoupling to other people's code
Highly coupled frameworksSome frameworks let you go really quickly by coupling directly to them.
You probably won't be able to reuse your code without considerable effort.
4 Drupal
4 Magento
4 Wordpress
More coupled framworksSome frameworks let you go faster when you adopt their conventions.
You can reuse your code with a fair amount of additional effort.
4 Laravel
4 Symfony1
4 CakePHP
More decoupled frameworksSome frameworks encourage decoupling so your code is more reusable.
You will go a little slower because you need to explicitly configure things
4 Symfony 2
4 Zend Framework 2
Very decoupled frameworksSome frameworks are extremely decoupled so code is almost entirely reusable.
You almost have to construct the entire framework yourself from components - this can be hard!
4 D.I.Y.
4 Zend Expressive
Only decouple where you need to
(wait, how do we know where that is?)
Coupled code get painful when change happens
Decoupled code was wasted effort when change
doesn't happen
Where does change come from?
Where does change come from?
People!
Decouple your system to respond to different streams of change
Organizations which design systems ... are constrained to produce designs which are copies of the communication structures
of these organizations1
Melvin Conway, 1967
Context mapping4 Identify subdomains
4 Identify which are:
4 Core subdomains
4 Supporting subdomains
4 Generic subdomains
4 Mirror the structure in your architecture
The bad news:You will get it wrong
You only find out afterwards
The good news:It's not just you
Things to check out4 Context Mapping
4 Hexagonal Architecture
4 Modelling by Example
Thank you!4 @ciaranmcnulty
4 Lead Maintainer of PhpSpec
4 Senior Trainer at Inviqa
4 https://joind.in/talk/e20e3
Questions?