SlideShare a Scribd company logo
So S.O.L.I.D Fu
Designing Better Code
Abstract
A chat about some of the most important
principles in software development. Discover
or get a refresher on these tried and tested
techniques for designing better code.
SaaS eCommerce Platform
What’s SOLID?
5 Principles
● Single Responsibility Principle
● Open / Closed Principle
● Liskov Substitution Principle
● Interface Segregation Principle
● Dependency Inversion Principle
SRP
OCP
LSPISP
DIP
S.O.L.I.D
Why S.O.L.I.D?
● Maintainable
● Understandable
● Extendable
● Testable
● Debuggable
● Reusable
● Robust, less fragile
BETTER
SOFTWARE
SRP
OCP
LSPISP
DIP
class Product
{
public function getId()
public function getName()
public function getPrice()
public function getStock()
public function setStock($stock)
public function getDescription()
}
https://github.com/neilcrookes/SoSOLIDFu
class CreditCard
{
public function getExpiryYear()
public function getExpiryMonth()
public function getLast4()
public function getToken()
}
https://github.com/neilcrookes/SoSOLIDFu
class Gateway
{
public function purchase(Shop $shop, CreditCard $card)
}
https://github.com/neilcrookes/SoSOLIDFu
class Shop
{
public function addToBasket(Product $product)
public function removeFromBasket(Product $product)
public function getTotal()
public function getBasket()
public function checkout(CreditCard $card)
protected function sendOrderConfirmationEmail(CreditCard $card)
}
https://github.com/neilcrookes/SoSOLIDFu
Single
Responsibility
States that
● Classes should have only one reason to change
● Therefore should have only one responsibility
class Shop
{
public function addToBasket(Product $product)
public function removeFromBasket(Product $product)
public function getTotal()
public function getBasket()
public function checkout(CreditCard $card)
protected function sendOrderConfirmationEmail(CreditCard $card)
}
https://github.com/neilcrookes/SoSOLIDFu
class Shop
{
public function addToBasket(Product $product)
public function removeFromBasket(Product $product)
public function getTotal()
public function getBasket()
public function checkout(CreditCard $card)
protected function sendOrderConfirmationEmail(CreditCard $card)
}
https://github.com/neilcrookes/SoSOLIDFu
class Basket
{
public function addToBasket(Product $product)
public function removeFromBasket(Product $product)
public function getTotal()
public function getBasket()
}
Class Checkout
{
public function checkout(CreditCard $card)
protected function sendOrderConfirmationEmail(CreditCard $card)
}
https://github.com/neilcrookes/SoSOLIDFu
Class Checkout
{
public function checkout(Basket $basket, CreditCard $card)
{
try
{
$reference = $this->gateway->purchase($basket, $card);
$this->sendOrderConfirmationEmail($basket, $card);
return true;
}
catch (GatewayException $e)
{
return false;
}
}
}
https://github.com/neilcrookes/SoSOLIDFu
Open / Closed
Objects or libraries should be open for extension, but closed for modification.
● Easy to add new features OR change behaviour
● Without modifying original
● Purists: without extending original…!?
Eh?
Open / Closed cont
Extension points
● Events
● Plugin architecture
● Composition
● Strategy pattern
● Callbacks
Open / Closed cont
Closed for modification - Clarity of intent
● Final classes
● Private members
● Encourage extension in expected / supported way
● Encourages decoupling
● Reduces risk of breaking changes
final class Checkout
{
public function checkout(Basket $basket, CreditCard $card)
{
try
{
$reference = $this->gateway->purchase($basket, $card);
$this->sendOrderConfirmationEmail($basket, $card);
fire(new CheckoutEvent($basket));
return true;
}
catch (GatewayException $e)
{
return false;
}
}
}
https://github.com/neilcrookes/SoSOLIDFu
final class Checkout
{
public function sendOrderConfirmationEmail(Basket $basket, CreditCard $card)
{
$message = "Thank you for your order.n";
foreach ($basket as $item)
{
/** @var Product $product */
$product = $item['product'];
$message .= "n" . $product->getName() . ' x ' . $item['quantity']
. ' @ £' . $product->getPrice();
}
$message .= "nnPayment has been taken from: xxxx xxxx xxxx " . $card->getLast4()
. ' Ex: ' . $card->getExpiryMonth() . '/' . $card->getExpiryYear();
mail('customer@domain.com', 'Your order at my store', $message);
}
}
https://github.com/neilcrookes/SoSOLIDFu
Liskov Substitution
States that you should be able to swap dependencies for a subclass class without
causing unexpected results
final class Checkout
{
public function sendOrderConfirmationEmail(Basket $basket, CreditCard $card)
{
$message = "Thank you for your order.n";
foreach ($basket as $item)
{
/** @var Product $product */
$product = $item['product'];
$message .= "n" . $product->getBasketTitle() . ' x ' . $item['quantity']
. ' @ £' . $product->getPrice();
}
$message .= "nnPayment has been taken from: xxxx xxxx xxxx " . $card->getLast4()
. ' Ex: ' . $card->getExpiryMonth() . '/' . $card->getExpiryYear();
mail('customer@domain.com', 'Your order at my store', $message);
}
}
https://github.com/neilcrookes/SoSOLIDFu
interface PurchasableInterface
{
public function getId();
public function getPrice();
public function getStock();
public function setStock($stock);
public function getBasketTitle();
}
https://github.com/neilcrookes/SoSOLIDFu
Interface
Segregation
● An interface should only impose the methods that a client relies on
● Split unrelated interface methods into separate interfaces
interface PurchasableInterface
{
public function getId();
public function getPrice();
public function getBasketTitle();
}
interface StockableInterface
{
public function getStock();
public function setStock($stock);
}
https://github.com/neilcrookes/SoSOLIDFu
final class Checkout
{
public function checkout(Basket $basket, CreditCard $card)
}
class Gateway
{
public function purchase(Basket $basket, CreditCard $card)
}
https://github.com/neilcrookes/SoSOLIDFu
Dependency
Inversion
● High level should not depend on low level modules, both should depend on
abstractions
● Abstractions should not depend upon details. Details should depend upon
abstractions.
● Depend on abstractions (abstracts / interfaces), not concretes
● Code to an Interface
interface ChargeableInterface
{
public function getChargeableAmount();
}
interface GatewayInterface
{
public function charge(ChargeableInterface $chargeable,
PaymentMethodInterface $paymentMethod);
}
interface PaymentMethodInterface
{
public function getToken();
public function getDetails();
}
https://github.com/neilcrookes/SoSOLIDFu
final class Checkout
{
public function checkout(ChargeableInterface $basket, PaymentMethodInterface $card)
}
class Gateway
{
public function purchase(ChargeableInterface $basket, PaymentMethodInterface $card)
}
https://github.com/neilcrookes/SoSOLIDFu
Conclusion:
Get Classy
● Actually only add some Interfaces, and an Event
● Our code is designed much better
● Easy to understand, extend
● More robust, less likely to introduce bugs
● Lots of classes is fine
● Lots of interfaces is fine
Questions
● Now?
● At the pub?
● @neilcrookes

