SlideShare a Scribd company logo
1 of 37
Download to read offline
The Secret Sauce
Writing Reusable Code
Alain Schlesser www.alainschlesser.com
Software Engineer & WordPress Consultant @schlessera
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
NOTE:
This talk was held at WordCamp Frankfurt on Sep. 4, 2016.
There’s an (on-going) series of complimentary blog posts:
https://www.alainschlesser.com/config-files-for-reusable-code/
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
About The Person In Front Of You
Born in Luxembourg
Living in Germany
Working in the Cloud
Passionate about:
Code quality, software architecture, best
practices, principles, patterns, and
everything related.
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
What To Expect
1. General principle that makes
code reusable
2. Common way of
implementing this principle
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
The Problem With Reusable Code…
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
Object-oriented syntax in and
of itself does not make your
code reusable.
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
So We Need To Rearrange This…
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
…Into This
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
Design your code so that the
reusable parts and the project-
specific parts never intermingle.
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
This Easily Allows Us To Go From This…
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
…To This
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
Example Code
Problem:
We want to have a reusable
Greeter class that can show
different greetings in different
projects.*
* Silly example that can still fit on slides
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
class Greeter {
/**
* @param string $name The name of the person to greet.
*/
public function greet( $name ) {
printf( 'Hello %2$s!', $name );
}
}
Mixed Code Types
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
class Greeter {
/**
* @param string $name The name of the person to greet.
*/
public function greet( $name ) {
printf( 'Hello %2$s!', $name );
}
}
= reusable code = project-specific code
Mixed Code Types
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
The reusable class should completely
ignore where it gets its business
logic from. It should act on
whatever gets passed to it.
à Injection
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
class ReusableGreeter {
/** @var ConfigInterface */
protected $config;
/**
* @param ConfigInterface $config The Config to use.
*/
public function __construct( ConfigInterface $config ) {
$this->config = $config;
}
/**
* @param string $name The name of the person to greet.
*/
public function greet( $name ) {
$greeting = $this->config->get( 'greeting' );
printf( '%1$s %2$s!', $greeting, $name );
}
}
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
class ReusableGreeter {
/** @var ConfigInterface */
protected $config;
/**
* @param ConfigInterface $config The Config to use.
*/
public function __construct( ConfigInterface $config ) {
$this->config = $config;
}
/**
* @param string $name The name of the person to greet.
*/
public function greet( $name ) {
$greeting = $this->config->get( 'greeting' );
printf( '%1$s %2$s!', $greeting, $name );
}
}
= reusable code = project-specific code
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
Business-specific Code à Config File
ConfigFile
Reusable Class
Business Logic
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
<?php
// We return a standard PHP array as a result of including
// this Config file.
return [
'greeting' => 'Hello',
];
Basic Config File
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
<?php
// We return a standard PHP array as a result of including
// this Config file.
return [
'greeting' => 'Hello',
];
= reusable code = project-specific code
=>
Basic Config File
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
Immediate Benefits
• Separate files
• Injection
• Type-hinting
• Validation
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
Secondary Benefits
• Forced modularisation
• Tested code
• Collaboration
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
Source Is Flexible
As the source should be irrelevant for the
classes that use the Config data, you can
combine several files into one, read them
from a database or network, build them at
run-time for unit tests, etc…
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
<?php
// We can prefix a section of a Config, so that one file
// can be used for multiple classes.
return [
'ReusableGreeter' => [
'greeting' => 'Hello',
],
];
Prefix For The Reusable Class
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
class Plugin {
/** @var ConfigInterface */
protected $config;
/**
* @param ConfigInterface $config The Config to use.
*/
public function __construct( ConfigInterface $config ) {
$this->config = $config;
}
public function run() {
$greeter = new ReusableGreeter(
$this->config->getSubConfig( 'ReusableGreeter' )
);
$greeter->greet( 'World' );
}
}
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
ConfigFile
Getting The Config File Into The Reusable Class
Reusable Class
Plugin ClassBusiness Logic
Business Logic
Business Logic
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
<?php
// We can prefix the entire Config, so that a single file
// can be shared across several plugins.
$reusable_greeter = [
'greeting' => 'Hello',
];
return [
'Example' => [
'Greeter' => [
'ReusableGreeter' => $reusable_greeter;
],
],
];
Prefix For Multiple Plugins
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
<?php
namespace ExampleGreeter;
$config = ConfigFactory::create( __DIR__ . 'config/defaults.php' );
$plugin = new Plugin( $config->getSubConfig( __NAMESPACE__ ) );
$plugin->run();
Prefix For Multiple Plugins
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
Configs & Auto-wiring Injection
interface DatabaseConfig extends ConfigInterface { }
class Database {
/** @var DatabaseConfig */
protected $config;
/**
* @param DatabaseConfig $config The Config to use.
*/
public function __construct( DatabaseConfig $config ) {
$this->config = $config;
}
}
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
Domain-Specific Language
Domain-specific language (noun):
a computer programming language of limited
expressiveness focused on a particular domain.
- Martin Fowler, ThoughtWorks
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
PHP Config ... Closures! ( = ~DSL )
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
Adapted Configs
Provide defaults and then overridewith differentConfigs …
• for different sites/apps (site_a.php)
• for different environments (site_a-development.php)
• for different contexts (unit-tests.php)
• for specific situations (run-backups.php)
• …
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
Site/App-specific Configurations
config/defaults.php
config/dt.php
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
Config Library
https://github.com/brightnucleus/config
My own library:
Alternatives:
https://github.com/symfony/config
symfony/config
https://github.com/zendframework/zend-config
zendframework/zend-config
brightnucleus/config
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
“Can You Summarize, Please?”
• Design for reusability from the get-go
• Have a clean separation between
different types of code
• Config files provide a structured way of
injecting project-specific logic into
reusable classes
The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
The End
I’m Alain Schlesser.
Follow me on twitter:
@schlessera
Or visit my site:
www.alainschlesser.com

