2016 wordcamp frankfurt - the secret sauce for writing reusable code
TRANSCRIPT
![Page 1: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/1.jpg)
The Secret SauceWriting Reusable
CodeAlain Schlesser www.alainschlesser.comSoftware Engineer & WordPress Consultant @schlessera
![Page 2: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/2.jpg)
The Secret Sauce For Writing 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/
![Page 3: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/3.jpg)
The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser
About The Person In Front Of You
Born in LuxembourgLiving in GermanyWorking in the Cloud
Passionate about:Code quality, software architecture, bestpractices, principles, patterns, andeverything related.
![Page 4: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/4.jpg)
The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser
What To Expect
1. General principle that makes code reusable
2. Common way of implementing this principle
![Page 5: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/5.jpg)
The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser
The Problem With Reusable Code…
![Page 6: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/6.jpg)
The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser
Object-oriented syntax in and of itself does not make your
code reusable.
![Page 7: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/7.jpg)
The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser
So We Need To Rearrange This…
![Page 8: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/8.jpg)
The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser
…Into This
![Page 9: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/9.jpg)
The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser
Design your code so that the reusable parts and the project-specific
parts never intermingle.
![Page 10: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/10.jpg)
The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser
This Easily Allows Us To Go From This…
![Page 11: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/11.jpg)
The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser
…To This
![Page 12: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/12.jpg)
The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser
Example CodeProblem:
We want to have a reusable Greeter class that can show different greetings in different projects.*
* Silly example that can still fit on slides
![Page 13: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/13.jpg)
The Secret Sauce For Writing 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
![Page 14: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/14.jpg)
The Secret Sauce For Writing 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
![Page 15: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/15.jpg)
The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser
The reusable class should completelyignore where it gets its business
logic from. It should act onwhatever gets passed to it.
Injection
![Page 16: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/16.jpg)
The Secret Sauce For Writing 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 ); }}
![Page 17: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/17.jpg)
The Secret Sauce For Writing 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
![Page 18: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/18.jpg)
The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser
Business-specific Code Config File
Confi
g Fil
e
Reusable Class
Business Logic
![Page 19: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/19.jpg)
The Secret Sauce For Writing 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
![Page 20: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/20.jpg)
The Secret Sauce For Writing 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
![Page 21: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/21.jpg)
The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser
Immediate Benefits
• Separate files• Injection• Type-hinting• Validation
![Page 22: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/22.jpg)
The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser
Secondary Benefits
• Forced modularisation• Tested code• Collaboration
![Page 23: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/23.jpg)
The Secret Sauce For Writing 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…
![Page 24: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/24.jpg)
The Secret Sauce For Writing 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
![Page 25: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/25.jpg)
The Secret Sauce For Writing 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' ); }}
![Page 26: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/26.jpg)
The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser
Confi
g Fil
e
Getting The Config File Into The Reusable Class
Reusable Class
Plugin ClassBusiness Logic
Business Logic
Business Logic
![Page 27: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/27.jpg)
The Secret Sauce For Writing 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
![Page 28: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/28.jpg)
The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser
<?php
namespace Example\Greeter;
$config = ConfigFactory::create( __DIR__ . 'config/defaults.php' );
$plugin = new Plugin( $config->getSubConfig( __NAMESPACE__ ) );
$plugin->run();
Prefix For Multiple Plugins
![Page 29: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/29.jpg)
The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser
Configs & Auto-wiring Injectioninterface DatabaseConfig extends ConfigInterface { }
class Database {
/** @var DatabaseConfig */ protected $config;
/** * @param DatabaseConfig $config The Config to use. */ public function __construct( DatabaseConfig $config ) { $this->config = $config; }}
![Page 30: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/30.jpg)
The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser
Domain-Specific Language
Domain-specific language (noun):
a computer programming language of limitedexpressiveness focused on a particular domain.
- Martin Fowler, ThoughtWorks
![Page 31: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/31.jpg)
The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser
PHP Config ... Closures! ( = ~DSL )
![Page 32: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/32.jpg)
The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser
Adapted Configs
Provide defaults and then override with different Configs …
• 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)• …
![Page 33: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/33.jpg)
The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser
Site/App-specific Configurationsconfig/defaults.php
config/dt.php
![Page 34: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/34.jpg)
The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser
![Page 35: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/35.jpg)
The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser
Config Library
https://github.com/brightnucleus/config
My own library:
Alternatives:
https://github.com/symfony/configsymfony/config
https://github.com/zendframework/zend-configzendframework/zend-config
brightnucleus/config
![Page 36: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/36.jpg)
The Secret Sauce For Writing 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
![Page 37: 2016 WordCamp Frankfurt - The Secret Sauce For Writing Reusable Code](https://reader035.vdocuments.us/reader035/viewer/2022081604/589e05af1a28ab67278b4b1d/html5/thumbnails/37.jpg)
The Secret Sauce For Writing Reusable Code – 4th September 2016– Alain Schlesser
The EndI’m Alain Schlesser.
Follow me on twitter: @schlessera
Or visit my site:www.alainschlesser.com