More Related Content

What's hot

PhpSpec 2.0 ilustrated by examples
PhpSpec 2.0 ilustrated by examplesPhpSpec 2.0 ilustrated by examples
PhpSpec 2.0 ilustrated by examples
Marcello Duarte
 
The IoC Hydra
The IoC HydraThe IoC Hydra
The IoC Hydra
Kacper Gunia
 
The IoC Hydra - Dutch PHP Conference 2016
The IoC Hydra - Dutch PHP Conference 2016The IoC Hydra - Dutch PHP Conference 2016
The IoC Hydra - Dutch PHP Conference 2016
Kacper Gunia
 
PHPunit and you
PHPunit and youPHPunit and you
PHPunit and you
markstory
 
Let's write secure Drupal code! - 13.09.2018 @ Drupal Europe, Darmstadt, Germany
Let's write secure Drupal code! - 13.09.2018 @ Drupal Europe, Darmstadt, GermanyLet's write secure Drupal code! - 13.09.2018 @ Drupal Europe, Darmstadt, Germany
Let's write secure Drupal code! - 13.09.2018 @ Drupal Europe, Darmstadt, Germany
Balázs Tatár
 
SOLID PRINCIPLES
SOLID PRINCIPLESSOLID PRINCIPLES
SOLID PRINCIPLES
Luciano Queiroz
 