More Related Content

Viewers also liked

Podcasten ist Bloggen, nur mit der Stimme, WordCamp Nürnberg 2016
Podcasten ist Bloggen, nur mit der Stimme, WordCamp Nürnberg 2016Podcasten ist Bloggen, nur mit der Stimme, WordCamp Nürnberg 2016
Podcasten ist Bloggen, nur mit der Stimme, WordCamp Nürnberg 2016djrewerb
 
WordPress SEO | Campixx 2016 | Hans Jung
WordPress SEO | Campixx 2016 | Hans JungWordPress SEO | Campixx 2016 | Hans Jung
WordPress SEO | Campixx 2016 | Hans JungHans Jung
 
Application package
Application packageApplication package
Application packageJAYAARC
 
Wie Printformate auch in einer digitalen Welt überzeugen
Wie Printformate auch in einer digitalen Welt überzeugenWie Printformate auch in einer digitalen Welt überzeugen
Wie Printformate auch in einer digitalen Welt überzeugenEhrhardt Heinold
 
2016 #WCFAY Anatomy of a Website
2016 #WCFAY Anatomy of a Website2016 #WCFAY Anatomy of a Website
2016 #WCFAY Anatomy of a WebsiteJamie's Notebook
 
WordPress 101 from WordCamp Cincinatti 2016
WordPress 101 from WordCamp Cincinatti 2016WordPress 101 from WordCamp Cincinatti 2016
WordPress 101 from WordCamp Cincinatti 2016Angela Meeker
 
WordPress mit React – Mehr als eine Zweckehe?!
WordPress mit React – Mehr als eine Zweckehe?!WordPress mit React – Mehr als eine Zweckehe?!
WordPress mit React – Mehr als eine Zweckehe?!Paul Vincent Beigang
 
VersionPress - WordPress + Git
VersionPress - WordPress + GitVersionPress - WordPress + Git
VersionPress - WordPress + Gitfrankstaude
 
WordPress Performance optimization
WordPress Performance optimizationWordPress Performance optimization
WordPress Performance optimizationBrecht Ryckaert
 
