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

How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
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
 
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
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Best Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfBest Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfIdiosysTechnologies1
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
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
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 

Recently uploaded (20)

How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
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
 
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...
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Best Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfBest Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdf
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
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...
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 

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