This talk was held at WordCamp Frankfurt on Sep. 4, 2016 (https://2016.frankfurt.wordcamp.org/session/the-secret-sauce-for-writing-reusable-code/)
There’s an (on-going) series of complimentary blog posts:
https://www.alainschlesser.com/config-files-for-reusable-code/
"Everyone knows that you need to write reusable code to be able to grow as a developer, right?
However, most developers struggle to understand how to split up their code to make it truly reusable, so they end up copy-pasting parts of code and modifying as needed, instead of effectively reusing the code that was already written, without a single change.
This session explains the concept of Config files and how they allow you to cleanly separate reusable code from project-specific code."
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
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
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