PHPUnit Episode iv.iii: Return of the tests
PHPUnit Episode iv.iii: Return of the testsPHPUnit Episode iv.iii: Return of the tests
PHPUnit Episode iv.iii: Return of the tests
Michelangelo van Dam
 
Building a Portfolio With Custom Post Types
Building a Portfolio With Custom Post TypesBuilding a Portfolio With Custom Post Types
Building a Portfolio With Custom Post Types
Alex Blackie
 
Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5
Leonardo Proietti
 
November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2
Kacper Gunia
 
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers CracowForget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Kacper Gunia
 
Hacking Your Way To Better Security - Dutch PHP Conference 2016
Hacking Your Way To Better Security - Dutch PHP Conference 2016Hacking Your Way To Better Security - Dutch PHP Conference 2016
Hacking Your Way To Better Security - Dutch PHP Conference 2016
Colin O'Dell
 
Crafting beautiful software
Crafting beautiful softwareCrafting beautiful software
Crafting beautiful software
Jorn Oomen
 
Your code sucks, let's fix it
Your code sucks, let's fix itYour code sucks, let's fix it
Your code sucks, let's fix it
Rafael Dohms
 
Solid principles
Solid principlesSolid principles
Solid principles
Declan Whelan
 
CGI::Prototype (NPW 2006)
CGI::Prototype (NPW 2006)CGI::Prototype (NPW 2006)
CGI::Prototype (NPW 2006)
brian d foy
 
Your code sucks, let's fix it! - php|tek13
Your code sucks, let's fix it! - php|tek13Your code sucks, let's fix it! - php|tek13
Your code sucks, let's fix it! - php|tek13
Rafael Dohms
 
Система рендеринга в Magento
Система рендеринга в MagentoСистема рендеринга в Magento
Система рендеринга в Magento
Magecom Ukraine
 
Let's write secure Drupal code!
Let's write secure Drupal code!Let's write secure Drupal code!
Let's write secure Drupal code!
Balázs Tatár
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven Design
Pascal Larocque
 

What's hot (20)

PhpSpec 2.0 ilustrated by examples
PhpSpec 2.0 ilustrated by examplesPhpSpec 2.0 ilustrated by examples
PhpSpec 2.0 ilustrated by examples
 
The IoC Hydra
The IoC HydraThe IoC Hydra
The IoC Hydra
 
The IoC Hydra - Dutch PHP Conference 2016
The IoC Hydra - Dutch PHP Conference 2016The IoC Hydra - Dutch PHP Conference 2016
The IoC Hydra - Dutch PHP Conference 2016
 
PHPunit and you
PHPunit and youPHPunit and you
PHPunit and you
 
Let's write secure Drupal code! - 13.09.2018 @ Drupal Europe, Darmstadt, Germany
Let's write secure Drupal code! - 13.09.2018 @ Drupal Europe, Darmstadt, GermanyLet's write secure Drupal code! - 13.09.2018 @ Drupal Europe, Darmstadt, Germany
Let's write secure Drupal code! - 13.09.2018 @ Drupal Europe, Darmstadt, Germany
 
SOLID PRINCIPLES
SOLID PRINCIPLESSOLID PRINCIPLES
SOLID PRINCIPLES
 
PHPUnit Episode iv.iii: Return of the tests
PHPUnit Episode iv.iii: Return of the testsPHPUnit Episode iv.iii: Return of the tests
PHPUnit Episode iv.iii: Return of the tests
 
Building a Portfolio With Custom Post Types
Building a Portfolio With Custom Post TypesBuilding a Portfolio With Custom Post Types
Building a Portfolio With Custom Post Types
 
Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5
 
November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2
 
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers CracowForget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers Cracow
 
Hacking Your Way To Better Security - Dutch PHP Conference 2016
Hacking Your Way To Better Security - Dutch PHP Conference 2016Hacking Your Way To Better Security - Dutch PHP Conference 2016
Hacking Your Way To Better Security - Dutch PHP Conference 2016
 
Crafting beautiful software
Crafting beautiful softwareCrafting beautiful software
Crafting beautiful software
 
Your code sucks, let's fix it
Your code sucks, let's fix itYour code sucks, let's fix it
Your code sucks, let's fix it
 
Solid principles
Solid principlesSolid principles
Solid principles
 
