Conscious Decoupling - Lone Star PHP
-
Upload
ciaranmcnulty -
Category
Software
-
view
576 -
download
0
Transcript of Conscious Decoupling - Lone Star PHP
![Page 1: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/1.jpg)
Conscious DecouplingBuilding Software that LastsCiaran McNulty at Lone Star PHP 2016
![Page 2: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/2.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/3.jpg)
My early period: Write procedural code!
![Page 4: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/4.jpg)
My middle period: Decouple all the things!
![Page 5: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/5.jpg)
My late period: Decouple some of the things!
![Page 6: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/6.jpg)
What is coupling?
![Page 7: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/7.jpg)
class TaxiDispatcher { function dispatch (Car $car, $destination) { $this->chargeCustomer(); $car->goTo($destination); }}
![Page 8: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/8.jpg)
![Page 9: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/9.jpg)
![Page 10: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/10.jpg)
![Page 11: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/11.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/12.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/13.jpg)
![Page 14: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/14.jpg)
![Page 15: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/15.jpg)
![Page 16: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/16.jpg)
![Page 17: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/17.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/18.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/19.jpg)
Example - upgrade eligibilityBad abstraction:
interface HttpClient{ public function getData : array ($url, $parameters);}
![Page 20: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/20.jpg)
Example - upgrade eligibilityBetter abstraction:
interface UpgradeEligibilityChecker{ public function getContractId : int ($phoneNumber); public function isEligibleForUpgrade : bool ($contractId);}
![Page 21: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/21.jpg)
Example - upgrade eligibilityBest abstraction:
interface UpgradeEligibilityChecker{ public function isEligibleForUpgrade : bool ($phoneNumber);}
![Page 22: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/22.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/23.jpg)
EventsDirect coupling:
$notifier->notifyLawyersAboutPurchase($id, $item, $cost);
Event style:
$eventBus->dispatch( new ItemWasPurchased($id, $item, $cost););
![Page 24: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/24.jpg)
CommandsUse case / service style:
$invoiceApprover->approveInvoice(1234);
Command style:
$commandBus->dispatch( new ApproveInvoice(1234););
![Page 25: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/25.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/26.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/27.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/28.jpg)
![Page 29: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/29.jpg)
![Page 30: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/30.jpg)
![Page 31: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/31.jpg)
FrameworksCoupling to other people's code
![Page 32: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/32.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/33.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/34.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/35.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/36.jpg)
Only decouple where you need to
(wait, how do we know where that is?)
![Page 37: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/37.jpg)
Coupled code get painful when change happens
Decoupled code was wasted effort when change
doesn't happen
![Page 38: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/38.jpg)
Where does change come from?
![Page 39: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/39.jpg)
Where does change come from?
People!
![Page 40: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/40.jpg)
![Page 41: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/41.jpg)
![Page 42: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/42.jpg)
![Page 43: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/43.jpg)
![Page 44: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/44.jpg)
Decouple your system to respond to different streams of change
![Page 45: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/45.jpg)
![Page 46: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/46.jpg)
![Page 47: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/47.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/48.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/49.jpg)
The bad news:You will get it wrong
You only find out afterwards
![Page 50: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/50.jpg)
The good news:It's not just you
![Page 51: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/51.jpg)
Things to check out4 Context Mapping
4 Hexagonal Architecture
4 Modelling by Example
![Page 52: Conscious Decoupling - Lone Star PHP](https://reader031.fdocuments.us/reader031/viewer/2022030301/587f3d2e1a28ab43318b4f45/html5/thumbnails/52.jpg)
Thank you!4 @ciaranmcnulty
4 Lead Maintainer of PhpSpec
4 Senior Trainer at Inviqa
4 https://joind.in/talk/e20e3
Questions?