Flexing Your WordPress Themes
Flexing Your WordPress ThemesFlexing Your WordPress Themes
Flexing Your WordPress ThemesTim Blodgett
 
WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes t...
WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes t...WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes t...
WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes t...allilevine
 
WooCommerce: An E-Commerce Solution for Wordpress
WooCommerce: An E-Commerce Solution for WordpressWooCommerce: An E-Commerce Solution for Wordpress
WooCommerce: An E-Commerce Solution for WordpressDigamber Pradhan
 
Diabeł tkwi w szczegółach...
Diabeł tkwi w szczegółach...Diabeł tkwi w szczegółach...
Diabeł tkwi w szczegółach...Ewa Karaszkiewicz
 
Pressnomics 2015 - Managing Client Expectations
Pressnomics 2015 - Managing Client ExpectationsPressnomics 2015 - Managing Client Expectations
Pressnomics 2015 - Managing Client ExpectationsSteve Zehngut
 
Project Management or how to herd cats
Project Management or how to herd catsProject Management or how to herd cats
Project Management or how to herd catsBecky Davis
 
My Contributor Story
My Contributor StoryMy Contributor Story
My Contributor StoryMarko Heijnen
 
Using the Editor the Proper Way - WordCamp Toronto 2015
Using the Editor the Proper Way - WordCamp Toronto 2015Using the Editor the Proper Way - WordCamp Toronto 2015
Using the Editor the Proper Way - WordCamp Toronto 2015sethta
 
Managing Clients without Going Crazy
Managing Clients without Going CrazyManaging Clients without Going Crazy
Managing Clients without Going CrazyJohn Eckman
 
Learning java script and wordpress rest api by tom hermans wordcamp netherl...
Learning java script and wordpress rest api by tom hermans   wordcamp netherl...Learning java script and wordpress rest api by tom hermans   wordcamp netherl...
Learning java script and wordpress rest api by tom hermans wordcamp netherl...Tom Hermans
 

Viewers also liked (19)

Podcasten ist Bloggen, nur mit der Stimme, WordCamp Nürnberg 2016
Podcasten ist Bloggen, nur mit der Stimme, WordCamp Nürnberg 2016Podcasten ist Bloggen, nur mit der Stimme, WordCamp Nürnberg 2016
Podcasten ist Bloggen, nur mit der Stimme, WordCamp Nürnberg 2016
 
WordPress SEO | Campixx 2016 | Hans Jung
WordPress SEO | Campixx 2016 | Hans JungWordPress SEO | Campixx 2016 | Hans Jung
WordPress SEO | Campixx 2016 | Hans Jung
 
Application package
Application packageApplication package
Application package
 
Wie Printformate auch in einer digitalen Welt überzeugen
Wie Printformate auch in einer digitalen Welt überzeugenWie Printformate auch in einer digitalen Welt überzeugen
Wie Printformate auch in einer digitalen Welt überzeugen
 
2016 #WCFAY Anatomy of a Website
2016 #WCFAY Anatomy of a Website2016 #WCFAY Anatomy of a Website
2016 #WCFAY Anatomy of a Website
 
WordPress 101 from WordCamp Cincinatti 2016
WordPress 101 from WordCamp Cincinatti 2016WordPress 101 from WordCamp Cincinatti 2016
WordPress 101 from WordCamp Cincinatti 2016
 
WordPress mit React – Mehr als eine Zweckehe?!
WordPress mit React – Mehr als eine Zweckehe?!WordPress mit React – Mehr als eine Zweckehe?!
WordPress mit React – Mehr als eine Zweckehe?!
 
VersionPress - WordPress + Git
VersionPress - WordPress + GitVersionPress - WordPress + Git
VersionPress - WordPress + Git
 
WordPress Performance optimization
WordPress Performance optimizationWordPress Performance optimization
WordPress Performance optimization
 
Flexing Your WordPress Themes
Flexing Your WordPress ThemesFlexing Your WordPress Themes
Flexing Your WordPress Themes
 
WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes t...
WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes t...WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes t...
WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes t...
 
WooCommerce: An E-Commerce Solution for Wordpress
WooCommerce: An E-Commerce Solution for WordpressWooCommerce: An E-Commerce Solution for Wordpress
WooCommerce: An E-Commerce Solution for Wordpress
 
Diabeł tkwi w szczegółach...
Diabeł tkwi w szczegółach...Diabeł tkwi w szczegółach...
Diabeł tkwi w szczegółach...
 
Pressnomics 2015 - Managing Client Expectations
Pressnomics 2015 - Managing Client ExpectationsPressnomics 2015 - Managing Client Expectations
Pressnomics 2015 - Managing Client Expectations
 
Project Management or how to herd cats
Project Management or how to herd catsProject Management or how to herd cats
Project Management or how to herd cats
 
My Contributor Story
My Contributor StoryMy Contributor Story
My Contributor Story
 
Using the Editor the Proper Way - WordCamp Toronto 2015
Using the Editor the Proper Way - WordCamp Toronto 2015Using the Editor the Proper Way - WordCamp Toronto 2015
Using the Editor the Proper Way - WordCamp Toronto 2015
 
Managing Clients without Going Crazy
Managing Clients without Going CrazyManaging Clients without Going Crazy
Managing Clients without Going Crazy
 
Learning java script and wordpress rest api by tom hermans wordcamp netherl...
Learning java script and wordpress rest api by tom hermans   wordcamp netherl...Learning java script and wordpress rest api by tom hermans   wordcamp netherl...
Learning java script and wordpress rest api by tom hermans wordcamp netherl...
 

Similar to The Secret Sauce For Writing Reusable Code

PHP SA 2014 - Releasing Your Open Source Project
PHP SA 2014 - Releasing Your Open Source ProjectPHP SA 2014 - Releasing Your Open Source Project
PHP SA 2014 - Releasing Your Open Source Projectxsist10
 
Head in the Clouds: Testing Infra as Code - Config Management 2020
Head in the Clouds: Testing Infra as Code - Config Management 2020Head in the Clouds: Testing Infra as Code - Config Management 2020
Head in the Clouds: Testing Infra as Code - Config Management 2020Peter Souter
 
Supercharging WordPress Development in 2018
Supercharging WordPress Development in 2018Supercharging WordPress Development in 2018
Supercharging WordPress Development in 2018Adam Tomat
 
$kernel->infect(): Creating a cryptovirus for Symfony2 apps
$kernel->infect(): Creating a cryptovirus for Symfony2 apps$kernel->infect(): Creating a cryptovirus for Symfony2 apps
$kernel->infect(): Creating a cryptovirus for Symfony2 appsRaul Fraile
 
Creating WordPress Theme Faster, Smarter & Without Swearing
Creating WordPress Theme Faster, Smarter & Without SwearingCreating WordPress Theme Faster, Smarter & Without Swearing
Creating WordPress Theme Faster, Smarter & Without Swearingmartinwolak
 
CICON2010: Adam Griffiths - CodeIgniter 2
CICON2010: Adam Griffiths - CodeIgniter 2CICON2010: Adam Griffiths - CodeIgniter 2
CICON2010: Adam Griffiths - CodeIgniter 2CodeIgniter Conference
 
7 Tips on Getting Your Theme Approved the First Time
7 Tips on Getting Your Theme Approved the First Time7 Tips on Getting Your Theme Approved the First Time
7 Tips on Getting Your Theme Approved the First TimeDmitry Mayorov
 
RESTful API development in Laravel 4 - Christopher Pecoraro
RESTful API development in Laravel 4 - Christopher PecoraroRESTful API development in Laravel 4 - Christopher Pecoraro
RESTful API development in Laravel 4 - Christopher PecoraroChristopher Pecoraro
 
Tobias Nyholm "Deep dive into Symfony 4 internals"
Tobias Nyholm "Deep dive into Symfony 4 internals"Tobias Nyholm "Deep dive into Symfony 4 internals"
Tobias Nyholm "Deep dive into Symfony 4 internals"Fwdays
 