CGI::Prototype (NPW 2006)
CGI::Prototype (NPW 2006)CGI::Prototype (NPW 2006)
CGI::Prototype (NPW 2006)
 
Your code sucks, let's fix it! - php|tek13
Your code sucks, let's fix it! - php|tek13Your code sucks, let's fix it! - php|tek13
Your code sucks, let's fix it! - php|tek13
 
Система рендеринга в Magento
Система рендеринга в MagentoСистема рендеринга в Magento
Система рендеринга в Magento
 
Let's write secure Drupal code!
Let's write secure Drupal code!Let's write secure Drupal code!
Let's write secure Drupal code!
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven Design
 

Similar to So S.O.L.I.D Fu - Designing Better Code

Hexagonal architecture
Hexagonal architectureHexagonal architecture
Hexagonal architecture
Alessandro Minoccheri
 
DDD on example of Symfony (Webcamp Odessa 2014)
DDD on example of Symfony (Webcamp Odessa 2014)DDD on example of Symfony (Webcamp Odessa 2014)
DDD on example of Symfony (Webcamp Odessa 2014)
Oleg Zinchenko
 
Dependency injection in Drupal 8
Dependency injection in Drupal 8Dependency injection in Drupal 8
Dependency injection in Drupal 8
Alexei Gorobets
 
PHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better CodePHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better Code
SWIFTotter Solutions
 
From framework coupled code to #microservices through #DDD /by @codelytv
From framework coupled code to #microservices through #DDD /by @codelytvFrom framework coupled code to #microservices through #DDD /by @codelytv
From framework coupled code to #microservices through #DDD /by @codelytv
CodelyTV
 
CDI @javaonehyderabad
CDI @javaonehyderabadCDI @javaonehyderabad
CDI @javaonehyderabad
Prasad Subramanian
 
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
James Titcumb
 
Domain-driven Design in PHP and Symfony - Drupal Camp Wroclaw!
Domain-driven Design in PHP and Symfony - Drupal Camp Wroclaw!Domain-driven Design in PHP and Symfony - Drupal Camp Wroclaw!
Domain-driven Design in PHP and Symfony - Drupal Camp Wroclaw!
Kacper Gunia
 
Practical AngularJS
Practical AngularJSPractical AngularJS
Practical AngularJS
Wei Ru
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
Alena Holligan
 
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
Dirk Haun
 
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
James Titcumb
 
Drupal 8 migrate!
Drupal 8 migrate!Drupal 8 migrate!
Drupal 8 migrate!
Pavel Makhrinsky
 
Fatc
FatcFatc
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
Rowan Merewood
 
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
James Titcumb
 
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег ЗинченкоWebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
GeeksLab Odessa
 
Php on the desktop and php gtk2
Php on the desktop and php gtk2Php on the desktop and php gtk2
Php on the desktop and php gtk2
Elizabeth Smith
 
SOLID
SOLIDSOLID
SproutCore and the Future of Web Apps
SproutCore and the Future of Web AppsSproutCore and the Future of Web Apps
SproutCore and the Future of Web Apps
Mike Subelsky
 

Similar to So S.O.L.I.D Fu - Designing Better Code (20)

Hexagonal architecture
Hexagonal architectureHexagonal architecture
Hexagonal architecture
 
DDD on example of Symfony (Webcamp Odessa 2014)
DDD on example of Symfony (Webcamp Odessa 2014)DDD on example of Symfony (Webcamp Odessa 2014)
DDD on example of Symfony (Webcamp Odessa 2014)
 
Dependency injection in Drupal 8
Dependency injection in Drupal 8Dependency injection in Drupal 8
Dependency injection in Drupal 8
 
PHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better CodePHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better Code
 
From framework coupled code to #microservices through #DDD /by @codelytv
From framework coupled code to #microservices through #DDD /by @codelytvFrom framework coupled code to #microservices through #DDD /by @codelytv
From framework coupled code to #microservices through #DDD /by @codelytv
 
CDI @javaonehyderabad
CDI @javaonehyderabadCDI @javaonehyderabad
CDI @javaonehyderabad
 
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
 
Domain-driven Design in PHP and Symfony - Drupal Camp Wroclaw!
Domain-driven Design in PHP and Symfony - Drupal Camp Wroclaw!Domain-driven Design in PHP and Symfony - Drupal Camp Wroclaw!
Domain-driven Design in PHP and Symfony - Drupal Camp Wroclaw!
 
