Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Conscious Decoupling - Lone Star PHP


Published on

By using a comprehensive feature-filled framework we can build software fast. On the other hand, by decoupling our applications we can build sofware that is independent of our framework and infrastructure choices, and therefore longer lasting.

We can't do both, is one approach always right?

In this talk we'll look at different decoupling techniques, what problems they solve, and when they make sense. We will learn some concrete techniques to discover where we should be investing in decoupling, and when we should let the framework maintainers do the work for us.

Published in: Software
  • Be the first to comment

Conscious Decoupling - Lone Star PHP

  1. 1. Conscious Decoupling Building Software that Lasts Ciaran McNulty at Lone Star PHP 2016
  2. 2. Me Hi, I'm Ciaran and I'm a PHP developer I am a consultant with Inviqa I maintain PhpSpec and contribute to Behat
  3. 3. My early period: Write procedural code!
  4. 4. My middle period: Decouple all the things!
  5. 5. My late period: Decouple some of the things!
  6. 6. What is coupling?
  7. 7. class TaxiDispatcher { function dispatch (Car $car, $destination) { $this->chargeCustomer(); $car->goTo($destination); } }
  8. 8. Problems with dependencies 4 Changes to Car mean changes to Dispatcher 4 Can't reuse Dispatcher with new vehicles
  9. 9. interface Vehicle { public function goTo($destination); } class TaxiDispatcher { function dispatch (Vehicle $vehicle, $destination) { $this->chargeCustomer(); $vehicle->goTo($destination); } }
  10. 10. Benefits of abstraction 4 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
  11. 11. Defining abstractions 4 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
  12. 12. Example - upgrade eligibility Bad abstraction: interface HttpClient { public function getData : array ($url, $parameters); }
  13. 13. Example - upgrade eligibility Better abstraction: interface UpgradeEligibilityChecker { public function getContractId : int ($phoneNumber); public function isEligibleForUpgrade : bool ($contractId); }
  14. 14. Example - upgrade eligibility Best abstraction: interface UpgradeEligibilityChecker { public function isEligibleForUpgrade : bool ($phoneNumber); }
  15. 15. Or, get on the bus 4 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
  16. 16. Events Direct coupling: $notifier->notifyLawyersAboutPurchase($id, $item, $cost); Event style: $eventBus->dispatch( new ItemWasPurchased($id, $item, $cost); );
  17. 17. Commands Use case / service style: $invoiceApprover->approveInvoice(1234); Command style: $commandBus->dispatch( new ApproveInvoice(1234); );
  18. 18. Advantages of decoupling via abstractions 4 Cleaner APIs 4 Swappable components 4 Separation of concerns 4 Easier to test 4 No 'ripple effect' around changes
  19. 19. Disadvantages of decoupling via abstractions 4 Makes execution flow harder to follow 4 Adds more complexity (more files in codebase) 4 Cognitive cost of thinking of good abstractions
  20. 20. Architectural boundaries 4 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.
  21. 21. Frameworks Coupling to other people's code
  22. 22. Highly coupled frameworks Some 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
  23. 23. More coupled framworks Some 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
  24. 24. More decoupled frameworks Some 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
  25. 25. Very decoupled frameworks Some 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
  26. 26. Only decouple where you need to (wait, how do we know where that is?)
  27. 27. Coupled code get painful when change happens Decoupled code was wasted effort when change doesn't happen
  28. 28. Where does change come from?
  29. 29. Where does change come from? People!
  30. 30. Decouple your system to respond to different streams of change
  31. 31. Organizations which design systems ... are constrained to produce designs which are copies of the communication structures of these organizations 1 Melvin Conway, 1967
  32. 32. Context mapping 4 Identify subdomains 4 Identify which are: 4 Core subdomains 4 Supporting subdomains 4 Generic subdomains 4 Mirror the structure in your architecture
  33. 33. The bad news: You will get it wrong You only find out afterwards
  34. 34. The good news: It's not just you
  35. 35. Things to check out 4 Context Mapping 4 Hexagonal Architecture 4 Modelling by Example
  36. 36. Thank you! 4 @ciaranmcnulty 4 Lead Maintainer of PhpSpec 4 SeniorTrainer at Inviqa 4 Questions?