Filesystem Management with Flysystem at PHP UK 2023
Filesystem Management with Flysystem at PHP UK 2023Filesystem Management with Flysystem at PHP UK 2023
Filesystem Management with Flysystem at PHP UK 2023Mark Niebergall
 
Filesystem Management with Flysystem - php[tek] 2023
Filesystem Management with Flysystem - php[tek] 2023Filesystem Management with Flysystem - php[tek] 2023
Filesystem Management with Flysystem - php[tek] 2023Mark Niebergall
 
Gigigo Workshop - Create an iOS Framework, document it and not die trying
Gigigo Workshop - Create an iOS Framework, document it and not die tryingGigigo Workshop - Create an iOS Framework, document it and not die trying
Gigigo Workshop - Create an iOS Framework, document it and not die tryingAlex Rupérez
 
The Enterprise Wor/d/thy/Press
The Enterprise Wor/d/thy/PressThe Enterprise Wor/d/thy/Press
The Enterprise Wor/d/thy/PressJeroen van Dijk
 
Webinar - Windows Application Management with Puppet
Webinar - Windows Application Management with PuppetWebinar - Windows Application Management with Puppet
Webinar - Windows Application Management with PuppetOlinData
 

Similar to The Secret Sauce For Writing Reusable Code (20)

PHP SA 2014 - Releasing Your Open Source Project
PHP SA 2014 - Releasing Your Open Source ProjectPHP SA 2014 - Releasing Your Open Source Project
PHP SA 2014 - Releasing Your Open Source Project
 
Head in the Clouds: Testing Infra as Code - Config Management 2020
Head in the Clouds: Testing Infra as Code - Config Management 2020Head in the Clouds: Testing Infra as Code - Config Management 2020
Head in the Clouds: Testing Infra as Code - Config Management 2020
 
Supercharging WordPress Development in 2018
Supercharging WordPress Development in 2018Supercharging WordPress Development in 2018
Supercharging WordPress Development in 2018
 
$kernel->infect(): Creating a cryptovirus for Symfony2 apps
$kernel->infect(): Creating a cryptovirus for Symfony2 apps$kernel->infect(): Creating a cryptovirus for Symfony2 apps
$kernel->infect(): Creating a cryptovirus for Symfony2 apps
 
Creating WordPress Theme Faster, Smarter & Without Swearing
Creating WordPress Theme Faster, Smarter & Without SwearingCreating WordPress Theme Faster, Smarter & Without Swearing
Creating WordPress Theme Faster, Smarter & Without Swearing
 
CICON2010: Adam Griffiths - CodeIgniter 2
CICON2010: Adam Griffiths - CodeIgniter 2CICON2010: Adam Griffiths - CodeIgniter 2
CICON2010: Adam Griffiths - CodeIgniter 2
 
7 Tips on Getting Your Theme Approved the First Time
7 Tips on Getting Your Theme Approved the First Time7 Tips on Getting Your Theme Approved the First Time
7 Tips on Getting Your Theme Approved the First Time
 
RESTful API development in Laravel 4 - Christopher Pecoraro
RESTful API development in Laravel 4 - Christopher PecoraroRESTful API development in Laravel 4 - Christopher Pecoraro
RESTful API development in Laravel 4 - Christopher Pecoraro
 
Tobias Nyholm "Deep dive into Symfony 4 internals"
Tobias Nyholm "Deep dive into Symfony 4 internals"Tobias Nyholm "Deep dive into Symfony 4 internals"
Tobias Nyholm "Deep dive into Symfony 4 internals"
 
Filesystem Management with Flysystem at PHP UK 2023
Filesystem Management with Flysystem at PHP UK 2023Filesystem Management with Flysystem at PHP UK 2023
Filesystem Management with Flysystem at PHP UK 2023
 