Practical AngularJS
Practical AngularJSPractical AngularJS
Practical AngularJS
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
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
 
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
 
Drupal 8 migrate!
Drupal 8 migrate!Drupal 8 migrate!
Drupal 8 migrate!
 
Fatc
FatcFatc
Fatc
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
 
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
 
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег ЗинченкоWebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
 
Php on the desktop and php gtk2
Php on the desktop and php gtk2Php on the desktop and php gtk2
Php on the desktop and php gtk2
 
SOLID
SOLIDSOLID
SOLID
 
SproutCore and the Future of Web Apps
SproutCore and the Future of Web AppsSproutCore and the Future of Web Apps
SproutCore and the Future of Web Apps
 

Recently uploaded

E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 
SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024
Hironori Washizaki
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
Deuglo Infosystem Pvt Ltd
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
SOCRadar
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
What is Augmented Reality Image Tracking
What is Augmented Reality Image TrackingWhat is Augmented Reality Image Tracking
What is Augmented Reality Image Tracking
pavan998932
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
kalichargn70th171
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
Aftab Hussain
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
Rakesh Kumar R
 
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdfRevolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
Undress Baby
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
Rakesh Kumar R
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
Neo4j
 

Recently uploaded (20)

E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 
SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
What is Augmented Reality Image Tracking
What is Augmented Reality Image TrackingWhat is Augmented Reality Image Tracking
What is Augmented Reality Image Tracking
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
 
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdfRevolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
 

