SlideShare a Scribd company logo
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 2016
djrewerb
 
WordPress SEO | Campixx 2016 | Hans Jung
WordPress SEO | Campixx 2016 | Hans JungWordPress SEO | Campixx 2016 | Hans Jung
WordPress SEO | Campixx 2016 | Hans Jung
Hans 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 überzeugen
Ehrhardt Heinold
 
2016 #WCFAY Anatomy of a Website
2016 #WCFAY Anatomy of a Website2016 #WCFAY Anatomy of a Website
2016 #WCFAY Anatomy of a Website
Jamie's Notebook
 
WordPress 101 from WordCamp Cincinatti 2016
WordPress 101 from WordCamp Cincinatti 2016WordPress 101 from WordCamp Cincinatti 2016
WordPress 101 from WordCamp Cincinatti 2016
Angela 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 + Git
frankstaude
 
WordPress Performance optimization
WordPress Performance optimizationWordPress Performance optimization
WordPress Performance optimization
Brecht Ryckaert
 
Flexing Your WordPress Themes
Flexing Your WordPress ThemesFlexing Your WordPress Themes
Flexing Your WordPress Themes
Tim 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 Wordpress
Digamber 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 Expectations
Steve 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 cats
Becky Davis
 
My Contributor Story
My Contributor StoryMy Contributor Story
My Contributor Story
Marko 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 Crazy
John 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 Project
xsist10
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
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
Peter Souter
 
Supercharging WordPress Development in 2018
Supercharging WordPress Development in 2018Supercharging WordPress Development in 2018
Supercharging WordPress Development in 2018
Adam 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 apps
Raul 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 2
CodeIgniter 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 Time
Dmitry 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 Pecoraro
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"
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 2023
Mark 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] 2023
Mark 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 trying
Alex Rupérez
 
The Enterprise Wor/d/thy/Press
The Enterprise Wor/d/thy/PressThe Enterprise Wor/d/thy/Press
The Enterprise Wor/d/thy/Press
Jeroen van Dijk
 

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
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
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
 

Recently uploaded

AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
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
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
Tier1 app
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
RISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent EnterpriseRISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent Enterprise
Srikant77
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 

Recently uploaded (20)

AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
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
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
RISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent EnterpriseRISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent Enterprise
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 

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