Filesystem Management with Flysystem - php[tek] 2023
Filesystem Management with Flysystem - php[tek] 2023Filesystem Management with Flysystem - php[tek] 2023
Filesystem Management with Flysystem - php[tek] 2023
 
extending-php
extending-phpextending-php
extending-php
 
extending-php
extending-phpextending-php
extending-php
 
extending-php
extending-phpextending-php
extending-php
 
extending-php
extending-phpextending-php
extending-php
 
extending-php
extending-phpextending-php
extending-php
 
extending-php
extending-phpextending-php
extending-php
 
Gigigo Workshop - Create an iOS Framework, document it and not die trying
Gigigo Workshop - Create an iOS Framework, document it and not die tryingGigigo Workshop - Create an iOS Framework, document it and not die trying
Gigigo Workshop - Create an iOS Framework, document it and not die trying
 
The Enterprise Wor/d/thy/Press
The Enterprise Wor/d/thy/PressThe Enterprise Wor/d/thy/Press
The Enterprise Wor/d/thy/Press
 
Webinar - Windows Application Management with Puppet
Webinar - Windows Application Management with PuppetWebinar - Windows Application Management with Puppet
Webinar - Windows Application Management with Puppet
 

Recently uploaded

Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLionel Briand
 
Lecture # 8 software design and architecture (SDA).ppt
Lecture # 8 software design and architecture (SDA).pptLecture # 8 software design and architecture (SDA).ppt
Lecture # 8 software design and architecture (SDA).pptesrabilgic2
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxAndreas Kunz
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identityteam-WIBU
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecturerahul_net
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 

Recently uploaded (20)

Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and Repair
 
Lecture # 8 software design and architecture (SDA).ppt
Lecture # 8 software design and architecture (SDA).pptLecture # 8 software design and architecture (SDA).ppt
Lecture # 8 software design and architecture (SDA).ppt
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identity
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecture
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 