So S.O.L.I.D Fu - Designing Better Code

  • 2. Abstract A chat about some of the most important principles in software development. Discover or get a refresher on these tried and tested techniques for designing better code.
  • 4.
  • 5. What’s SOLID? 5 Principles ● Single Responsibility Principle ● Open / Closed Principle ● Liskov Substitution Principle ● Interface Segregation Principle ● Dependency Inversion Principle SRP OCP LSPISP DIP S.O.L.I.D
  • 6. Why S.O.L.I.D? ● Maintainable ● Understandable ● Extendable ● Testable ● Debuggable ● Reusable ● Robust, less fragile BETTER SOFTWARE SRP OCP LSPISP DIP
  • 7. class Product { public function getId() public function getName() public function getPrice() public function getStock() public function setStock($stock) public function getDescription() } https://github.com/neilcrookes/SoSOLIDFu
  • 8. class CreditCard { public function getExpiryYear() public function getExpiryMonth() public function getLast4() public function getToken() } https://github.com/neilcrookes/SoSOLIDFu
  • 9. class Gateway { public function purchase(Shop $shop, CreditCard $card) } https://github.com/neilcrookes/SoSOLIDFu
  • 10. class Shop { public function addToBasket(Product $product) public function removeFromBasket(Product $product) public function getTotal() public function getBasket() public function checkout(CreditCard $card) protected function sendOrderConfirmationEmail(CreditCard $card) } https://github.com/neilcrookes/SoSOLIDFu
  • 11. Single Responsibility States that ● Classes should have only one reason to change ● Therefore should have only one responsibility
  • 12. class Shop { public function addToBasket(Product $product) public function removeFromBasket(Product $product) public function getTotal() public function getBasket() public function checkout(CreditCard $card) protected function sendOrderConfirmationEmail(CreditCard $card) } https://github.com/neilcrookes/SoSOLIDFu
  • 13. class Shop { public function addToBasket(Product $product) public function removeFromBasket(Product $product) public function getTotal() public function getBasket() public function checkout(CreditCard $card) protected function sendOrderConfirmationEmail(CreditCard $card) } https://github.com/neilcrookes/SoSOLIDFu
  • 14. class Basket { public function addToBasket(Product $product) public function removeFromBasket(Product $product) public function getTotal() public function getBasket() } Class Checkout { public function checkout(CreditCard $card) protected function sendOrderConfirmationEmail(CreditCard $card) } https://github.com/neilcrookes/SoSOLIDFu
  • 15. Class Checkout { public function checkout(Basket $basket, CreditCard $card) { try { $reference = $this->gateway->purchase($basket, $card); $this->sendOrderConfirmationEmail($basket, $card); return true; } catch (GatewayException $e) { return false; } } } https://github.com/neilcrookes/SoSOLIDFu
  • 16. Open / Closed Objects or libraries should be open for extension, but closed for modification. ● Easy to add new features OR change behaviour ● Without modifying original ● Purists: without extending original…!? Eh?
  • 17. Open / Closed cont Extension points ● Events ● Plugin architecture ● Composition ● Strategy pattern ● Callbacks
  • 18. Open / Closed cont Closed for modification - Clarity of intent ● Final classes ● Private members ● Encourage extension in expected / supported way ● Encourages decoupling ● Reduces risk of breaking changes
  • 19. final class Checkout { public function checkout(Basket $basket, CreditCard $card) { try { $reference = $this->gateway->purchase($basket, $card); $this->sendOrderConfirmationEmail($basket, $card); fire(new CheckoutEvent($basket)); return true; } catch (GatewayException $e) { return false; } } } https://github.com/neilcrookes/SoSOLIDFu
  • 20. final class Checkout { public function sendOrderConfirmationEmail(Basket $basket, CreditCard $card) { $message = "Thank you for your order.n"; foreach ($basket as $item) { /** @var Product $product */ $product = $item['product']; $message .= "n" . $product->getName() . ' x ' . $item['quantity'] . ' @ £' . $product->getPrice(); } $message .= "nnPayment has been taken from: xxxx xxxx xxxx " . $card->getLast4() . ' Ex: ' . $card->getExpiryMonth() . '/' . $card->getExpiryYear(); mail('customer@domain.com', 'Your order at my store', $message); } } https://github.com/neilcrookes/SoSOLIDFu
  • 21. Liskov Substitution States that you should be able to swap dependencies for a subclass class without causing unexpected results
  • 22. final class Checkout { public function sendOrderConfirmationEmail(Basket $basket, CreditCard $card) { $message = "Thank you for your order.n"; foreach ($basket as $item) { /** @var Product $product */ $product = $item['product']; $message .= "n" . $product->getBasketTitle() . ' x ' . $item['quantity'] . ' @ £' . $product->getPrice(); } $message .= "nnPayment has been taken from: xxxx xxxx xxxx " . $card->getLast4() . ' Ex: ' . $card->getExpiryMonth() . '/' . $card->getExpiryYear(); mail('customer@domain.com', 'Your order at my store', $message); } } https://github.com/neilcrookes/SoSOLIDFu
  • 23. interface PurchasableInterface { public function getId(); public function getPrice(); public function getStock(); public function setStock($stock); public function getBasketTitle(); } https://github.com/neilcrookes/SoSOLIDFu
  • 24. Interface Segregation ● An interface should only impose the methods that a client relies on ● Split unrelated interface methods into separate interfaces
  • 25. interface PurchasableInterface { public function getId(); public function getPrice(); public function getBasketTitle(); } interface StockableInterface { public function getStock(); public function setStock($stock); } https://github.com/neilcrookes/SoSOLIDFu
  • 26. final class Checkout { public function checkout(Basket $basket, CreditCard $card) } class Gateway { public function purchase(Basket $basket, CreditCard $card) } https://github.com/neilcrookes/SoSOLIDFu
  • 27. Dependency Inversion ● High level should not depend on low level modules, both should depend on abstractions ● Abstractions should not depend upon details. Details should depend upon abstractions. ● Depend on abstractions (abstracts / interfaces), not concretes ● Code to an Interface
  • 28. interface ChargeableInterface { public function getChargeableAmount(); } interface GatewayInterface { public function charge(ChargeableInterface $chargeable, PaymentMethodInterface $paymentMethod); } interface PaymentMethodInterface { public function getToken(); public function getDetails(); } https://github.com/neilcrookes/SoSOLIDFu
  • 29. final class Checkout { public function checkout(ChargeableInterface $basket, PaymentMethodInterface $card) } class Gateway { public function purchase(ChargeableInterface $basket, PaymentMethodInterface $card) } https://github.com/neilcrookes/SoSOLIDFu
  • 30. Conclusion: Get Classy ● Actually only add some Interfaces, and an Event ● Our code is designed much better ● Easy to understand, extend ● More robust, less likely to introduce bugs ● Lots of classes is fine ● Lots of interfaces is fine
  • 31. Questions ● Now? ● At the pub? ● @neilcrookes