52
Conscious Decoupling Building Softwar e that Las ts Ciaran McNulty a t Lone Star PHP 2016

Conscious Decoupling - Lone Star PHP

Embed Size (px)

Citation preview

Page 1: Conscious Decoupling - Lone Star PHP

Conscious DecouplingBuilding Software that LastsCiaran McNulty at Lone Star PHP 2016

Page 2: Conscious Decoupling - Lone Star PHP

MeHi, I'm Ciaran and I'm a PHP developer

I am a consultant with Inviqa

I maintain PhpSpec and contribute to Behat

Page 3: Conscious Decoupling - Lone Star PHP

My early period: Write procedural code!

Page 4: Conscious Decoupling - Lone Star PHP

My middle period: Decouple all the things!

Page 5: Conscious Decoupling - Lone Star PHP

My late period: Decouple some of the things!

Page 6: Conscious Decoupling - Lone Star PHP

What is coupling?

Page 7: Conscious Decoupling - Lone Star PHP

class TaxiDispatcher { function dispatch (Car $car, $destination) { $this->chargeCustomer(); $car->goTo($destination); }}

Page 8: Conscious Decoupling - Lone Star PHP
Page 9: Conscious Decoupling - Lone Star PHP
Page 10: Conscious Decoupling - Lone Star PHP
Page 11: Conscious Decoupling - Lone Star PHP

Problems with dependencies4 Changes to Car mean changes to Dispatcher

4 Can't reuse Dispatcher with new vehicles

Page 12: Conscious Decoupling - Lone Star PHP

interface Vehicle{ public function goTo($destination);}

class TaxiDispatcher { function dispatch (Vehicle $vehicle, $destination) { $this->chargeCustomer(); $vehicle->goTo($destination); }}

Page 13: Conscious Decoupling - Lone Star PHP
Page 14: Conscious Decoupling - Lone Star PHP
Page 15: Conscious Decoupling - Lone Star PHP
Page 16: Conscious Decoupling - Lone Star PHP
Page 17: Conscious Decoupling - Lone Star PHP

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

Page 18: Conscious Decoupling - Lone Star PHP

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

Page 19: Conscious Decoupling - Lone Star PHP

Example - upgrade eligibilityBad abstraction:

interface HttpClient{ public function getData : array ($url, $parameters);}

Page 20: Conscious Decoupling - Lone Star PHP

Example - upgrade eligibilityBetter abstraction:

interface UpgradeEligibilityChecker{ public function getContractId : int ($phoneNumber); public function isEligibleForUpgrade : bool ($contractId);}

Page 21: Conscious Decoupling - Lone Star PHP

Example - upgrade eligibilityBest abstraction:

interface UpgradeEligibilityChecker{ public function isEligibleForUpgrade : bool ($phoneNumber);}

Page 22: Conscious Decoupling - Lone Star PHP

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

Page 23: Conscious Decoupling - Lone Star PHP

EventsDirect coupling:

$notifier->notifyLawyersAboutPurchase($id, $item, $cost);

Event style:

$eventBus->dispatch( new ItemWasPurchased($id, $item, $cost););

Page 24: Conscious Decoupling - Lone Star PHP

CommandsUse case / service style:

$invoiceApprover->approveInvoice(1234);

Command style:

$commandBus->dispatch( new ApproveInvoice(1234););

Page 25: Conscious Decoupling - Lone Star PHP

Advantages of decoupling via abstractions4 Cleaner APIs

4 Swappable components

4 Separation of concerns

4 Easier to test

4 No 'ripple effect' around changes

Page 26: Conscious Decoupling - Lone Star PHP

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

Page 27: Conscious Decoupling - Lone Star PHP

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.

Page 28: Conscious Decoupling - Lone Star PHP
Page 29: Conscious Decoupling - Lone Star PHP
Page 30: Conscious Decoupling - Lone Star PHP
Page 31: Conscious Decoupling - Lone Star PHP

FrameworksCoupling to other people's code

Page 32: Conscious Decoupling - Lone Star PHP

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

Page 33: Conscious Decoupling - Lone Star PHP

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

Page 34: Conscious Decoupling - Lone Star PHP

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

Page 35: Conscious Decoupling - Lone Star PHP

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

Page 36: Conscious Decoupling - Lone Star PHP

Only decouple where you need to

(wait, how do we know where that is?)

Page 37: Conscious Decoupling - Lone Star PHP

Coupled code get painful when change happens

Decoupled code was wasted effort when change

doesn't happen

Page 38: Conscious Decoupling - Lone Star PHP

Where does change come from?

Page 39: Conscious Decoupling - Lone Star PHP

Where does change come from?

People!

Page 40: Conscious Decoupling - Lone Star PHP
Page 41: Conscious Decoupling - Lone Star PHP
Page 42: Conscious Decoupling - Lone Star PHP
Page 43: Conscious Decoupling - Lone Star PHP
Page 44: Conscious Decoupling - Lone Star PHP

Decouple your system to respond to different streams of change

Page 45: Conscious Decoupling - Lone Star PHP
Page 46: Conscious Decoupling - Lone Star PHP
Page 47: Conscious Decoupling - Lone Star PHP

Organizations which design systems ... are constrained to produce designs which are copies of the communication structures

of these organizations1

Melvin Conway, 1967

Page 48: Conscious Decoupling - Lone Star PHP

Context mapping4 Identify subdomains

4 Identify which are:

4 Core subdomains

4 Supporting subdomains

4 Generic subdomains

4 Mirror the structure in your architecture

Page 49: Conscious Decoupling - Lone Star PHP

The bad news:You will get it wrong

You only find out afterwards

Page 50: Conscious Decoupling - Lone Star PHP

The good news:It's not just you

Page 51: Conscious Decoupling - Lone Star PHP

Things to check out4 Context Mapping

4 Hexagonal Architecture

4 Modelling by Example

Page 52: Conscious Decoupling - Lone Star PHP

Thank you!4 @ciaranmcnulty

4 Lead Maintainer of PhpSpec

4 Senior Trainer at Inviqa

4 https://joind.in/talk/e20e3

Questions?