The Secret Sauce For Writing Reusable Code

  • 1. The Secret Sauce Writing Reusable Code Alain Schlesser www.alainschlesser.com Software Engineer & WordPress Consultant @schlessera
  • 2. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser NOTE: This talk was held at WordCamp Frankfurt on Sep. 4, 2016. There’s an (on-going) series of complimentary blog posts: https://www.alainschlesser.com/config-files-for-reusable-code/
  • 3. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser About The Person In Front Of You Born in Luxembourg Living in Germany Working in the Cloud Passionate about: Code quality, software architecture, best practices, principles, patterns, and everything related.
  • 4. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser What To Expect 1. General principle that makes code reusable 2. Common way of implementing this principle
  • 5. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser The Problem With Reusable Code…
  • 6. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser Object-oriented syntax in and of itself does not make your code reusable.
  • 7. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser So We Need To Rearrange This…
  • 8. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser …Into This
  • 9. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser Design your code so that the reusable parts and the project- specific parts never intermingle.
  • 10. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser This Easily Allows Us To Go From This…
  • 11. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser …To This
  • 12. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser Example Code Problem: We want to have a reusable Greeter class that can show different greetings in different projects.* * Silly example that can still fit on slides
  • 13. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser class Greeter { /** * @param string $name The name of the person to greet. */ public function greet( $name ) { printf( 'Hello %2$s!', $name ); } } Mixed Code Types
  • 14. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser class Greeter { /** * @param string $name The name of the person to greet. */ public function greet( $name ) { printf( 'Hello %2$s!', $name ); } } = reusable code = project-specific code Mixed Code Types
  • 15. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser The reusable class should completely ignore where it gets its business logic from. It should act on whatever gets passed to it. à Injection
  • 16. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser class ReusableGreeter { /** @var ConfigInterface */ protected $config; /** * @param ConfigInterface $config The Config to use. */ public function __construct( ConfigInterface $config ) { $this->config = $config; } /** * @param string $name The name of the person to greet. */ public function greet( $name ) { $greeting = $this->config->get( 'greeting' ); printf( '%1$s %2$s!', $greeting, $name ); } }
  • 17. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser class ReusableGreeter { /** @var ConfigInterface */ protected $config; /** * @param ConfigInterface $config The Config to use. */ public function __construct( ConfigInterface $config ) { $this->config = $config; } /** * @param string $name The name of the person to greet. */ public function greet( $name ) { $greeting = $this->config->get( 'greeting' ); printf( '%1$s %2$s!', $greeting, $name ); } } = reusable code = project-specific code
  • 18. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser Business-specific Code à Config File ConfigFile Reusable Class Business Logic
  • 19. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser <?php // We return a standard PHP array as a result of including // this Config file. return [ 'greeting' => 'Hello', ]; Basic Config File
  • 20. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser <?php // We return a standard PHP array as a result of including // this Config file. return [ 'greeting' => 'Hello', ]; = reusable code = project-specific code => Basic Config File
  • 21. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser Immediate Benefits • Separate files • Injection • Type-hinting • Validation
  • 22. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser Secondary Benefits • Forced modularisation • Tested code • Collaboration
  • 23. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser Source Is Flexible As the source should be irrelevant for the classes that use the Config data, you can combine several files into one, read them from a database or network, build them at run-time for unit tests, etc…
  • 24. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser <?php // We can prefix a section of a Config, so that one file // can be used for multiple classes. return [ 'ReusableGreeter' => [ 'greeting' => 'Hello', ], ]; Prefix For The Reusable Class
  • 25. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser class Plugin { /** @var ConfigInterface */ protected $config; /** * @param ConfigInterface $config The Config to use. */ public function __construct( ConfigInterface $config ) { $this->config = $config; } public function run() { $greeter = new ReusableGreeter( $this->config->getSubConfig( 'ReusableGreeter' ) ); $greeter->greet( 'World' ); } }
  • 26. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser ConfigFile Getting The Config File Into The Reusable Class Reusable Class Plugin ClassBusiness Logic Business Logic Business Logic
  • 27. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser <?php // We can prefix the entire Config, so that a single file // can be shared across several plugins. $reusable_greeter = [ 'greeting' => 'Hello', ]; return [ 'Example' => [ 'Greeter' => [ 'ReusableGreeter' => $reusable_greeter; ], ], ]; Prefix For Multiple Plugins
  • 28. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser <?php namespace ExampleGreeter; $config = ConfigFactory::create( __DIR__ . 'config/defaults.php' ); $plugin = new Plugin( $config->getSubConfig( __NAMESPACE__ ) ); $plugin->run(); Prefix For Multiple Plugins
  • 29. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser Configs & Auto-wiring Injection interface DatabaseConfig extends ConfigInterface { } class Database { /** @var DatabaseConfig */ protected $config; /** * @param DatabaseConfig $config The Config to use. */ public function __construct( DatabaseConfig $config ) { $this->config = $config; } }
  • 30. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser Domain-Specific Language Domain-specific language (noun): a computer programming language of limited expressiveness focused on a particular domain. - Martin Fowler, ThoughtWorks
  • 31. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser PHP Config ... Closures! ( = ~DSL )
  • 32. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser Adapted Configs Provide defaults and then overridewith differentConfigs … • for different sites/apps (site_a.php) • for different environments (site_a-development.php) • for different contexts (unit-tests.php) • for specific situations (run-backups.php) • …
  • 33. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser Site/App-specific Configurations config/defaults.php config/dt.php
  • 34. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser
  • 35. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser Config Library https://github.com/brightnucleus/config My own library: Alternatives: https://github.com/symfony/config symfony/config https://github.com/zendframework/zend-config zendframework/zend-config brightnucleus/config
  • 36. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser “Can You Summarize, Please?” • Design for reusability from the get-go • Have a clean separation between different types of code • Config files provide a structured way of injecting project-specific logic into reusable classes
  • 37. The Secret Sauce ForWriting Reusable Code – 4th September 2016– Alain Schlesser The End I’m Alain Schlesser. Follow me on twitter: @schlessera Or visit my site: www.alainschlesser.com